Skip to content

Commit

Permalink
Merge branch '0.9'
Browse files Browse the repository at this point in the history
* 0.9:
  Simply dirty tracking by comparing casted values before and after the change
  Avoid casting blank strings to floats
  added test: changed? should be false if the same ObjectId was assigned in String format
  • Loading branch information
bkeepers committed Oct 11, 2011
2 parents 560a21a + 320f27a commit cbed744
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/mongo_mapper/extensions/float.rb
Expand Up @@ -3,7 +3,7 @@ module MongoMapper
module Extensions
module Float
def to_mongo(value)
value.nil? ? nil : value.to_f
value.blank? ? nil : value.to_f
end
end
end
Expand Down
17 changes: 6 additions & 11 deletions lib/mongo_mapper/plugins/dirty.rb
Expand Up @@ -47,19 +47,14 @@ def clear_changes

def write_key(key, value)
key = key.to_s
old = read_key(key)
attribute_will_change!(key) if attribute_should_change?(key, old, value)
changed_attributes.delete(key) unless attribute_value_changed?(key, attribute_was(key), value)
super(key, value)
end

def attribute_should_change?(key, old, value)
attribute_changed?(key) == false && attribute_value_changed?(key, old, value)
attribute_will_change!(key) unless attribute_changed?(key)
super(key, value).tap do
changed_attributes.delete(key) unless attribute_value_changed?(key)
end
end

def attribute_value_changed?(key_name, old, value)
value = nil if keys[key_name.to_s].number? && value.blank?
old != value
def attribute_value_changed?(key_name)
attribute_was(key_name) != read_key(key_name)
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/functional/test_dirty.rb
Expand Up @@ -106,6 +106,15 @@ def setup
doc = @document.new
doc.value_changed?.should be_false
end

should "be false if the same ObjectId was assigned in String format" do
@document.key :doc_id, ObjectId

doc = @document.create!(:doc_id => BSON::ObjectId.new)
doc.changed?.should be_false
doc.doc_id = doc.doc_id.to_s
doc.changed?.should be_false
end
end

context "changes" do
Expand Down
4 changes: 4 additions & 0 deletions test/unit/test_extensions.rb
Expand Up @@ -137,6 +137,10 @@ class SupportTest < Test::Unit::TestCase
should "leave nil values nil" do
Float.to_mongo(nil).should == nil
end

should "leave blank values nil" do
Float.to_mongo('').should == nil
end
end

context "Hash.from_mongo" do
Expand Down

0 comments on commit cbed744

Please sign in to comment.