mongoid_max_denormalize 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +76 -61
- data/lib/mongoid/max/denormalize/one_to_many.rb +2 -2
- data/lib/mongoid/max/denormalize/version.rb +1 -1
- metadata +14 -20
data/README.md
CHANGED
@@ -31,7 +31,9 @@ Or install with RubyGems:
|
|
31
31
|
|
32
32
|
Add `include Mongoid::Max::Denormalize` in your model and also:
|
33
33
|
|
34
|
-
|
34
|
+
```ruby
|
35
|
+
denormalize :relation, :field_1, :field_2 ... :field_n, :options_1 => :value, :options_2 => :value
|
36
|
+
```
|
35
37
|
|
36
38
|
|
37
39
|
### Warming up
|
@@ -49,39 +51,43 @@ Note: you can't warm up from both sides of the relation. Only the most efficient
|
|
49
51
|
|
50
52
|
#### Example:
|
51
53
|
|
52
|
-
|
53
|
-
|
54
|
+
```ruby
|
55
|
+
class Post
|
56
|
+
include Mongoid::Document
|
54
57
|
|
55
|
-
|
58
|
+
field :title
|
56
59
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
+
def slug
|
61
|
+
title.try(:parameterize)
|
62
|
+
end
|
60
63
|
|
61
|
-
|
62
|
-
|
64
|
+
has_many :comments
|
65
|
+
end
|
63
66
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
+
class Comment
|
68
|
+
include Mongoid::Document
|
69
|
+
include Mongoid::Max::Denormalize
|
67
70
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
+
belongs_to :post
|
72
|
+
denormalize :post, :title, :slug
|
73
|
+
end
|
71
74
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
75
|
+
@post = Post.create(:title => "Mush from the Wimp")
|
76
|
+
@comment = @post.comments.create
|
77
|
+
@comment.post_title #=> "Mush from the Wimp"
|
78
|
+
@comment.post_slug #=> "mush-from-the-wimp"
|
76
79
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
80
|
+
@post.update_attributes(:title => "All Must Share The Burden")
|
81
|
+
@comment.reload # to reload the comment from the DB
|
82
|
+
@comment.post_title #=> "All Must Share The Burden"
|
83
|
+
@comment.post_slug #=> "all-must-share-the-burden"
|
84
|
+
```
|
81
85
|
|
82
86
|
To warm up the denormalization for an existing collection:
|
83
87
|
|
84
|
-
|
88
|
+
```ruby
|
89
|
+
Post.denormalize_to_comments!
|
90
|
+
```
|
85
91
|
|
86
92
|
**Tips :** In your views, do not use `@comment.post` but `@comment.post_id` or `@comment.post_id?`
|
87
93
|
to avoid a query that checks/retrieve for the post. We want to avoid it, don't we ?
|
@@ -105,6 +111,7 @@ This is better :
|
|
105
111
|
</div>
|
106
112
|
|
107
113
|
|
114
|
+
|
108
115
|
### Many to One
|
109
116
|
|
110
117
|
**Supported fields:** only normal Mongoid fields, no methods *(optionnal)*
|
@@ -116,64 +123,72 @@ This is better :
|
|
116
123
|
|
117
124
|
#### Example:
|
118
125
|
|
119
|
-
|
120
|
-
|
121
|
-
|
126
|
+
```ruby
|
127
|
+
class Post
|
128
|
+
include Mongoid::Document
|
129
|
+
include Mongoid::Max::Denormalize
|
122
130
|
|
123
|
-
|
124
|
-
|
125
|
-
|
131
|
+
has_many :comments
|
132
|
+
denormalize :comments, :rating, :stuff, :count => true
|
133
|
+
end
|
126
134
|
|
127
|
-
|
128
|
-
|
135
|
+
class Comment
|
136
|
+
include Mongoid::Document
|
129
137
|
|
130
|
-
|
138
|
+
belongs_to :post
|
131
139
|
|
132
|
-
|
133
|
-
|
134
|
-
|
140
|
+
field :rating
|
141
|
+
field :stuff
|
142
|
+
end
|
135
143
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
144
|
+
@post = Post.create
|
145
|
+
@comment = @post.comments.create(:rating => 5, :stuff => "A")
|
146
|
+
@comment = @post.comments.create(:rating => 3, :stuff => "B")
|
147
|
+
@post.reload
|
148
|
+
@post.comments_count #=> 2
|
149
|
+
@post.comments_rating #=> [5, 3]
|
150
|
+
@post.comments_stuff #=> ["A", "B"]
|
151
|
+
```
|
143
152
|
|
144
153
|
To warm up the denormalization for an existing collection:
|
145
154
|
|
146
|
-
|
155
|
+
```ruby
|
156
|
+
Post.denormalize_from_comments!
|
157
|
+
```
|
147
158
|
|
148
159
|
You can see that each denormalized field in stored in a separate array. This is wanted.
|
149
160
|
An option `:group` will come to allow the way below (and maybe permit methods denormalization) :
|
150
161
|
|
151
|
-
|
162
|
+
```ruby
|
163
|
+
@post.comments_fields #=> [{:rating => 5, :stuff => "A"}, {:rating => 5, :stuff => "B"}]
|
164
|
+
```
|
152
165
|
|
153
166
|
#### Example 2: only count
|
154
167
|
|
155
|
-
|
156
|
-
|
157
|
-
|
168
|
+
```ruby
|
169
|
+
class Post
|
170
|
+
include Mongoid::Document
|
171
|
+
include Mongoid::Max::Denormalize
|
158
172
|
|
159
|
-
|
160
|
-
|
161
|
-
|
173
|
+
has_many :comments
|
174
|
+
denormalize :comments, :count => true
|
175
|
+
end
|
162
176
|
|
163
|
-
|
164
|
-
|
177
|
+
class Comment
|
178
|
+
include Mongoid::Document
|
165
179
|
|
166
|
-
|
167
|
-
|
180
|
+
belongs_to :post
|
181
|
+
end
|
168
182
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
183
|
+
@post = Post.create
|
184
|
+
@comment = @post.comments.create
|
185
|
+
@comment = @post.comments.create
|
186
|
+
@post.reload
|
187
|
+
@post.comments_count #=> 2
|
188
|
+
```
|
174
189
|
|
175
190
|
|
176
|
-
### Many to
|
191
|
+
### Many to Many
|
177
192
|
|
178
193
|
To come...
|
179
194
|
|
@@ -61,7 +61,7 @@ module Mongoid
|
|
61
61
|
to_set[:"#{relation}_\#{field}"] = send(field)
|
62
62
|
end
|
63
63
|
|
64
|
-
#{inverse_relation}.
|
64
|
+
#{inverse_relation}.update_all(to_set) unless to_set.empty?
|
65
65
|
end
|
66
66
|
EOM
|
67
67
|
|
@@ -89,7 +89,7 @@ module Mongoid
|
|
89
89
|
to_set[:"#{relation}_\#{field}"] = nil
|
90
90
|
end
|
91
91
|
|
92
|
-
#{inverse_relation}.
|
92
|
+
#{inverse_relation}.update_all(to_set) unless to_set.empty?
|
93
93
|
end
|
94
94
|
EOM
|
95
95
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_max_denormalize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
@@ -87,19 +87,19 @@ files:
|
|
87
87
|
- README.md
|
88
88
|
- LICENSE
|
89
89
|
- lib/mongoid_max_denormalize.rb
|
90
|
-
- lib/mongoid/max/denormalize
|
91
|
-
- lib/mongoid/max/denormalize/
|
90
|
+
- lib/mongoid/max/denormalize.rb
|
91
|
+
- lib/mongoid/max/denormalize/base.rb
|
92
92
|
- lib/mongoid/max/denormalize/version.rb
|
93
93
|
- lib/mongoid/max/denormalize/many_to_one.rb
|
94
|
-
- lib/mongoid/max/denormalize/
|
95
|
-
- lib/mongoid/max/denormalize.rb
|
94
|
+
- lib/mongoid/max/denormalize/config_error.rb
|
95
|
+
- lib/mongoid/max/denormalize/one_to_many.rb
|
96
96
|
- spec/lib/mongoid_max_denormalize_spec.rb
|
97
|
-
- spec/
|
98
|
-
- spec/cases/song_and_ratings_spec.rb
|
99
|
-
- spec/cases/existing_models_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
100
98
|
- spec/cases/contact_and_addresses_spec.rb
|
99
|
+
- spec/cases/song_and_ratings_spec.rb
|
101
100
|
- spec/cases/post_and_comments_spec.rb
|
102
|
-
- spec/
|
101
|
+
- spec/cases/city_and_inhabitants_spec.rb
|
102
|
+
- spec/cases/existing_models_spec.rb
|
103
103
|
homepage: http://github.com/maximeg/mongoid_max_denormalize
|
104
104
|
licenses: []
|
105
105
|
post_install_message:
|
@@ -112,18 +112,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
112
|
- - ! '>='
|
113
113
|
- !ruby/object:Gem::Version
|
114
114
|
version: '0'
|
115
|
-
segments:
|
116
|
-
- 0
|
117
|
-
hash: -1176296074452929639
|
118
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
116
|
none: false
|
120
117
|
requirements:
|
121
118
|
- - ! '>='
|
122
119
|
- !ruby/object:Gem::Version
|
123
120
|
version: '0'
|
124
|
-
segments:
|
125
|
-
- 0
|
126
|
-
hash: -1176296074452929639
|
127
121
|
requirements: []
|
128
122
|
rubyforge_project:
|
129
123
|
rubygems_version: 1.8.24
|
@@ -132,9 +126,9 @@ specification_version: 3
|
|
132
126
|
summary: MaxMapper, polyvalent ORM for Rails
|
133
127
|
test_files:
|
134
128
|
- spec/lib/mongoid_max_denormalize_spec.rb
|
135
|
-
- spec/
|
136
|
-
- spec/cases/song_and_ratings_spec.rb
|
137
|
-
- spec/cases/existing_models_spec.rb
|
129
|
+
- spec/spec_helper.rb
|
138
130
|
- spec/cases/contact_and_addresses_spec.rb
|
131
|
+
- spec/cases/song_and_ratings_spec.rb
|
139
132
|
- spec/cases/post_and_comments_spec.rb
|
140
|
-
- spec/
|
133
|
+
- spec/cases/city_and_inhabitants_spec.rb
|
134
|
+
- spec/cases/existing_models_spec.rb
|