diff --git a/Gemfile b/Gemfile index e857791..fe80d9b 100644 --- a/Gemfile +++ b/Gemfile @@ -16,4 +16,4 @@ end # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+) # gem 'ruby-debug' -# gem 'ruby-debug19' \ No newline at end of file +# gem 'ruby-debug19' diff --git a/Rakefile b/Rakefile index 8b35f85..a09d6cb 100644 --- a/Rakefile +++ b/Rakefile @@ -39,4 +39,4 @@ begin Jeweler::GemcutterTasks.new rescue LoadError puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler" -end \ No newline at end of file +end diff --git a/lib/cocoon.rb b/lib/cocoon.rb index ec9039c..3570373 100644 --- a/lib/cocoon.rb +++ b/lib/cocoon.rb @@ -10,7 +10,11 @@ module Cocoon # configure our plugin on boot initializer "cocoon.initialize" do |app| ActionView::Base.send :include, Cocoon::ViewHelpers + + if Object.const_defined?("Formtastic") and Formtastic.const_defined?("Inputs") + require 'cocoon/formtastic/cocoon_input' + end end end -end \ No newline at end of file +end diff --git a/lib/cocoon/formtastic/cocoon_input.rb b/lib/cocoon/formtastic/cocoon_input.rb new file mode 100644 index 0000000..209f157 --- /dev/null +++ b/lib/cocoon/formtastic/cocoon_input.rb @@ -0,0 +1,24 @@ +require 'formtastic' + +class CocoonInput + include ::Formtastic::Inputs::Base + + def to_html + output = [] + + output << label_html + + output << builder.semantic_fields_for(method) do |fields| + if fields.object + template.render :partial => "#{method.to_s.singularize}_fields", :locals => { :f => fields } + end + end + + output << template.content_tag(:div, :class => 'links') do + template.link_to_add_association template.t('.add'), builder, method + end + + template.content_tag(:li, output.join('').html_safe, :class => 'input cocoon') + end +end +