Merge pull request #90 from dnagir/create-object-option
Allow to use with Arrays and non-associations
This commit is contained in:
commit
64fd7e718d
|
@ -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);
|
||||
})(jQuery);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue