Added custom partial feature

This commit is contained in:
fl00r 2012-03-12 23:30:11 +04:00
parent 04c231570b
commit 3f5e89361f
2 changed files with 34 additions and 14 deletions

View File

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

View File

@ -18,13 +18,6 @@ describe Cocoon do
@tester.stub(:render_association).and_return('form<tag>')
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 == '<a href="#" class="add_fields" data-association="person" data-associations="people" data-template="form&lt;tag&gt;">add something</a>'
end
@ -140,6 +133,22 @@ describe Cocoon do
result.to_s.should == "<input id=\"Post__destroy\" name=\"Post[_destroy]\" type=\"hidden\" /><a href=\"#\" class=\"add_some_class remove_fields dynamic\" data-something=\"bla\">remove some long name</a>"
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