From a581d0a3ec7369db7af7024f759fda926ed55bec Mon Sep 17 00:00:00 2001 From: Dmytrii Nagirniak Date: Mon, 24 Sep 2012 17:47:47 +1000 Subject: [PATCH 1/5] Raise more describprive error when can't reflect on association --- lib/cocoon/view_helpers.rb | 1 + spec/cocoon_spec.rb | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index 7b66e7c..2321894 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -92,6 +92,7 @@ module Cocoon def create_object(f, association) assoc = f.object.class.reflect_on_association(association) + raise "Association #{association} doesn't exist on #{f.object.class}" unless assoc if assoc.class.name == "Mongoid::Relations::Metadata" conditions = assoc.respond_to?(:conditions) ? assoc.conditions.flatten : [] assoc.klass.new(*conditions) diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb index 355d650..cc1a61a 100644 --- a/spec/cocoon_spec.rb +++ b/spec/cocoon_spec.rb @@ -191,6 +191,10 @@ describe Cocoon do result = @tester.create_object(stub(:object => Comment.new), :post) result.should be_a Post 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/ + end end context "get_partial_path" do From 3f590653665ba1c3a7fd349e64012987c633fc06 Mon Sep 17 00:00:00 2001 From: Dmytrii Nagirniak Date: Mon, 24 Sep 2012 18:08:07 +1000 Subject: [PATCH 2/5] Refactor object creating and add pending spec --- lib/cocoon/view_helpers.rb | 27 ++++++++++++++++++--------- spec/cocoon_spec.rb | 7 +++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index 2321894..59e080b 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -92,13 +92,26 @@ module Cocoon def create_object(f, association) assoc = f.object.class.reflect_on_association(association) - raise "Association #{association} doesn't exist on #{f.object.class}" unless assoc - if assoc.class.name == "Mongoid::Relations::Metadata" - conditions = assoc.respond_to?(:conditions) ? assoc.conditions.flatten : [] - assoc.klass.new(*conditions) + assoc ? create_object_on_association(f, association, assoc) : create_object_on_non_association(f, association) + end + + def get_partial_path(partial, association) + partial ? partial : association.to_s.singularize + "_fields" + end + + private + + def create_object_on_non_association(f, association) + raise "Association #{association} doesn't exist on #{f.object.class}" + end + + def create_object_on_association(f, association, instance) + if instance.class.name == "Mongoid::Relations::Metadata" + conditions = instance.respond_to?(:conditions) ? instance.conditions.flatten : [] + instance.klass.new(*conditions) else # assume ActiveRecord or compatible - if assoc.collection? + if instance.collection? f.object.send(association).build else f.object.send("build_#{association}") @@ -106,9 +119,5 @@ module Cocoon end end - def get_partial_path(partial, association) - partial ? partial : association.to_s.singularize + "_fields" - end - end end diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb index cc1a61a..57522aa 100644 --- a/spec/cocoon_spec.rb +++ b/spec/cocoon_spec.rb @@ -195,6 +195,13 @@ describe Cocoon do it "should raise error if cannot reflect on association" do expect { @tester.create_object(stub(:object => Comment.new), :not_existing) }.to raise_error /exist/ 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' + end end context "get_partial_path" do From 80f949d15aee0da4b5ee50e5667fd8af36782c3e Mon Sep 17 00:00:00 2001 From: Dmytrii Nagirniak Date: Mon, 24 Sep 2012 18:15:25 +1000 Subject: [PATCH 3/5] 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 From bee1a8ba0bfe3825956fa0fbac74d94cab971b86 Mon Sep 17 00:00:00 2001 From: Dmytrii Nagirniak Date: Mon, 24 Sep 2012 18:20:18 +1000 Subject: [PATCH 4/5] 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 From d4b997a0827511ac9a443af54a1c698cb0d09520 Mon Sep 17 00:00:00 2001 From: Dmytrii Nagirniak Date: Mon, 24 Sep 2012 19:00:19 +1000 Subject: [PATCH 5/5] Fix broken js at c69c157afffd39263811cc3503d6c668763f2fe0 --- app/assets/javascripts/cocoon.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/cocoon.js b/app/assets/javascripts/cocoon.js index 6ddc7be..dffa378 100644 --- a/app/assets/javascripts/cocoon.js +++ b/app/assets/javascripts/cocoon.js @@ -5,7 +5,7 @@ content.replace(reg_exp, with_str); } - function trigger_befofe_removal_callback(node) { + function trigger_before_removal_callback(node) { node.trigger('before-remove.cocoon'); } @@ -78,4 +78,4 @@ trigger_after_removal_callback(trigger_node); }); -})(jQuery); \ No newline at end of file +})(jQuery);