Skip to content
Browse files

Added support for after_initialize callbacks when instantiating models

  • Loading branch information...
1 parent ae37933 commit 752c10c0ee4f21d615d09e5b1a79ea6bf4cde522 @tanordheim tanordheim committed with durran Mar 3, 2011
Showing with 22 additions and 0 deletions.
  1. +1 −0 lib/mongoid/document.rb
  2. +21 −0 spec/unit/mongoid/document_spec.rb
View
1 lib/mongoid/document.rb
@@ -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)
View
21 spec/unit/mongoid/document_spec.rb
@@ -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

0 comments on commit 752c10c

Please sign in to comment.
Something went wrong with that request. Please try again.