diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb
index 29b96a1..e5d7d90 100644
--- a/lib/cocoon/view_helpers.rb
+++ b/lib/cocoon/view_helpers.rb
@@ -31,8 +31,9 @@ module Cocoon
end
# :nodoc:
- def render_association(association, f, new_object)
- f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
+ def render_association(association, f, new_object, render_options={})
+ method_name = f.respond_to?(:semantic_fields_for) ? :semantic_fields_for : (f.respond_to?(:simple_fields_for) ? :simple_fields_for : :fields_for)
+ f.send(method_name, association, new_object, {:child_index => "new_#{association}"}.merge(render_options)) do |builder|
render(association.to_s.singularize + "_fields", :f => builder, :dynamic => true)
end
end
@@ -57,12 +58,15 @@ module Cocoon
association = args[2]
html_options = args[3] || {}
+ render_options = html_options.delete(:render_options)
+ render_options ||={}
+
html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
html_options[:'data-association'] = association.to_s.singularize
html_options[:'data-associations'] = association.to_s.pluralize
new_object = f.object.class.reflect_on_association(association).klass.new
- html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object)).html_safe
+ html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options)).html_safe
link_to(name, '#', html_options )
end
diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb
index 71b10d0..dbdfbaf 100644
--- a/spec/cocoon_spec.rb
+++ b/spec/cocoon_spec.rb
@@ -52,7 +52,48 @@ describe Cocoon do
result = @tester.link_to_add_association('add something', @form_obj, :people)
result.to_s.should == 'add something'
end
+ end
+ it "tttt" do
+ @post.class.reflect_on_association(:people).klass.new.should be_a(Person)
+ end
+
+ context "with extra render-options for rendering the child relation" do
+ it "should use the correct plural" do
+ @tester.should_receive(:render_association).with(:people, @form_obj, anything, {:wrapper => 'inline'})
+ result = @tester.link_to_add_association('add something', @form_obj, :people, :render_options => {:wrapper => 'inline'})
+ result.to_s.should == 'add something'
+ end
+ end
+
+ context "when using formtastic" do
+ before(:each) do
+ @tester.unstub(:render_association)
+ @form_obj.stub(:semantic_fields_for).and_return('form')
+ end
+ it "calls semantic_fields_for and not fields_for" do
+ @form_obj.should_receive(:semantic_fields_for)
+ @form_obj.should_receive(:fields_for).never
+ result = @tester.link_to_add_association('add something', @form_obj, :people)
+ result.to_s.should == 'add something'
+
+ end
+ end
+ context "when using simple_form" do
+ before(:each) do
+ @tester.unstub(:render_association)
+ @form_obj.stub(:simple_fields_for).and_return('form')
+ end
+ it "responds_to :simple_fields_for" do
+ @form_obj.should respond_to(:simple_fields_for)
+ end
+ it "calls simple_fields_for and not fields_for" do
+ @form_obj.should_receive(:simple_fields_for)
+ @form_obj.should_receive(:fields_for).never
+ result = @tester.link_to_add_association('add something', @form_obj, :people)
+ result.to_s.should == 'add something'
+
+ end
end
end
diff --git a/spec/dummy/app/models/person.rb b/spec/dummy/app/models/person.rb
index 2f2e286..474e73f 100644
--- a/spec/dummy/app/models/person.rb
+++ b/spec/dummy/app/models/person.rb
@@ -1,2 +1,3 @@
class Person < ActiveRecord::Base
+ belongs_to :post
end