Skip to content

Commit

Permalink
added :as option
Browse files Browse the repository at this point in the history
  • Loading branch information
icambron committed Apr 5, 2012
1 parent cb18454 commit befc119
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
7 changes: 4 additions & 3 deletions lib/mongoid_denormalize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ def denormalize_from
def denormalize_to
self.denormalize_definitions.each do |definition|
next unless definition[:options][:to]
assigns = Hash[*definition[:fields].collect { |name| ["#{self.class.name.underscore}_#{name}", self.send(name)] }.flatten(1)]

as = definition[:options][:as]
prefix = as ? as : self.class.name.underscore

assigns = Hash[*definition[:fields].collect { |name| ["#{prefix}_#{name}", self.send(name)] }.flatten(1)]

[definition[:options][:to]].flatten.each do |association|
relation = []
Expand All @@ -86,7 +88,6 @@ def denormalize_to

c.to_a.each do |a|
assigns.each { |assign| a.send("#{assign[0]}=", assign[1]) }

a.save
end
end
Expand Down
4 changes: 3 additions & 1 deletion spec/app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ class Comment

belongs_to :post, :inverse_of => :comments
belongs_to :user
belongs_to :moderator, :class_name => "User", :inverse_of => :moderated_comments

attr_accessible :body, :user
attr_accessible :body, :user, :moderator

denormalize :location, :type => Array, :from => :user
denormalize :name, :from => :user
denormalize :email, :from => :user
denormalize :created_at, :type => Time, :from => :post
denormalize :nickname, :from => :moderator
end
5 changes: 4 additions & 1 deletion spec/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ class User

field :name
field :email
field :nickname
field :location, :type => Array

has_one :post
has_many :comments
has_many :moderated_comments, :class_name => "Comment", :inverse_of => :moderator

denormalize :name, :email, :location, :to => [:post, :comments]
denormalize :nickname, :to => :moderated_comments, as: :moderator
end
12 changes: 11 additions & 1 deletion spec/mongoid_denormalize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)

@post = Post.create!(:title => "Blog post", :body => "Lorem ipsum...", :created_at => Time.parse("Jan 1 2010 12:00"))
@user = User.create!(:name => "John Doe", :email => "john@doe.com", :post => @post, :location => [1, 1])
@user = User.create!(:name => "John Doe", :email => "john@doe.com", :post => @post, :location => [1, 1], :nickname => "jdoe")
@comment = @post.comments.create(:body => "This is the comment", :user => @user)
@moderated_comment = @post.comments.create(:body => "This is a moderated comment", :moderator => @user)

@user.comments << @comment
@user.moderated_comments << @moderated_comment
@other_user = User.create!(:name => "Bill")
end

Expand Down Expand Up @@ -78,6 +80,14 @@

@comment.post_created_at.should eql Time.parse("Jan 1 2011 12:00")
end

it "should push to overriden field names" do
@user.nickname = "jonsey"
@user.save!

@moderated_comment.reload
@moderated_comment.moderator_nickname.should eql "jonsey"
end
end

context "rake task" do
Expand Down

0 comments on commit befc119

Please sign in to comment.