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); diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index 7b66e7c..f83a207 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -92,12 +92,28 @@ module Cocoon def create_object(f, association) assoc = f.object.class.reflect_on_association(association) - 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) + 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 + + 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}") @@ -105,9 +121,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 355d650..ec46dca 100644 --- a/spec/cocoon_spec.rb +++ b/spec/cocoon_spec.rb @@ -191,6 +191,22 @@ 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 /association/i + end + + it "should create an association if object responds to 'build_association' as singular" do + object = Comment.new + 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