Permalink
Browse files

Skip dirty checks and assignments while loading from the DB; improves…

… load times
  • Loading branch information...
1 parent eb232a9 commit 808cd4edfa9381475c720cb1d6bce6d897625ae3 @cheald cheald committed Nov 25, 2012
Showing with 21 additions and 5 deletions.
  1. +13 −5 lib/mongo_mapper/plugins/dirty.rb
  2. +8 −0 test/functional/test_dirty.rb
@@ -12,7 +12,11 @@ def initialize(*)
end
def initialize_from_database(*)
- super.tap { changed_attributes.clear }
+ @initializing_from_database = true
+ super.tap {
+ changed_attributes.clear
+ @initializing_from_database = false
+ }
end
def save(*)
@@ -43,10 +47,14 @@ def attribute_method?(attr_name)
private
def write_key(key, value)
- key = key.to_s
- attribute_will_change!(key) unless attribute_changed?(key)
- super(key, value).tap do
- changed_attributes.delete(key) unless attribute_value_changed?(key)
+ if @initializing_from_database
+ super
+ else
+ key = key.to_s
+ attribute_will_change!(key) unless attribute_changed?(key)
+ super.tap do
+ changed_attributes.delete(key) unless attribute_value_changed?(key)
+ end
end
end
View
@@ -45,6 +45,14 @@ def setup
should "not happen when loading from database" do
doc = @document.create(:phrase => 'Foo')
+ @document.any_instance.expects(:attribute_will_change!).never
+ @document.any_instance.expects(:attribute_changed?).never
+ doc = @document.find(doc.id)
+ doc.changed?.should be_false
+ end
+
+ should "not happen when reloading from database" do
+ doc = @document.create(:phrase => 'Foo')
doc = @document.find(doc.id)
doc.changed?.should be_false

0 comments on commit 808cd4e

Please sign in to comment.