diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index 6d3da1c..f01b6ba 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -32,7 +32,7 @@ module Cocoon # :nodoc: 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) || {} 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| @@ -57,24 +57,23 @@ module Cocoon f = args[0] association = args[1] html_options = args[2] || {} - options = args[3] || {} - link_to_add_association(capture(&block), f, association, html_options, options) + link_to_add_association(capture(&block), f, association, html_options) else name = args[0] f = args[1] association = args[2] html_options = args[3] || {} - options = args[4] || {} render_options = html_options.delete(:render_options) render_options ||= {} + override_partial = html_options.delete(:partial) 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 = 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 ) end @@ -90,12 +89,8 @@ module Cocoon new_object = assoc.klass.new(*conditions) end - def setup_partial(partial, association) - if partial - partial - else - association.to_s.singularize + "_fields" - end + def get_partial_path(partial, association) + partial ? partial : association.to_s.singularize + "_fields" end end diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb index bf2a2e1..865f68f 100644 --- a/spec/cocoon_spec.rb +++ b/spec/cocoon_spec.rb @@ -28,6 +28,13 @@ describe Cocoon do result = @tester.link_to_add_association('add something', @form_obj, :comments, {:class => 'something silly'}) result.to_s.should == 'add something' 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 == 'add something' + end end context "with a block" do @@ -45,10 +52,10 @@ describe Cocoon do result.to_s.should == 'some long name' 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.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" end result.to_s.should == 'some long name' @@ -175,13 +182,13 @@ describe Cocoon do end end - context "setup_partial" do + context "get_partial_path" 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" end 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" end end