mongoid_max_denormalize 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,10 +1,6 @@
1
1
  # Mongoid::Max::Denormalize
2
2
 
3
- `Mongoid::Max::Denormalize` is a denormalization extension for Mongoid.
4
-
5
- It was designed for a minimum number of queries to the database.
6
-
7
- For now, support only Mongoid 3.
3
+ `Mongoid::Max::Denormalize` is a denormalization extension for Mongoid. It was designed to do a minimum number of queries to the database.
8
4
 
9
5
  * Denormalize fields
10
6
  * Denormalize methods (only in One to Many situations for now)
@@ -14,9 +10,8 @@ For now, support only Mongoid 3.
14
10
  * for methods: always (we can't know in an inexpensive way what is the old value to figure out if there is a change)
15
11
  * Take advantage of atomic operations on multiple documents of MongoDB
16
12
 
17
- *This is a pre-version not suitable for production.*
18
-
19
-
13
+ **Versions supported:** Mongoid 3.0 and Mongoid 2.4
14
+ [![Continuous Integration status](https://secure.travis-ci.org/maximeg/mongoid_max_denormalize.png)](http://travis-ci.org/maximeg/mongoid_max_denormalize)
20
15
 
21
16
  ## Installation
22
17
 
@@ -52,7 +47,7 @@ Note: you can't warm up from both sides of the relation. Only the most efficient
52
47
 
53
48
  **Supported options:** none.
54
49
 
55
- Example :
50
+ #### Example:
56
51
 
57
52
  class Post
58
53
  include Mongoid::Document
@@ -112,14 +107,14 @@ This is better :
112
107
 
113
108
  ### Many to One
114
109
 
115
- **Supported fields:** *only* normal Mongoid fields (no methods)
110
+ **Supported fields:** only normal Mongoid fields, no methods *(optionnal)*
116
111
 
117
112
  **Supported options:**
118
113
 
119
114
  * `:count => true` : to keep a count !
120
115
 
121
116
 
122
- Example :
117
+ #### Example:
123
118
 
124
119
  class Post
125
120
  include Mongoid::Document
@@ -138,7 +133,7 @@ Example :
138
133
  field :stuff
139
134
  end
140
135
 
141
- @post = Post.create(:title => "J'accuse !")
136
+ @post = Post.create
142
137
  @comment = @post.comments.create(:rating => 5, :stuff => "A")
143
138
  @comment = @post.comments.create(:rating => 3, :stuff => "B")
144
139
  @post.reload
@@ -155,6 +150,28 @@ An option `:group` will come to allow the way below (and maybe permit methods de
155
150
 
156
151
  @post.comments_fields #=> [{:rating => 5, :stuff => "A"}, {:rating => 5, :stuff => "B"}]
157
152
 
153
+ #### Example 2: only count
154
+
155
+ class Post
156
+ include Mongoid::Document
157
+ include Mongoid::Max::Denormalize
158
+
159
+ has_many :comments
160
+ denormalize :comments, :count => true
161
+ end
162
+
163
+ class Comment
164
+ include Mongoid::Document
165
+
166
+ belongs_to :post
167
+ end
168
+
169
+ @post = Post.create
170
+ @comment = @post.comments.create
171
+ @comment = @post.comments.create
172
+ @post.reload
173
+ @post.comments_count #=> 2
174
+
158
175
 
159
176
  ### Many to One
160
177
 
@@ -43,13 +43,13 @@ module Mongoid
43
43
  end
44
44
 
45
45
  def relation
46
- @meta.name
46
+ meta.name
47
47
  end
48
48
  def inverse_relation
49
- @meta.inverse
49
+ meta.inverse
50
50
  end
51
51
  def inverse_klass
52
- @meta.klass
52
+ meta.klass
53
53
  end
54
54
 
55
55
 
@@ -14,6 +14,9 @@ module Mongoid
14
14
  end
15
15
 
16
16
  def attach
17
+ #
18
+ # This side of the relation
19
+ #
17
20
  fields_only.each do |field|
18
21
  klass.field "#{relation}_#{field}", type: Array, default: []
19
22
  end
@@ -22,9 +25,9 @@ module Mongoid
22
25
  klass.field "#{relation}_count", type: Integer, default: 0
23
26
  end
24
27
 
25
- klass.class_eval <<-EOM, __FILE__, __LINE__
26
- before_create :denormalize_from_#{relation}
28
+ klass.before_create :"denormalize_from_#{relation}"
27
29
 
30
+ klass.class_eval <<-EOM, __FILE__, __LINE__
28
31
  def denormalize_from_#{relation}(force=false)
29
32
  #{relation}_retrieved = nil
30
33
 
@@ -44,9 +47,11 @@ module Mongoid
44
47
 
45
48
  true
46
49
  end
50
+ EOM
47
51
 
52
+ klass.class_eval <<-EOM, __FILE__, __LINE__
48
53
  def self.denormalize_from_#{relation}!
49
- each do |obj|
54
+ all.entries.each do |obj|
50
55
  obj.denormalize_from_#{relation}(true)
51
56
  obj.save!
52
57
  end
@@ -55,9 +60,12 @@ module Mongoid
55
60
  end
56
61
  EOM
57
62
 
58
- meta.klass.class_eval <<-EOM, __FILE__, __LINE__
59
- around_save :denormalize_to_#{inverse_relation}
63
+ #
64
+ # Other side of the relation
65
+ #
66
+ inverse_klass.around_save :"denormalize_to_#{inverse_relation}"
60
67
 
68
+ inverse_klass.class_eval <<-EOM, __FILE__, __LINE__
61
69
  def denormalize_to_#{inverse_relation}
62
70
  if !changed? && !new_record?
63
71
  yield if block_given?
@@ -143,9 +151,11 @@ module Mongoid
143
151
  to_update["$inc"][:#{relation}_count] = -1 if #{has_count?} && (was_removed)
144
152
 
145
153
  to_update.reject! {|k,v| v.empty?}
146
- #{klass}.collection.find(:_id => remote_id).update_all(to_update) unless to_update.empty?
154
+ #{klass}.denormalize_update_all({:_id => remote_id}, to_update) unless to_update.empty?
147
155
  end
156
+ EOM
148
157
 
158
+ inverse_klass.class_eval <<-EOM, __FILE__, __LINE__
149
159
  def denormalize_to_#{inverse_relation}_old
150
160
  fields = [#{Base.array_code_for(fields_only)}]
151
161
 
@@ -180,13 +190,14 @@ module Mongoid
180
190
  to_update["$inc"][:#{relation}_count] = -1 if #{has_count?}
181
191
 
182
192
  to_update.reject! {|k,v| v.empty?}
183
- #{klass}.collection.find(:_id => remote_id).update_all(to_update) unless to_update.empty?
193
+ #{klass}.denormalize_update_all({:_id => remote_id}, to_update) unless to_update.empty?
184
194
  end
195
+ EOM
185
196
 
186
197
 
198
+ inverse_klass.around_destroy :"denormalize_to_#{inverse_relation}_destroy"
187
199
 
188
- around_destroy :denormalize_to_#{inverse_relation}_destroy
189
-
200
+ inverse_klass.class_eval <<-EOM, __FILE__, __LINE__
190
201
  def denormalize_to_#{inverse_relation}_destroy
191
202
  fields = [#{Base.array_code_for(fields)}]
192
203
 
@@ -220,7 +231,7 @@ module Mongoid
220
231
  to_update["$inc"][:#{relation}_count] = -1 if #{has_count?}
221
232
 
222
233
  to_update.reject! {|k,v| v.empty?}
223
- #{klass}.collection.find(:_id => remote_id).update_all(to_update) unless to_update.empty?
234
+ #{klass}.denormalize_update_all({:_id => remote_id}, to_update) unless to_update.empty?
224
235
  end
225
236
  EOM
226
237
  end
@@ -6,14 +6,17 @@ module Mongoid
6
6
  class OneToMany < Base
7
7
 
8
8
  def attach
9
+ #
10
+ # This side of the relation
11
+ #
9
12
  fields.each do |field|
10
- field_meta = meta.klass.fields[field.to_s]
13
+ field_meta = inverse_klass.fields[field.to_s]
11
14
  klass.field "#{relation}_#{field}", type: field_meta.try(:type)
12
15
  end
13
16
 
14
- klass.class_eval <<-EOM, __FILE__, __LINE__
15
- before_save :denormalize_from_#{relation}
17
+ klass.before_save :"denormalize_from_#{relation}"
16
18
 
19
+ klass.class_eval <<-EOM, __FILE__, __LINE__
17
20
  def denormalize_from_#{relation}
18
21
  return true unless #{meta.key}_changed?
19
22
 
@@ -32,9 +35,12 @@ module Mongoid
32
35
  end
33
36
  EOM
34
37
 
35
- meta.klass.class_eval <<-EOM, __FILE__, __LINE__
36
- around_save :denormalize_to_#{inverse_relation}
38
+ #
39
+ # Other side of the relation
40
+ #
41
+ inverse_klass.around_save :"denormalize_to_#{inverse_relation}"
37
42
 
43
+ inverse_klass.class_eval <<-EOM, __FILE__, __LINE__
38
44
  def denormalize_to_#{inverse_relation}(force = false)
39
45
  unless changed? || force
40
46
  yield if block_given?
@@ -57,17 +63,22 @@ module Mongoid
57
63
 
58
64
  #{inverse_relation}.update to_set
59
65
  end
66
+ EOM
60
67
 
68
+ inverse_klass.class_eval <<-EOM, __FILE__, __LINE__
61
69
  def self.denormalize_to_#{inverse_relation}!
62
- each do |obj|
70
+ all.entries.each do |obj|
63
71
  obj.denormalize_to_#{inverse_relation}(true)
64
72
  end
65
73
 
66
74
  nil
67
75
  end
76
+ EOM
77
+
68
78
 
69
- around_destroy :denormalize_to_#{inverse_relation}_destroy
79
+ inverse_klass.around_destroy :"denormalize_to_#{inverse_relation}_destroy"
70
80
 
81
+ inverse_klass.class_eval <<-EOM, __FILE__, __LINE__
71
82
  def denormalize_to_#{inverse_relation}_destroy
72
83
  fields = [#{Base.array_code_for(fields)}]
73
84
 
@@ -4,7 +4,7 @@ module Mongoid
4
4
  module Denormalize
5
5
  module Version
6
6
 
7
- STRING = '0.0.5'
7
+ STRING = '0.0.6'
8
8
 
9
9
  end
10
10
  end
@@ -38,6 +38,21 @@ module Mongoid
38
38
 
39
39
  end
40
40
 
41
+ def mongoid3
42
+ Mongoid::VERSION =~ /\A3\./
43
+ end
44
+ def mongoid2
45
+ Mongoid::VERSION =~ /\A2\./
46
+ end
47
+
48
+ def denormalize_update_all(conditions, updates)
49
+ if mongoid3
50
+ self.collection.find(conditions).update_all(updates)
51
+ else
52
+ self.collection.update(conditions, updates, :multi => true)
53
+ end
54
+ end
55
+
41
56
  end
42
57
 
43
58
  end
@@ -9,3 +9,5 @@ require 'mongoid/max/denormalize/base'
9
9
  require 'mongoid/max/denormalize/one_to_many'
10
10
  require 'mongoid/max/denormalize/many_to_one'
11
11
 
12
+ require 'mongoid/version'
13
+
@@ -36,16 +36,17 @@ describe "Case: a city and his inhabitants" do
36
36
  context "considering the city" do
37
37
  subject { @city }
38
38
 
39
- it "should not have inhabitants" do
39
+ it "count should be 0" do
40
40
  @city.inhabitants.should be_empty
41
41
  @city.inhabitants_count.should eq 0
42
42
  end
43
43
  end
44
44
  end
45
45
 
46
- context "when adding 20 inhabitants" do
46
+ inhabitants_number = 5
47
+ context "when adding #{inhabitants_number} inhabitants" do
47
48
  before do
48
- 5.times do
49
+ inhabitants_number.times do
49
50
  @city.inhabitants.create!
50
51
  end
51
52
  @city.reload
@@ -54,8 +55,10 @@ describe "Case: a city and his inhabitants" do
54
55
  context "considering the city" do
55
56
  subject { @city }
56
57
 
57
- its(:inhabitants) { should have(5).inhabitants }
58
- its(:inhabitants_count) { should eq 5 }
58
+ it "count should be #{inhabitants_number}" do
59
+ @city.inhabitants.should have(inhabitants_number).inhabitants
60
+ @city.inhabitants_count.should eq inhabitants_number
61
+ end
59
62
 
60
63
  context "when destroying 2 inhabitants" do
61
64
  before do
@@ -65,8 +68,10 @@ describe "Case: a city and his inhabitants" do
65
68
  @city.reload
66
69
  end
67
70
 
68
- its(:inhabitants) { should have(3).inhabitants }
69
- its(:inhabitants_count) { should eq 3 }
71
+ it "count should be #{inhabitants_number - 2}" do
72
+ @city.inhabitants.should have(inhabitants_number - 2).inhabitants
73
+ @city.inhabitants_count.should eq(inhabitants_number - 2)
74
+ end
70
75
  end
71
76
 
72
77
  context "when destroying all inhabitants" do
@@ -75,8 +80,10 @@ describe "Case: a city and his inhabitants" do
75
80
  @city.reload
76
81
  end
77
82
 
78
- its(:inhabitants) { should have(0).inhabitant }
79
- its(:inhabitants_count) { should eq 0 }
83
+ it "count should be 0" do
84
+ @city.inhabitants.should be_empty
85
+ @city.inhabitants_count.should eq 0
86
+ end
80
87
  end
81
88
  end
82
89
  end
@@ -57,12 +57,10 @@ describe "Case: a post and his comments" do
57
57
  its(:comments) { should have(comments_number).comment }
58
58
  end
59
59
 
60
- (0..comments_number-1).each do |i|
61
- context "considering the comment #{i}" do
62
- subject { @comments[i] }
63
-
64
- its(:post_title) { should eq @post.title }
65
- its(:post_slug) { should eq @post.slug }
60
+ it "denormalized fields should be set" do
61
+ @comments.each do |comment|
62
+ comment.post_title.should eq @post.title
63
+ comment.post_slug.should eq @post.slug
66
64
  end
67
65
  end
68
66
 
@@ -73,12 +71,10 @@ describe "Case: a post and his comments" do
73
71
  @comments.each(&:reload)
74
72
  end
75
73
 
76
- (0..comments_number-1).each do |i|
77
- context "considering the comment #{i}" do
78
- subject { @comments[i] }
79
-
80
- its(:post_title) { should eq @post.title }
81
- its(:post_slug) { should eq @post.slug }
74
+ it "denormalized fields should be set" do
75
+ @comments.each do |comment|
76
+ comment.post_title.should eq @post.title
77
+ comment.post_slug.should eq @post.slug
82
78
  end
83
79
  end
84
80
  end
@@ -89,12 +85,10 @@ describe "Case: a post and his comments" do
89
85
  @comments.each(&:reload)
90
86
  end
91
87
 
92
- (0..comments_number-1).each do |i|
93
- context "considering the comment #{i}" do
94
- subject { @comments[i] }
95
-
96
- its(:post_title) { should be_nil }
97
- its(:post_slug) { should be_nil }
88
+ it "denormalized fields should be set" do
89
+ @comments.each do |comment|
90
+ comment.post_title.should be_nil
91
+ comment.post_slug.should be_nil
98
92
  end
99
93
  end
100
94
  end
@@ -106,10 +100,10 @@ describe "Case: a post and his comments" do
106
100
  @post.reload
107
101
  end
108
102
 
109
- subject { @comment_only }
110
-
111
- its(:post_title) { should be_nil }
112
- its(:post_slug) { should be_nil }
103
+ it "denormalized fields should be nil" do
104
+ @comment_only.post_title.should be_nil
105
+ @comment_only.post_slug.should be_nil
106
+ end
113
107
 
114
108
  context "when associating it" do
115
109
  before do
@@ -117,8 +111,10 @@ describe "Case: a post and his comments" do
117
111
  @comment_only.save!
118
112
  end
119
113
 
120
- its(:post_title) { should eq @post.title }
121
- its(:post_slug) { should eq @post.slug }
114
+ it "denormalized fields should be set" do
115
+ @comment_only.post_title.should eq @post.title
116
+ @comment_only.post_slug.should eq @post.slug
117
+ end
122
118
  end
123
119
 
124
120
  context "when associating it (2nd way)" do
@@ -127,8 +123,10 @@ describe "Case: a post and his comments" do
127
123
  @comment_only.reload
128
124
  end
129
125
 
130
- its(:post_title) { should eq @post.title }
131
- its(:post_slug) { should eq @post.slug }
126
+ it "denormalized fields should be set" do
127
+ @comment_only.post_title.should eq @post.title
128
+ @comment_only.post_slug.should eq @post.slug
129
+ end
132
130
  end
133
131
  end
134
132
 
@@ -137,10 +135,10 @@ describe "Case: a post and his comments" do
137
135
  @comment = @post.comments.create!
138
136
  end
139
137
 
140
- subject { @comment }
141
-
142
- its(:post_title) { should eq @post.title }
143
- its(:post_slug) { should eq @post.slug }
138
+ it "denormalized fields should be set" do
139
+ @comment.post_title.should eq @post.title
140
+ @comment.post_slug.should eq @post.slug
141
+ end
144
142
 
145
143
  context "when associating the comment to another post" do
146
144
  before do
@@ -149,8 +147,10 @@ describe "Case: a post and his comments" do
149
147
  @comment.save
150
148
  end
151
149
 
152
- its(:post_title) { should eq @other_post.title }
153
- its(:post_slug) { should eq @other_post.slug }
150
+ it "denormalized fields should be set" do
151
+ @comment.post_title.should eq @other_post.title
152
+ @comment.post_slug.should eq @other_post.slug
153
+ end
154
154
  end
155
155
  end
156
156
 
@@ -35,12 +35,8 @@ describe "Case: a song and his ratings" do
35
35
  end
36
36
 
37
37
  context "when nothing" do
38
- context "considering the song" do
39
- subject { @song }
40
-
41
- it "should not have ratings" do
42
- @song.ratings.should be_empty
43
- end
38
+ it "Song should not have ratings" do
39
+ @song.ratings.should be_empty
44
40
  end
45
41
  end
46
42
 
@@ -53,11 +49,13 @@ describe "Case: a song and his ratings" do
53
49
  context "considering the song" do
54
50
  subject { @song }
55
51
 
56
- its(:ratings) { should have(1).rating }
57
- its(:ratings_count) { should eq 1 }
58
- its(:ratings_note) { should eq [5] }
59
- its(:ratings_comment) { should eq ["Good!"] }
60
- its(:ratings_upset_level) { should eq [] }
52
+ it "denormalized fields should be set" do
53
+ @song.ratings.should have(1).rating
54
+ @song.ratings_count.should eq 1
55
+ @song.ratings_note.should eq [5]
56
+ @song.ratings_comment.should eq ["Good!"]
57
+ @song.ratings_upset_level.should eq []
58
+ end
61
59
 
62
60
  context "when modifing the first rating (=4)" do
63
61
  before do
@@ -67,11 +65,13 @@ describe "Case: a song and his ratings" do
67
65
  @song.reload
68
66
  end
69
67
 
70
- its(:ratings) { should have(1).rating }
71
- its(:ratings_count) { should eq 1 }
72
- its(:ratings_note) { should eq [4] }
73
- its(:ratings_comment) { should eq ["Good!"] }
74
- its(:ratings_upset_level) { should eq [0] }
68
+ it "denormalized fields should be set" do
69
+ @song.ratings.should have(1).rating
70
+ @song.ratings_count.should eq 1
71
+ @song.ratings_note.should eq [4]
72
+ @song.ratings_comment.should eq ["Good!"]
73
+ @song.ratings_upset_level.should eq [0]
74
+ end
75
75
  end
76
76
 
77
77
  context "when adding an other rating (=5)" do
@@ -80,10 +80,12 @@ describe "Case: a song and his ratings" do
80
80
  @song.reload
81
81
  end
82
82
 
83
- its(:ratings) { should have(2).rating }
84
- its(:ratings_count) { should eq 2 }
85
- its(:ratings_note) { should eq [5, 5] }
86
- its(:ratings_comment) { should eq ["Good!", "Another good!"] }
83
+ it "denormalized fields should be set" do
84
+ @song.ratings.should have(2).rating
85
+ @song.ratings_count.should eq 2
86
+ @song.ratings_note.should eq [5, 5]
87
+ @song.ratings_comment.should eq ["Good!", "Another good!"]
88
+ end
87
89
 
88
90
  context "when modifing the first rating (=4)" do
89
91
  before do
@@ -92,9 +94,11 @@ describe "Case: a song and his ratings" do
92
94
  @song.reload
93
95
  end
94
96
 
95
- its(:ratings_count) { should eq 2 }
96
- its(:ratings_note) { should eq [5, 4] }
97
- its(:ratings_comment) { should eq ["Good!", "Another good!"] }
97
+ it "denormalized fields should be set" do
98
+ @song.ratings_count.should eq 2
99
+ @song.ratings_note.should eq [5, 4]
100
+ @song.ratings_comment.should eq ["Good!", "Another good!"]
101
+ end
98
102
  end
99
103
 
100
104
  context "when modifing the other rating (=4)" do
@@ -104,9 +108,11 @@ describe "Case: a song and his ratings" do
104
108
  @song.reload
105
109
  end
106
110
 
107
- its(:ratings_count) { should eq 2 }
108
- its(:ratings_note) { should eq [5, 4] }
109
- its(:ratings_comment) { should eq ["Good!", "Another good!"] }
111
+ it "denormalized fields should be set" do
112
+ @song.ratings_count.should eq 2
113
+ @song.ratings_note.should eq [5, 4]
114
+ @song.ratings_comment.should eq ["Good!", "Another good!"]
115
+ end
110
116
 
111
117
  context "when modifing again the other rating (=3)" do
112
118
  before do
@@ -116,9 +122,11 @@ describe "Case: a song and his ratings" do
116
122
  @song.reload
117
123
  end
118
124
 
119
- its(:ratings_count) { should eq 2 }
120
- its(:ratings_note) { should eq [5, 3] }
121
- its(:ratings_comment) { should eq ["Good!", "Another good, again!"] }
125
+ it "denormalized fields should be set" do
126
+ @song.ratings_count.should eq 2
127
+ @song.ratings_note.should eq [5, 3]
128
+ @song.ratings_comment.should eq ["Good!", "Another good, again!"]
129
+ end
122
130
  end
123
131
  end
124
132
 
@@ -128,10 +136,12 @@ describe "Case: a song and his ratings" do
128
136
  @song.reload
129
137
  end
130
138
 
131
- its(:ratings_count) { should eq 1 }
132
- its(:ratings) { should have(1).rating }
133
- its(:ratings_note) { should eq [5] }
134
- its(:ratings_comment) { should eq ["Good!"] }
139
+ it "denormalized fields should be set" do
140
+ @song.ratings_count.should eq 1
141
+ @song.ratings.should have(1).rating
142
+ @song.ratings_note.should eq [5]
143
+ @song.ratings_comment.should eq ["Good!"]
144
+ end
135
145
  end
136
146
  end
137
147
 
@@ -141,10 +151,12 @@ describe "Case: a song and his ratings" do
141
151
  @song.reload
142
152
  end
143
153
 
144
- its(:ratings) { should have(1).rating }
145
- its(:ratings_count) { should eq 1 }
146
- its(:ratings_note) { should eq [5] }
147
- its(:ratings_comment) { should eq ["Good!"] }
154
+ it "denormalized fields should remain the same" do
155
+ @song.ratings.should have(1).rating
156
+ @song.ratings_count.should eq 1
157
+ @song.ratings_note.should eq [5]
158
+ @song.ratings_comment.should eq ["Good!"]
159
+ end
148
160
 
149
161
  context "when associating it" do
150
162
  before do
@@ -153,10 +165,12 @@ describe "Case: a song and his ratings" do
153
165
  @song.reload
154
166
  end
155
167
 
156
- its(:ratings) { should have(2).rating }
157
- its(:ratings_count) { should eq 2 }
158
- its(:ratings_note) { should eq [5, 1] }
159
- its(:ratings_comment) { should eq ["Good!", "Bad"] }
168
+ it "denormalized fields should be set" do
169
+ @song.ratings.should have(2).rating
170
+ @song.ratings_count.should eq 2
171
+ @song.ratings_note.should eq [5, 1]
172
+ @song.ratings_comment.should eq ["Good!", "Bad"]
173
+ end
160
174
  end
161
175
 
162
176
  context "when associating it (2nd way)" do
@@ -165,10 +179,12 @@ describe "Case: a song and his ratings" do
165
179
  @song.reload
166
180
  end
167
181
 
168
- its(:ratings) { should have(2).rating }
169
- its(:ratings_count) { should eq 2 }
170
- its(:ratings_note) { should eq [5, 1] }
171
- its(:ratings_comment) { should eq ["Good!", "Bad"] }
182
+ it "denormalized fields should be set" do
183
+ @song.ratings.should have(2).rating
184
+ @song.ratings_count.should eq 2
185
+ @song.ratings_note.should eq [5, 1]
186
+ @song.ratings_comment.should eq ["Good!", "Bad"]
187
+ end
172
188
  end
173
189
  end
174
190
 
@@ -180,22 +196,19 @@ describe "Case: a song and his ratings" do
180
196
  @song.reload
181
197
  end
182
198
 
183
- its(:ratings) { should have(0).rating }
184
- its(:ratings_count) { should eq 0 }
185
- its(:ratings_note) { should eq [] }
186
- its(:ratings_comment) { should eq [] }
187
-
188
- context "considering the other song" do
189
- before do
190
- @other_song.reload
191
- end
192
-
193
- subject { @other_song }
199
+ it "denormalized fields should be set in old Song" do
200
+ @song.ratings.should have(0).rating
201
+ @song.ratings_count.should eq 0
202
+ @song.ratings_note.should eq []
203
+ @song.ratings_comment.should eq []
204
+ end
194
205
 
195
- its(:ratings) { should have(1).rating }
196
- its(:ratings_count) { should eq 1 }
197
- its(:ratings_note) { should eq [5] }
198
- its(:ratings_comment) { should eq ["Good!"] }
206
+ it "denormalized fields should be set in new Song" do
207
+ @other_song.reload
208
+ @other_song.ratings.should have(1).rating
209
+ @other_song.ratings_count.should eq 1
210
+ @other_song.ratings_note.should eq [5]
211
+ @other_song.ratings_comment.should eq ["Good!"]
199
212
  end
200
213
  end
201
214
  end
data/spec/spec_helper.rb CHANGED
@@ -3,26 +3,63 @@ require 'rubygems'
3
3
  require 'rspec'
4
4
  require 'mongoid'
5
5
  require 'mongoid_max_denormalize'
6
- require 'logger'
6
+
7
+ # Are we in presence of Mongoid 3
8
+ def mongoid3
9
+ Mongoid::VERSION =~ /\A3\./
10
+ end
11
+
12
+ # When testing locally we use the database named mongoid_max_denormalize_test.
13
+ # However when tests are running in parallel on Travis we need to use different
14
+ # database names for each process running since we do not have transactions and
15
+ # want a clean slate before each spec run.
16
+ def database_name
17
+ ENV["CI"] ? "mongoid_max_denormalize_#{Process.pid}" : "mongoid_max_denormalize_test"
18
+ end
19
+
20
+ # Logger
21
+ if mongoid3 && !ENV["CI"]
22
+ Moped.logger = Logger.new("log/moped-test.log")
23
+ Moped.logger.level = Logger::DEBUG
24
+ end
7
25
 
8
26
  Mongoid.configure do |config|
9
- config.connect_to('mongoid_max_denormalize_test')
27
+ if mongoid3
28
+ config.connect_to(database_name)
29
+ else
30
+ database = Mongo::Connection.new("127.0.0.1", 27017).db(database_name)
31
+ database.add_user("mongoid", "test")
32
+ config.master = database
33
+ config.logger = nil
34
+ end
10
35
  end
11
36
 
12
- Mongoid.logger = Logger.new("log/mongoid-test.log")
13
- Mongoid.logger.level = Logger::DEBUG
14
- Moped.logger = Logger.new("log/moped-test.log")
15
- Moped.logger.level = Logger::DEBUG
16
37
 
17
38
  RSpec.configure do |config|
18
39
 
40
+ config.before(:suite) do
41
+ puts "#\n# Mongoid #{Mongoid::VERSION}\n#"
42
+ end
43
+
19
44
  # Drop all collections and clear the identity map before each spec.
20
45
  config.before(:each) do
21
- Moped.logger.info("\n ### " << example.full_description)
46
+ Moped.logger.info("\n ### " << example.full_description) if mongoid3 && !ENV["CI"]
22
47
 
23
48
  Mongoid.purge!
24
49
  Mongoid::IdentityMap.clear
25
50
  end
26
51
 
52
+ # On travis we are creating many different databases on each test run. We
53
+ # drop the database after the suite.
54
+ config.after(:suite) do
55
+ if ENV["CI"]
56
+ if mongoid3
57
+ Mongoid::Threaded.sessions[:default].drop
58
+ else
59
+ Mongoid.master.connection.drop_database(database_name)
60
+ end
61
+ end
62
+ end
63
+
27
64
  end
28
65
 
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.5
4
+ version: 0.0.6
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-06-14 00:00:00.000000000 Z
12
+ date: 2012-06-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 3.0.0.rc
21
+ version: '2.4'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 3.0.0.rc
29
+ version: '2.4'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: activesupport
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -112,12 +112,18 @@ 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
115
118
  required_rubygems_version: !ruby/object:Gem::Requirement
116
119
  none: false
117
120
  requirements:
118
121
  - - ! '>='
119
122
  - !ruby/object:Gem::Version
120
123
  version: '0'
124
+ segments:
125
+ - 0
126
+ hash: -1176296074452929639
121
127
  requirements: []
122
128
  rubyforge_project:
123
129
  rubygems_version: 1.8.24