Skip to content

Commit

Permalink
Added support for after_initialize callbacks when instantiating models
Browse files Browse the repository at this point in the history
  • Loading branch information
tanordheim authored and durran committed Mar 4, 2011
1 parent ae37933 commit 752c10c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/mongoid/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def instantiate(attrs = nil)
doc.instance_variable_set(:@attributes, attributes)
doc.send(:apply_default_attributes)
doc.setup_modifications
doc.run_callbacks(:initialize) { doc }
end
else
new(attrs)
Expand Down
21 changes: 21 additions & 0 deletions spec/unit/mongoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,27 @@
person.game.name.should == "Ms. Pacman"
end
end

context "when instantiating model" do

let(:person) do
Person.instantiate("_id" => BSON::ObjectId.new, "title" => "Sir")
end

before do
Person.set_callback :initialize, :after do |doc|
doc.title = "Madam"
end
end

after do
Person.reset_callbacks(:initialize)
end

it "runs the callbacks" do
person.title.should == "Madam"
end
end
end

context "when defaults are defined" do
Expand Down

0 comments on commit 752c10c

Please sign in to comment.