Skip to content

Commit

Permalink
Allow appending of referenced relations in create blocks, post defaul…
Browse files Browse the repository at this point in the history
…t set. Fixes #1239.
  • Loading branch information
durran committed Oct 12, 2011
1 parent 5f9ed6a commit 6d7b792
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/mongoid/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ def initialize(attrs = nil, options = nil)
@attributes ||= {}
options ||= {}
process(attrs, options[:as] || :default, !options[:without_protection]) do
yield self if block_given?
identify
apply_defaults
yield(self) if block_given?
end
run_callbacks(:initialize) { self }
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/relations/referenced/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def method_missing(name, *args, &block)
#
# @since 2.1.0
def persistable?
base.persisted? && !binding? && !_building?
creating? || base.persisted? && !binding? && !_building?
end

# Deletes all related documents from the database given the supplied
Expand Down
12 changes: 12 additions & 0 deletions spec/functional/mongoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
Person.delete_all
end

describe "#initialize" do

context "when providing a block" do

it "sets the defaults before yielding" do
Person.new do |person|
person.age.should eq(100)
end
end
end
end

context "defining a BSON::ObjectId as a field" do

let(:bson_id) do
Expand Down
33 changes: 33 additions & 0 deletions spec/functional/mongoid/relations/referenced/many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,39 @@
end
end

context "when appending in a parent create block" do

let!(:post) do
Post.create(:title => "testing")
end

let!(:person) do
Person.create(:ssn => "345-11-1124") do |doc|
doc.posts << post
end
end

it "adds the documents to the relation" do
person.posts.should eq([ post ])
end

it "sets the foreign key on the inverse relation" do
post.person_id.should eq(person.id)
end

it "saves the target" do
post.should be_persisted
end

it "adds the correct number of documents" do
person.posts.size.should eq(1)
end

it "persists the link" do
person.reload.posts.should eq([ post ])
end
end

context "when the parent is not a new record" do

let(:person) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

context "when the relations are not polymorphic" do


context "when the inverse relation is not defined" do

let(:person) do
Expand All @@ -44,6 +43,43 @@
end
end

context "when appending in a parent create block" do

let!(:preference) do
Preference.create(:name => "testing")
end

let!(:person) do
Person.create(:ssn => "345-11-1123") do |doc|
doc.preferences << preference
end
end

it "adds the documents to the relation" do
person.preferences.should eq([ preference ])
end

it "sets the foreign key on the relation" do
person.preference_ids.should eq([ preference.id ])
end

it "sets the foreign key on the inverse relation" do
preference.person_ids.should eq([ person.id ])
end

it "saves the target" do
preference.should be_persisted
end

it "adds the correct number of documents" do
person.preferences.size.should eq(1)
end

it "persists the link" do
person.reload.preferences.should eq([ preference ])
end
end

context "when the parent is a new record" do

let(:person) do
Expand Down

0 comments on commit 6d7b792

Please sign in to comment.