From 80f949d15aee0da4b5ee50e5667fd8af36782c3e Mon Sep 17 00:00:00 2001 From: Dmytrii Nagirniak Date: Mon, 24 Sep 2012 18:15:25 +1000 Subject: [PATCH] Allow to create association from build_xyz method on an object --- lib/cocoon/view_helpers.rb | 2 ++ spec/cocoon_spec.rb | 5 ++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index 59e080b..ceff81b 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -102,6 +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) raise "Association #{association} doesn't exist on #{f.object.class}" end diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb index 57522aa..192a485 100644 --- a/spec/cocoon_spec.rb +++ b/spec/cocoon_spec.rb @@ -193,14 +193,13 @@ describe Cocoon do end it "should raise error if cannot reflect on association" do - expect { @tester.create_object(stub(:object => Comment.new), :not_existing) }.to raise_error /exist/ + expect { @tester.create_object(stub(:object => Comment.new), :not_existing) }.to raise_error /association/i end it "should create an association if object responds to 'build_association' as singular" do - pending 'WIP' object = Comment.new object.should_receive(:build_custom_item).and_return 'custom' - @tester.create_object(stub(:object => Comment.new), :custom_item).should == 'custom' + @tester.create_object(stub(:object => object), :custom_item).should == 'custom' end end