Remove the extra options-hash for the partial, instead use the same html_options hash. Is less confusing, and matches the documentation (I was under the impression it was already like that --ooops).

This commit is contained in:
nathanvda 2012-04-09 12:45:59 +02:00
parent a0c2d6db53
commit 0225126532
2 changed files with 18 additions and 16 deletions

View File

@ -32,7 +32,7 @@ module Cocoon
# :nodoc: # :nodoc:
def render_association(association, f, new_object, render_options={}, custom_partial=nil) def render_association(association, f, new_object, render_options={}, custom_partial=nil)
partial = setup_partial(custom_partial, association) partial = get_partial_path(custom_partial, association)
locals = render_options.delete(:locals) || {} locals = render_options.delete(:locals) || {}
method_name = f.respond_to?(:semantic_fields_for) ? :semantic_fields_for : (f.respond_to?(:simple_fields_for) ? :simple_fields_for : :fields_for) 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| f.send(method_name, association, new_object, {:child_index => "new_#{association}"}.merge(render_options)) do |builder|
@ -57,24 +57,23 @@ module Cocoon
f = args[0] f = args[0]
association = args[1] association = args[1]
html_options = args[2] || {} html_options = args[2] || {}
options = args[3] || {} link_to_add_association(capture(&block), f, association, html_options)
link_to_add_association(capture(&block), f, association, html_options, options)
else else
name = args[0] name = args[0]
f = args[1] f = args[1]
association = args[2] association = args[2]
html_options = args[3] || {} html_options = args[3] || {}
options = args[4] || {}
render_options = html_options.delete(:render_options) render_options = html_options.delete(:render_options)
render_options ||= {} render_options ||= {}
override_partial = html_options.delete(:partial)
html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ') html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
html_options[:'data-association'] = association.to_s.singularize html_options[:'data-association'] = association.to_s.singularize
html_options[:'data-associations'] = association.to_s.pluralize html_options[:'data-associations'] = association.to_s.pluralize
new_object = create_object(f, association) new_object = create_object(f, association)
html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options, options[:partial])).html_safe html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options, override_partial)).html_safe
link_to(name, '#', html_options ) link_to(name, '#', html_options )
end end
@ -90,12 +89,8 @@ module Cocoon
new_object = assoc.klass.new(*conditions) new_object = assoc.klass.new(*conditions)
end end
def setup_partial(partial, association) def get_partial_path(partial, association)
if partial partial ? partial : association.to_s.singularize + "_fields"
partial
else
association.to_s.singularize + "_fields"
end
end end
end end

View File

@ -28,6 +28,13 @@ describe Cocoon do
result = @tester.link_to_add_association('add something', @form_obj, :comments, {:class => 'something silly'}) result = @tester.link_to_add_association('add something', @form_obj, :comments, {:class => 'something silly'})
result.to_s.should == '<a href="#" class="something silly add_fields" data-association="comment" data-associations="comments" data-template="form&lt;tag&gt;">add something</a>' result.to_s.should == '<a href="#" class="something silly add_fields" data-association="comment" data-associations="comments" data-template="form&lt;tag&gt;">add something</a>'
end end
it "allows to explicitly hand the wanted partial" do
@tester.unstub(:render_association)
@tester.should_receive(:render_association).with(anything(), anything(), anything(), anything(), "shared/partial").and_return('partiallll')
result = @tester.link_to_add_association('add something', @form_obj, :comments, :partial => "shared/partial")
result.to_s.should == '<a href="#" class="add_fields" data-association="comment" data-associations="comments" data-template="partiallll">add something</a>'
end
end end
context "with a block" do context "with a block" do
@ -45,10 +52,10 @@ describe Cocoon do
result.to_s.should == '<a href="#" class="floppy disk add_fields" data-association="comment" data-associations="comments" data-template="form&lt;tag&gt;">some long name</a>' result.to_s.should == '<a href="#" class="floppy disk add_fields" data-association="comment" data-associations="comments" data-template="form&lt;tag&gt;">some long name</a>'
end end
it "accepts options and passes them to link_to" do it "allows to explicitly hand the wanted partial" do
@tester.unstub(:render_association) @tester.unstub(:render_association)
@tester.should_receive(:render_association).with(anything(), anything(), anything(), anything(), "shared/partial").and_return('partiallll') @tester.should_receive(:render_association).with(anything(), anything(), anything(), anything(), "shared/partial").and_return('partiallll')
result = @tester.link_to_add_association( @form_obj, :comments, {:class => 'floppy disk'}, {:partial => "shared/partial"}) do result = @tester.link_to_add_association( @form_obj, :comments, :class => 'floppy disk', :partial => "shared/partial") do
"some long name" "some long name"
end end
result.to_s.should == '<a href="#" class="floppy disk add_fields" data-association="comment" data-associations="comments" data-template="partiallll">some long name</a>' result.to_s.should == '<a href="#" class="floppy disk add_fields" data-association="comment" data-associations="comments" data-template="partiallll">some long name</a>'
@ -175,13 +182,13 @@ describe Cocoon do
end end
end end
context "setup_partial" do context "get_partial_path" do
it "generates the default partial name if no partial given" do it "generates the default partial name if no partial given" do
result = @tester.setup_partial(nil, :admin_comments) result = @tester.get_partial_path(nil, :admin_comments)
result.should == "admin_comment_fields" result.should == "admin_comment_fields"
end end
it "uses the given partial name" do it "uses the given partial name" do
result = @tester.setup_partial("comment_fields", :admin_comments) result = @tester.get_partial_path("comment_fields", :admin_comments)
result.should == "comment_fields" result.should == "comment_fields"
end end
end end