mongoid_denormalize 0.2.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +24 -8
- data/lib/mongoid_denormalize.rb +13 -6
- data/lib/railties/denormalize.rake +2 -2
- metadata +11 -16
- data/spec/app/models/comment.rb +0 -13
- data/spec/app/models/post.rb +0 -14
- data/spec/app/models/user.rb +0 -12
- data/spec/mongoid_denormalize_spec.rb +0 -103
- data/spec/spec_helper.rb +0 -18
data/README.md
CHANGED
@@ -34,11 +34,11 @@ In your model:
|
|
34
34
|
Example
|
35
35
|
-------
|
36
36
|
|
37
|
-
|
37
|
+
class User
|
38
38
|
include Mongoid::Document
|
39
39
|
include Mongoid::Denormalize
|
40
40
|
|
41
|
-
|
41
|
+
has_many :comments
|
42
42
|
|
43
43
|
field :name
|
44
44
|
field :email
|
@@ -46,11 +46,11 @@ Example
|
|
46
46
|
denormalize :name, :email, :to => :comments
|
47
47
|
end
|
48
48
|
|
49
|
-
|
49
|
+
class Comment
|
50
50
|
include Mongoid::Document
|
51
51
|
include Mongoid::Denormalize
|
52
52
|
|
53
|
-
|
53
|
+
belongs_to :user
|
54
54
|
|
55
55
|
field :body
|
56
56
|
|
@@ -87,6 +87,14 @@ the parent. When using the `:to` option, the parent will push the values to its
|
|
87
87
|
# Multiple children. Will set the user_name attribute of "self.posts" and "self.comments" with "self.name".
|
88
88
|
denormalize :name, :to => [:posts, :comments]
|
89
89
|
|
90
|
+
You must specify the type of all denormalizations when using the `:from` option, unless the denormalized type is `String`, the default.
|
91
|
+
|
92
|
+
# in User
|
93
|
+
field :location, :type => Array
|
94
|
+
denormalize :location, :to => :posts
|
95
|
+
|
96
|
+
# in Post
|
97
|
+
denormalize :location, :type => Array, :from => :user
|
90
98
|
|
91
99
|
Rake tasks
|
92
100
|
----------
|
@@ -104,12 +112,20 @@ Known issues
|
|
104
112
|
|
105
113
|
**Relational associations in combination with embedded records**
|
106
114
|
|
107
|
-
|
108
|
-
|
109
|
-
|
115
|
+
It is not recommended nor supported to use mongoid_denormalize to perform denormalization to embedded records through a relational association because
|
116
|
+
MongoDB/Mongoid do not support direct access to embedded fields via a relational association.
|
117
|
+
|
118
|
+
So, if User has_many :posts and User has_many :comments, but Comments are embedded_in :post, a user can't directly access a comment.
|
119
|
+
|
120
|
+
|
121
|
+
Contributors
|
122
|
+
-------
|
123
|
+
* hubsmoke (https://github.com/hubsmoke)
|
124
|
+
* Leo Lou (https://github.com/l4u)
|
125
|
+
* Austin Bales (https://github.com/arbales)
|
110
126
|
|
111
127
|
|
112
128
|
Credits
|
113
129
|
-------
|
114
130
|
|
115
|
-
Copyright (c) 2010 Logan Raarup, released under the MIT license.
|
131
|
+
Copyright (c) 2010 Logan Raarup, released under the MIT license.
|
data/lib/mongoid_denormalize.rb
CHANGED
@@ -33,7 +33,7 @@ module Mongoid::Denormalize
|
|
33
33
|
|
34
34
|
# Define schema
|
35
35
|
unless options[:to]
|
36
|
-
fields.each { |name| field "#{options[:from]}_#{name}", :type => options[:type] }
|
36
|
+
fields.each { |name| field "#{options[:from]}_#{name}", :type => options[:type] || String }
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
@@ -59,14 +59,21 @@ module Mongoid::Denormalize
|
|
59
59
|
def denormalize_to
|
60
60
|
self.denormalize_definitions.each do |definition|
|
61
61
|
next unless definition[:options][:to]
|
62
|
+
assigns = Hash[*definition[:fields].collect { |name| ["#{self.class.name.underscore}_#{name}", self.send(name)] }.flatten(1)]
|
62
63
|
|
63
|
-
|
64
|
-
|
64
|
+
|
65
65
|
[definition[:options][:to]].flatten.each do |association|
|
66
|
-
|
67
|
-
|
66
|
+
relation = []
|
67
|
+
reflect = self.class.reflect_on_association(association)
|
68
|
+
relation = reflect.relation.macro unless reflect.nil? || reflect.relation.nil?
|
69
|
+
|
70
|
+
if [:embedded_in, :embeds_one, :referenced_in, :references_one, :has_one, :belongs_to].include? relation
|
71
|
+
c = self.send(association)
|
72
|
+
|
73
|
+
c.update_attributes(assigns) unless c.blank?
|
68
74
|
else
|
69
|
-
self.send(association)
|
75
|
+
c = self.send(association)
|
76
|
+
c.to_a.each { |a| a.update_attributes(assigns) }
|
70
77
|
end
|
71
78
|
end
|
72
79
|
end
|
@@ -4,11 +4,11 @@ namespace :db do
|
|
4
4
|
get_denormalizable_models.each do |klass|
|
5
5
|
if klass.embedded?
|
6
6
|
reflection = klass.reflect_on_all_associations(:embedded_in).first
|
7
|
-
parent = reflection.
|
7
|
+
parent = reflection.class_name.to_s.classify.constantize
|
8
8
|
|
9
9
|
unless parent.embedded?
|
10
10
|
parent.all.each do |parent_instance|
|
11
|
-
parent_instance.send(reflection.
|
11
|
+
parent_instance.send(reflection.inverse).each(&:repair_denormalized!)
|
12
12
|
end
|
13
13
|
end
|
14
14
|
else
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 2
|
9
|
+
version: 0.2.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Logan Raarup
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-08-04 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -22,14 +22,13 @@ dependencies:
|
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- -
|
25
|
+
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
segments:
|
28
28
|
- 2
|
29
|
-
-
|
30
|
-
-
|
31
|
-
|
32
|
-
version: 2.0.0.beta9
|
29
|
+
- 1
|
30
|
+
- 9
|
31
|
+
version: 2.1.9
|
33
32
|
type: :runtime
|
34
33
|
version_requirements: *id001
|
35
34
|
description: Helper module for denormalizing association attributes in Mongoid models.
|
@@ -52,8 +51,8 @@ homepage: http://github.com/logandk/mongoid_denormalize
|
|
52
51
|
licenses: []
|
53
52
|
|
54
53
|
post_install_message:
|
55
|
-
rdoc_options:
|
56
|
-
|
54
|
+
rdoc_options: []
|
55
|
+
|
57
56
|
require_paths:
|
58
57
|
- lib
|
59
58
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -77,9 +76,5 @@ rubygems_version: 1.3.6
|
|
77
76
|
signing_key:
|
78
77
|
specification_version: 3
|
79
78
|
summary: Mongoid denormalization helper.
|
80
|
-
test_files:
|
81
|
-
|
82
|
-
- spec/app/models/post.rb
|
83
|
-
- spec/app/models/user.rb
|
84
|
-
- spec/mongoid_denormalize_spec.rb
|
85
|
-
- spec/spec_helper.rb
|
79
|
+
test_files: []
|
80
|
+
|
data/spec/app/models/comment.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
class Comment
|
2
|
-
include Mongoid::Document
|
3
|
-
include Mongoid::Denormalize
|
4
|
-
|
5
|
-
field :body
|
6
|
-
|
7
|
-
embedded_in :post, :inverse_of => :comments
|
8
|
-
referenced_in :user
|
9
|
-
|
10
|
-
denormalize :name, :from => :user
|
11
|
-
denormalize :email, :from => :user
|
12
|
-
denormalize :created_at, :type => Time, :from => :post
|
13
|
-
end
|
data/spec/app/models/post.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
class Post
|
2
|
-
include Mongoid::Document
|
3
|
-
include Mongoid::Denormalize
|
4
|
-
|
5
|
-
field :title
|
6
|
-
field :body
|
7
|
-
field :created_at, :type => Time
|
8
|
-
|
9
|
-
referenced_in :user
|
10
|
-
embeds_many :comments
|
11
|
-
|
12
|
-
denormalize :name, :email, :from => :user
|
13
|
-
denormalize :created_at, :to => :comments
|
14
|
-
end
|
data/spec/app/models/user.rb
DELETED
@@ -1,103 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Mongoid::Denormalize do
|
4
|
-
before(:all) do
|
5
|
-
Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
|
6
|
-
|
7
|
-
@post = Post.create!(:title => "Blog post", :body => "Lorem ipsum...", :created_at => Time.parse("Jan 1 2010 12:00"))
|
8
|
-
@user = User.create!(:name => "John Doe", :email => "john@doe.com", :post => @post)
|
9
|
-
@comment = @post.comments.create(:body => "This is the comment", :user => @user)
|
10
|
-
|
11
|
-
@user.comments << @comment
|
12
|
-
|
13
|
-
@other_user = User.create!(:name => "Bill")
|
14
|
-
end
|
15
|
-
|
16
|
-
context "denormalize from" do
|
17
|
-
it "should define multiple fields for association" do
|
18
|
-
@post.fields.should have_key "user_name"
|
19
|
-
@post.fields.should have_key "user_email"
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should default to string field type for associated fields" do
|
23
|
-
@post.fields["user_name"].type.should eql String
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should allow setting the field type for associated fields" do
|
27
|
-
@comment.fields["post_created_at"].type.should eql Time
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should allow multiple declarations for the same association" do
|
31
|
-
@comment.fields.should have_key "user_name"
|
32
|
-
@comment.fields.should have_key "user_email"
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should denormalize fields without specified type" do
|
36
|
-
@comment.user_name.should eql @user.name
|
37
|
-
@comment.user_email.should eql @user.email
|
38
|
-
@post.user_name.should eql @user.name
|
39
|
-
@post.user_email.should eql @user.email
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should denormalize fields with specified type" do
|
43
|
-
@comment.post_created_at.should eql @post.created_at
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should update denormalized values if attribute is changed" do
|
47
|
-
@user.update_attributes(:name => "Bob Doe")
|
48
|
-
|
49
|
-
@comment.user_name.should eql @user.name
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should update denormalized values if object is changed" do
|
53
|
-
@other_user = User.create!(:name => "Bill", :email => "bill@doe.com")
|
54
|
-
|
55
|
-
@comment.user = @other_user
|
56
|
-
@comment.save!
|
57
|
-
|
58
|
-
@comment.user_name.should eql @other_user.name
|
59
|
-
@comment.user_email.should eql @other_user.email
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
context "denormalize to" do
|
64
|
-
it "should push denormalized fields to one-to-one association" do
|
65
|
-
@user.name = "Elvis"
|
66
|
-
@user.save!
|
67
|
-
|
68
|
-
@post.user_name.should eql "Elvis"
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should push denormalized fields to one-to-many association" do
|
72
|
-
@post.created_at = Time.parse("Jan 1 2011 12:00")
|
73
|
-
@post.save!
|
74
|
-
|
75
|
-
@comment.post_created_at.should eql Time.parse("Jan 1 2011 12:00")
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
context "rake task" do
|
80
|
-
it "should correct inconsistent denormalizations on regular documents" do
|
81
|
-
Post.collection.update({ '_id' => @post.id }, { '$set' => { 'user_name' => 'Clint Eastwood' } })
|
82
|
-
|
83
|
-
Rake::Task["db:denormalize"].invoke
|
84
|
-
Rake::Task["db:denormalize"].reenable
|
85
|
-
|
86
|
-
@post.reload
|
87
|
-
@post.user_name.should eql @user.name
|
88
|
-
end
|
89
|
-
|
90
|
-
it "should correct inconsistent denormalizations on referenced embedded documents" do
|
91
|
-
@rake_user = User.create!(:name => "Johnny Depp", :email => "johnny@depp.com")
|
92
|
-
@rake_comment = @post.comments.create!(:body => "Depp's comment", :user => @rake_user)
|
93
|
-
|
94
|
-
@rake_user.update_attributes!(:name => "J. Depp")
|
95
|
-
|
96
|
-
Rake::Task["db:denormalize"].invoke
|
97
|
-
Rake::Task["db:denormalize"].reenable
|
98
|
-
|
99
|
-
@post.reload
|
100
|
-
@post.comments.last.user_name.should eql @rake_user.name
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'spec'
|
3
|
-
require 'mongoid'
|
4
|
-
require 'yaml'
|
5
|
-
require 'rake'
|
6
|
-
|
7
|
-
# Load rake tasks
|
8
|
-
load File.expand_path("../../lib/railties/denormalize.rake", __FILE__)
|
9
|
-
task :environment do
|
10
|
-
Dir.chdir(File.dirname(__FILE__))
|
11
|
-
end
|
12
|
-
|
13
|
-
Mongoid.configure do |config|
|
14
|
-
config.master = Mongo::Connection.new.db("mongoid_denormalize_development")
|
15
|
-
end
|
16
|
-
|
17
|
-
require File.expand_path("../../lib/mongoid_denormalize", __FILE__)
|
18
|
-
Dir["#{File.dirname(__FILE__)}/app/models/*.rb"].each { |f| require f }
|