Allow to build plural transient associations

This commit is contained in:
Dmytrii Nagirniak 2012-09-24 18:20:18 +10:00
parent 80f949d15a
commit bee1a8ba0b
2 changed files with 8 additions and 2 deletions

View File

@ -102,8 +102,8 @@ module Cocoon
private private
def create_object_on_non_association(f, association) def create_object_on_non_association(f, association)
builder_method = "build_#{association}" builder_method = %W{build_#{association} build_#{association.to_s.singularize}}.select { |m| f.object.respond_to?(m) }.first
return f.object.send(builder_method) if f.object.respond_to?(builder_method) return f.object.send(builder_method) if builder_method
raise "Association #{association} doesn't exist on #{f.object.class}" raise "Association #{association} doesn't exist on #{f.object.class}"
end end

View File

@ -201,6 +201,12 @@ describe Cocoon do
object.should_receive(:build_custom_item).and_return 'custom' object.should_receive(:build_custom_item).and_return 'custom'
@tester.create_object(stub(:object => object), :custom_item).should == 'custom' @tester.create_object(stub(:object => object), :custom_item).should == 'custom'
end end
it "should create an association if object responds to 'build_association' as plural" do
object = Comment.new
object.should_receive(:build_custom_item).and_return 'custom'
@tester.create_object(stub(:object => object), :custom_items).should == 'custom'
end
end end
context "get_partial_path" do context "get_partial_path" do