mongoid_max_denormalize 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +17 -8
- data/lib/mongoid/max/denormalize/many_to_one.rb +0 -1
- data/lib/mongoid/max/denormalize/version.rb +1 -1
- data/spec/cases/existing_spec.rb +61 -0
- metadata +3 -1
data/README.md
CHANGED
@@ -9,6 +9,8 @@ For now, support only Mongoid 3.
|
|
9
9
|
* Propagate only when needed
|
10
10
|
* Take advantage of atomic operations on multi documents of MongoDB
|
11
11
|
|
12
|
+
*This is a pre-version not suitable for production.*
|
13
|
+
|
12
14
|
|
13
15
|
|
14
16
|
## Installation
|
@@ -29,29 +31,33 @@ Or install with RubyGems:
|
|
29
31
|
|
30
32
|
Add `include Mongoid::Max::Denormalize` in your model and also:
|
31
33
|
|
32
|
-
|
34
|
+
denormalize relation, field_1, field_2 ... field_n, options
|
33
35
|
|
34
36
|
|
35
37
|
### One to Many
|
36
38
|
|
37
|
-
Supported fields
|
39
|
+
**Supported fields:** normal Mongoid fields, and methods.
|
38
40
|
|
39
|
-
Supported options
|
41
|
+
**Supported options:** none.
|
40
42
|
|
41
43
|
Example :
|
42
44
|
|
43
45
|
class Post
|
44
46
|
include Mongoid::Document
|
47
|
+
|
45
48
|
field :title
|
49
|
+
|
46
50
|
def slug
|
47
51
|
title.try(:parameterize)
|
48
52
|
end
|
53
|
+
|
49
54
|
has_many :comments
|
50
55
|
end
|
51
56
|
|
52
57
|
class Comment
|
53
58
|
include Mongoid::Document
|
54
59
|
include Mongoid::Max::Denormalize
|
60
|
+
|
55
61
|
belons_to :post
|
56
62
|
denormalize :post, :title, :slug
|
57
63
|
end
|
@@ -60,7 +66,7 @@ Example :
|
|
60
66
|
@comment = @post.comments.create
|
61
67
|
@comment.post_title #=> "Mush from the Wimp"
|
62
68
|
@comment.post_slug #=> "mush-from-the-wimp"
|
63
|
-
|
69
|
+
|
64
70
|
@post.update_attributes(:title => "All Must Share The Burden")
|
65
71
|
@comment.reload # to reload the comment from the DB
|
66
72
|
@comment.post_title #=> "All Must Share The Burden"
|
@@ -90,9 +96,9 @@ This is better :
|
|
90
96
|
|
91
97
|
### Many to One
|
92
98
|
|
93
|
-
Supported fields
|
99
|
+
**Supported fields:** *only* normal Mongoid fields (no methods)
|
94
100
|
|
95
|
-
Supported options
|
101
|
+
**Supported options:**
|
96
102
|
|
97
103
|
* `:count => true` : to keep a count !
|
98
104
|
|
@@ -102,13 +108,16 @@ Example :
|
|
102
108
|
class Post
|
103
109
|
include Mongoid::Document
|
104
110
|
include Mongoid::Max::Denormalize
|
111
|
+
|
105
112
|
has_many :comments
|
106
113
|
denormalize :comments, :rating, :stuff, :count => true
|
107
114
|
end
|
108
115
|
|
109
116
|
class Comment
|
110
117
|
include Mongoid::Document
|
118
|
+
|
111
119
|
belons_to :post
|
120
|
+
|
112
121
|
field :rating
|
113
122
|
field :stuff
|
114
123
|
end
|
@@ -117,9 +126,9 @@ Example :
|
|
117
126
|
@comment = @post.comments.create(:rating => 5, :stuff => "A")
|
118
127
|
@comment = @post.comments.create(:rating => 3, :stuff => "B")
|
119
128
|
@post.reload
|
120
|
-
@post.comments_count
|
129
|
+
@post.comments_count #=> 2
|
121
130
|
@post.comments_rating #=> [5, 3]
|
122
|
-
@post.comments_stuff
|
131
|
+
@post.comments_stuff #=> ["A", "B"]
|
123
132
|
|
124
133
|
You can see that each denormalized field in stored in a separate array. This is wanted.
|
125
134
|
An option `:group` will come to allow :
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
#
|
5
|
+
# The case
|
6
|
+
#
|
7
|
+
class Author
|
8
|
+
include Mongoid::Document
|
9
|
+
|
10
|
+
field :name, type: String
|
11
|
+
|
12
|
+
has_many :books
|
13
|
+
end
|
14
|
+
|
15
|
+
class Book
|
16
|
+
include Mongoid::Document
|
17
|
+
include Mongoid::Max::Denormalize
|
18
|
+
|
19
|
+
field :name, type: String
|
20
|
+
|
21
|
+
belongs_to :author
|
22
|
+
|
23
|
+
has_many :reviews
|
24
|
+
end
|
25
|
+
|
26
|
+
class Review
|
27
|
+
include Mongoid::Document
|
28
|
+
include Mongoid::Max::Denormalize
|
29
|
+
|
30
|
+
belongs_to :book
|
31
|
+
|
32
|
+
field :rating, type: Integer
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# The specs
|
37
|
+
#
|
38
|
+
describe "Case: Existing" do
|
39
|
+
|
40
|
+
before do
|
41
|
+
@author = Author.create!(name: "Michael Crichton")
|
42
|
+
@book = Book.create!(name: "Jurassic Park", author: @author)
|
43
|
+
@review_1 = Review.create!(book: @book, rating: 4)
|
44
|
+
@review_2 = Review.create!(book: @book, rating: 5)
|
45
|
+
end
|
46
|
+
|
47
|
+
context "when defining the denormalization" do
|
48
|
+
before do
|
49
|
+
Book.denormalize :author, :name
|
50
|
+
Book.denormalize :reviews, :rating, :count => true
|
51
|
+
Review.denormalize :book, :name
|
52
|
+
end
|
53
|
+
|
54
|
+
it "denormalization should be empty" do
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
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.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -94,6 +94,7 @@ files:
|
|
94
94
|
- lib/mongoid/max/denormalize/base.rb
|
95
95
|
- lib/mongoid/max/denormalize.rb
|
96
96
|
- spec/lib/mongoid_max_denormalize_spec.rb
|
97
|
+
- spec/cases/existing_spec.rb
|
97
98
|
- spec/cases/contact_and_addresses_spec.rb
|
98
99
|
- spec/cases/post_and_comments_spec.rb
|
99
100
|
- spec/cases/song_and_notes_spec.rb
|
@@ -124,6 +125,7 @@ specification_version: 3
|
|
124
125
|
summary: MaxMapper, polyvalent ORM for Rails
|
125
126
|
test_files:
|
126
127
|
- spec/lib/mongoid_max_denormalize_spec.rb
|
128
|
+
- spec/cases/existing_spec.rb
|
127
129
|
- spec/cases/contact_and_addresses_spec.rb
|
128
130
|
- spec/cases/post_and_comments_spec.rb
|
129
131
|
- spec/cases/song_and_notes_spec.rb
|