Skip to content
Browse files

Merge pull request #17 from dusty/master

Only mark an attribute dirty if it changes
  • Loading branch information...
2 parents 4307f5d + d23ee1a commit ff170bdb315d3d296d9e439e593b40f0d5365381 Carlos Paramio committed Sep 2, 2011
View
3 .gitignore
@@ -1,4 +1,5 @@
doc
pkg
.rvmrc
-.bundle
+.bundle
+vendor/
View
12 lib/mongo_odm/document/attribute_methods/dirty.rb
@@ -16,27 +16,29 @@ module Dirty
alias_method_chain :save, :dirty
alias_method_chain :reload, :dirty
end
-
+
module InstanceMethods
def initialize_with_dirty(*args)
initialize_without_dirty(*args)
changed_attributes.clear
self
end
-
+
def write_attribute_with_dirty(attr_name, value)
- send("#{attr_name}_will_change!") if respond_to?("#{attr_name}_will_change!")
+ if respond_to?("#{attr_name}_will_change!") && (read_attribute(attr_name) != value)
+ send("#{attr_name}_will_change!")
+ end
write_attribute_without_dirty(attr_name, value)
end
-
+
def save_with_dirty(*args)
if status = save_without_dirty(*args)
@previously_changed = changes
changed_attributes.clear
end
status
end
-
+
def reload_with_dirty(*args)
reload_without_dirty(*args).tap do
@previously_changed = nil
View
20 spec/mongo_odm/document/attribute_methods/dirty_spec.rb
@@ -2,5 +2,23 @@
require "spec_helper"
describe MongoODM::Document::AttributeMethods::Dirty do
-
+
+ class TestDocument < Shape
+ validates_presence_of :color
+ end
+
+ it "marks as dirty when changed" do
+ doc = TestDocument.new :color => 'blue'
+ doc.color_changed?.should == false
+ doc.color = 'red'
+ doc.color_changed?.should == true
+ end
+
+ it "does not mark as dirty when the same" do
+ doc = TestDocument.new :color => 'blue'
+ doc.color_changed?.should == false
+ doc.color = 'blue'
+ doc.color_changed?.should == false
+ end
+
end

0 comments on commit ff170bd

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