Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #11 from jbbarth/patch-use-fresh-values-in-denorma…

…lize-from

Patch : use fresh value from database in denormalize_from
  • Loading branch information...
commit 009db72cd2597916eda96e48aaaf29760a2aee64 2 parents 11df77d + b38c508
@logandk authored
Showing with 14 additions and 3 deletions.
  1. +7 −2 lib/mongoid_denormalize.rb
  2. +7 −1 spec/mongoid_denormalize_spec.rb
View
9 lib/mongoid_denormalize.rb
@@ -56,7 +56,12 @@ def denormalize_from
self.denormalize_definitions.each do |definition|
next if definition[:options][:to]
- definition[:fields].each { |name| self.send("#{definition[:options][:from]}_#{name}=", self.send(definition[:options][:from]).try(name)) }
+ definition[:fields].each do |name|
+ field = definition[:options][:from]
+ # force reload if :from method is an association ; call it normally otherwise
+ associated = self.class.reflect_on_association(field) ? self.send(field, true) : self.send(field)
+ self.send("#{field}_#{name}=", associated.try(name))
+ end
end
end
@@ -96,4 +101,4 @@ def denormalize_to
end
end
end
-end
+end
View
8 spec/mongoid_denormalize_spec.rb
@@ -46,6 +46,12 @@
@post.user_location.should eql @user.location
end
+
+ it "should use fresh values from database where possible" do
+ @other_post = Post.create!(:title => "My first blog post")
+ @other_post.update_attribute(:user_id, @user.id)
+ @other_post.user_name.should eql @user.name
+ end
it "should update denormalized values if attribute is changed" do
@user.update_attributes(:name => "Bob Doe", :location => [4, 4])
@@ -114,4 +120,4 @@
@post.comments.last.user_name.should eql @rake_user.name
end
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.