Added an extra option :render_option which is handed to the nested fields for builder. Also call the correct nested fields builder for each form builder (e.g. semantic_fields_for, simple_fields_for or fields_for).

This commit is contained in:
nathanvda 2011-10-22 01:42:07 +02:00
parent 5daa7d3c26
commit 003040bc0b
3 changed files with 49 additions and 3 deletions

View File

@ -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

View File

@ -52,7 +52,48 @@ describe Cocoon do
result = @tester.link_to_add_association('add something', @form_obj, :people)
result.to_s.should == '<a href="#" class="add_fields" data-association="person" data-associations="people" data-template="form&lt;tag&gt;">add something</a>'
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 == '<a href="#" class="add_fields" data-association="person" data-associations="people" data-template="form&lt;tag&gt;">add something</a>'
end
end
context "when using formtastic" do
before(:each) do
@tester.unstub(:render_association)
@form_obj.stub(:semantic_fields_for).and_return('form<tagzzz>')
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 == '<a href="#" class="add_fields" data-association="person" data-associations="people" data-template="form&lt;tagzzz&gt;">add something</a>'
end
end
context "when using simple_form" do
before(:each) do
@tester.unstub(:render_association)
@form_obj.stub(:simple_fields_for).and_return('form<tagxxx>')
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 == '<a href="#" class="add_fields" data-association="person" data-associations="people" data-template="form&lt;tagxxx&gt;">add something</a>'
end
end
end

View File

@ -1,2 +1,3 @@
class Person < ActiveRecord::Base
belongs_to :post
end