From bee1a8ba0bfe3825956fa0fbac74d94cab971b86 Mon Sep 17 00:00:00 2001 From: Dmytrii Nagirniak Date: Mon, 24 Sep 2012 18:20:18 +1000 Subject: [PATCH] Allow to build plural transient associations --- lib/cocoon/view_helpers.rb | 4 ++-- spec/cocoon_spec.rb | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index ceff81b..f83a207 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -102,8 +102,8 @@ module Cocoon private def create_object_on_non_association(f, association) - builder_method = "build_#{association}" - return f.object.send(builder_method) if f.object.respond_to?(builder_method) + 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 builder_method raise "Association #{association} doesn't exist on #{f.object.class}" end diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb index 192a485..ec46dca 100644 --- a/spec/cocoon_spec.rb +++ b/spec/cocoon_spec.rb @@ -201,6 +201,12 @@ describe Cocoon do object.should_receive(:build_custom_item).and_return 'custom' @tester.create_object(stub(:object => object), :custom_item).should == 'custom' 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 context "get_partial_path" do