Skip to content

Commit

Permalink
Unset all instance variables when reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Sep 3, 2011
1 parent 80a1d90 commit afecd0c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
11 changes: 5 additions & 6 deletions lib/mongo_mapper/plugins/document.rb
Expand Up @@ -21,13 +21,12 @@ def destroyed?

def reload
if doc = collection.find_one(:_id => id)
tap do |instance|
instance.class.associations.each_value do |association|
get_proxy(association).reset
end
instance.clear_keys!
instance.attributes = doc
self.class.associations.each_value do |association|
get_proxy(association).reset
end
instance_variables.each { |ivar| instance_variable_set(ivar, nil) }
self.attributes = doc
self
else
raise DocumentNotFound, "Document match #{_id.inspect} does not exist in #{collection.name} collection"
end
Expand Down
10 changes: 0 additions & 10 deletions lib/mongo_mapper/plugins/keys.rb
Expand Up @@ -172,16 +172,6 @@ def persisted?
!new? && !destroyed?
end

def clear_keys!
keys.each_key do |key|
if respond_to?(:"#{key}=")
self.send(:"#{key}=", nil)
else
self[key] = nil
end
end
end

def attributes=(attrs)
return if attrs.blank?

Expand Down
5 changes: 5 additions & 0 deletions test/functional/test_document.rb
Expand Up @@ -245,6 +245,11 @@ def setup
@instance.destroy
assert_raises(MongoMapper::DocumentNotFound) { @instance.reload }
end

should "clear keys that were removed from the database" do
@instance.unset(:age)
@instance.reload.age.should be_nil
end
end

context "database has keys not defined in model" do
Expand Down

0 comments on commit afecd0c

Please sign in to comment.