From 3f5e89361f47be81ba6a358f5cc4e3a34ab08ff7 Mon Sep 17 00:00:00 2001 From: fl00r Date: Mon, 12 Mar 2012 23:30:11 +0400 Subject: [PATCH] Added custom partial feature --- lib/cocoon/view_helpers.rb | 23 +++++++++++++++++------ spec/cocoon_spec.rb | 25 +++++++++++++++++-------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index c661a01..1d30e31 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -31,10 +31,11 @@ module Cocoon end # :nodoc: - def render_association(association, f, new_object, render_options={}) + def render_association(association, f, new_object, render_options={}, custom_partial=nil) + partial = setup_partial(custom_partial, association) 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) + render(partial, :f => builder, :dynamic => true) end end @@ -51,22 +52,24 @@ 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) 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 ||={} + 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 = create_object(f, association) - html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options)).html_safe + html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options, options[:partial])).html_safe link_to(name, '#', html_options ) end @@ -75,12 +78,20 @@ module Cocoon # creates new association object with its conditions, like # `` has_many :admin_comments, class_name: "Comment", conditions: { author: "Admin" } # will create new Comment with author "Admin" - + def create_object(f, association) assoc = f.object.class.reflect_on_association(association) conditions = assoc.conditions.flatten new_object = assoc.klass.new(*conditions) end + def setup_partial(partial, association) + if partial + partial + else + association.to_s.singularize + "_fields" + end + end + end end diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb index 40e7586..962a898 100644 --- a/spec/cocoon_spec.rb +++ b/spec/cocoon_spec.rb @@ -18,13 +18,6 @@ describe Cocoon do @tester.stub(:render_association).and_return('form') end - context "association with conditions" do - it "should create correct association" do - result = @tester.create_object(@form_obj, :admin_comments) - result.author.should == "Admin" - end - end - context "without a block" do it "should accept a name" do result = @tester.link_to_add_association('add something', @form_obj, :comments) @@ -67,7 +60,7 @@ describe Cocoon do 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'}) + @tester.should_receive(:render_association).with(:people, @form_obj, anything, {:wrapper => 'inline'}, nil) result = @tester.link_to_add_association('add something', @form_obj, :people, :render_options => {:wrapper => 'inline'}) result.to_s.should == 'add something' end @@ -140,6 +133,22 @@ describe Cocoon do result.to_s.should == "remove some long name" end end + + context "association with conditions" do + it "should create correct association" do + result = @tester.create_object(@form_obj, :admin_comments) + result.author.should == "Admin" + end + end + + context "should create proper partial" do + it "should create correct association" do + result = @tester.setup_partial(nil, :admin_comments) + result.should == "admin_comment_fields" + result = @tester.setup_partial("comment_fields", :admin_comments) + result.should == "comment_fields" + end + end end end