mongoid_rating 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 72536149cfcd00af58a019ae87fd58ff6ef7df12
4
- data.tar.gz: 71e4a9e92c6fadb02837169e164210e6e0ce9f39
3
+ metadata.gz: c46a533fed5f93903eea6cb5af5d45ad034fd14c
4
+ data.tar.gz: 0ebcd0ae9aea25aca80ad0d9712035cc7dc593bb
5
5
  SHA512:
6
- metadata.gz: e626fef6faa5f24f34aaece3536875a5342f365b3772a5e6010ae177dddaf33f76c433b7612cc7c3c3349437fe16fa8d69eba9d0a84acbac815018c4bb806bbc
7
- data.tar.gz: b10bba1aeaaf6c92dc291d4407548e1a804dfa5f05d2d3144d4a1a241c12afe56eb6b935919de7f808b53e7832968423364ce1d22f4881c13b31b48fec545998
6
+ metadata.gz: 1e0a902185c5a8115b8f879bcf5bac2f10b3a08d3a3bfd4b127fce5e4db402d9398a67974d0c5dfe366cfdd299e0f96a23a3f18bc0667142aa44603b19c09bf7
7
+ data.tar.gz: ee70c1b36395f47eec03e008c8252a7a291289f8622f292b422c102833a4aeef46f54ff247e6f2862478f9b4e0b9c04125cd6a672f8f1a61887557a4bb41bcec
data/README.md CHANGED
@@ -11,7 +11,6 @@
11
11
 
12
12
  - Multiple rating fields per model
13
13
  - Float rating marks (users can give 4.5 stars)
14
- - Accurate concurrent rating updates with db.eval
15
14
 
16
15
  ## Installation
17
16
 
@@ -9,17 +9,15 @@ module Mongoid
9
9
  # @param [Hash] options a hash containings:
10
10
  #
11
11
  # rateable :overall, range: -5..5
12
- #
13
- # Disable atomic eval (in case it's disabled on the database side)
14
- # rateable :design, range: -5..5, average: false
15
12
  #
16
13
  # Disable average completely
14
+ # rateable :design, range: -5..5, average: false
15
+ #
17
16
  # rateable :quality, range: -5..5, average: true
18
17
  def rateable(field, options = {})
19
18
  options = {
20
19
  range: 1..5,
21
20
  rerate: true,
22
- eval: true,
23
21
  counters: true,
24
22
  float: true
25
23
  }.merge(options)
@@ -61,44 +59,11 @@ module Mongoid
61
59
  raise "bad vote value"
62
60
  end
63
61
  raise "can't rate" unless can_#{field}?(rater)
64
- if #{options[:eval]}
65
- #{field}_data.where(rater_id: rater.id).destroy_all
62
+ un#{field}!(rater)
63
+ atomically do
64
+ inc("#{field}_count" => 1, "#{field}_sum" => value)
66
65
  #{field}_data.create!(rater: rater, value: value)
67
- collection.database.session.cluster.with_primary do
68
- doc = collection.database.command({
69
- eval: 'function(id) {
70
- var oid = ObjectId(id);
71
- var doc = db.' + collection.name + '.findOne( { _id : oid } );
72
- if (doc) {
73
- doc.#{field}_count = 0
74
- doc.#{field}_sum = 0;
75
- doc.#{field}_data.forEach(function(fd) {
76
- doc.#{field}_sum += fd.value;
77
- doc.#{field}_count += 1;
78
- })
79
- doc.#{field}_average = doc.#{field}_sum /doc.#{field}_count;
80
- db.' + collection.name + '.save(doc);
81
- return doc;
82
- } else {
83
- return false;
84
- }
85
- }',
86
- args: [ id.to_s ]
87
- })
88
- self.#{field}_count = doc[:retval]["#{field}_count"]
89
- self.#{field}_sum = doc[:retval]["#{field}_sum"]
90
- self.#{field}_average = doc[:retval]["#{field}_average"]
91
- remove_change(:#{field}_count)
92
- remove_change(:#{field}_sum)
93
- remove_change(:#{field}_average)
94
- end
95
- else
96
- un#{field}!(rater)
97
- atomically do
98
- inc("#{field}_count" => 1, "#{field}_sum" => value)
99
- #{field}_data.create!(rater: rater, value: value)
100
- set("#{field}_average" => calc_#{field}_avg)
101
- end
66
+ set("#{field}_average" => calc_#{field}_avg)
102
67
  end
103
68
  end
104
69
  def calc_#{field}_avg
@@ -1,3 +1,3 @@
1
1
  module MongoidRating
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/spec/article_spec.rb CHANGED
@@ -39,7 +39,7 @@ describe Article do
39
39
 
40
40
  describe "#overall" do
41
41
  it "should track #overall properly" do
42
- @article.overall! 1, @sally
42
+ @article.overall!(1, @sally)
43
43
  @article.overall_count.should eql 2
44
44
  @article.overall.should eql 1.0
45
45
  end
@@ -51,17 +51,17 @@ describe Article do
51
51
  end
52
52
 
53
53
  it "should limit #overalls by user properly" do
54
- @article.overall! 5, @bob
54
+ @article.overall!(5, @bob)
55
55
  @article.overall.should eql 5.0
56
56
  end
57
57
 
58
58
  context "when overall_value in rating range" do
59
- it { expect { @article.overall 1, @sally }.not_to raise_error }
59
+ it { expect { @article.overall(1, @sally) }.not_to raise_error }
60
60
  end
61
61
 
62
62
  context "when overall_value not in rating range" do
63
- it { expect { @article.overall 17, @sally }.to raise_error() }
64
- it { expect { @article.overall -17, @sally }.to raise_error() }
63
+ it { expect { @article.overall(17, @sally) }.to raise_error() }
64
+ it { expect { @article.overall(-17, @sally) }.to raise_error() }
65
65
  end
66
66
 
67
67
  describe "when using positive values" do
@@ -133,7 +133,7 @@ describe Article do
133
133
  end
134
134
 
135
135
  it "should calculate the average overall if the result is zero" do
136
- @article.overall -1, @sally
136
+ @article.overall(-1, @sally)
137
137
  @article.overall.should eq 0.0
138
138
  end
139
139
  end
@@ -165,26 +165,26 @@ describe Article do
165
165
 
166
166
  context "when saving the collection" do
167
167
  before (:each) do
168
- @article.overall 3, @bob
169
- @article.overall -5, @sally
168
+ @article.overall(3, @bob)
169
+ @article.overall(-5, @sally)
170
170
  @article.save
171
171
  @f_article = Article.where(:name => "Article").first
172
172
  end
173
173
 
174
174
  it "disallows incorrect rates" do
175
- expect { @article.overall 8, @bob }.to raise_error
176
- expect { @article.overall -10, @sally }.to raise_error
175
+ expect { @article.overall(8, @bob) }.to raise_error
176
+ expect { @article.overall(-10, @sally) }.to raise_error
177
177
  end
178
178
 
179
179
  describe "#overall_by?" do
180
180
  describe "for Bob" do
181
181
  specify { @f_article.overall_by?(@bob).should be_true }
182
- specify { @f_article.overall_by(@bob).should eq 3 }
182
+ specify { @f_article.overall_by(@bob).should eq(3) }
183
183
  end
184
184
 
185
185
  describe "for Sally" do
186
186
  specify { @f_article.overall_by?(@sally).should be_true }
187
- specify { @f_article.overall_by(@sally).should eq -5 }
187
+ specify { @f_article.overall_by(@sally).should eq(-5) }
188
188
  end
189
189
 
190
190
  describe "for Alice" do
@@ -194,11 +194,11 @@ describe Article do
194
194
  end
195
195
 
196
196
  describe "#overall" do
197
- specify { @f_article.overall.should eql -1.0 }
197
+ specify { @f_article.overall.should eq(-1.0) }
198
198
  end
199
199
 
200
200
  describe "#overall_count" do
201
- specify { @f_article.overall_count.should eql 2 }
201
+ specify { @f_article.overall_count.should eq(2) }
202
202
  end
203
203
  end
204
204
  end
@@ -268,14 +268,12 @@ describe Article do
268
268
  describe '#by_overall' do
269
269
  it "should return proper count of articles" do
270
270
  Article.by_overall.limit(10).count(true).should eq 5
271
- Article.by_overall.to_a.should eq [@article3, @article1, @article2, @article4, @article5]
272
271
  end
273
272
 
274
273
  it 'returns articles in proper order' do
275
-
274
+ Article.by_overall.to_a.should eq [@article3, @article1, @article2, @article4, @article5]
276
275
  end
277
276
  end
278
277
  end
279
-
280
278
  end
281
279
  end
data/spec/post_spec.rb CHANGED
@@ -178,12 +178,16 @@ describe Post do
178
178
  @post3 = Post.create(:name => "Post 3")
179
179
  @post4 = Post.create(:name => "Post 4")
180
180
  @post5 = Post.create(:name => "Post 5")
181
- @post1.rate 5, @sally
182
- @post1.rate 3, @bob
183
- @post4.rate 1, @sally
184
181
  end
185
182
 
183
+
186
184
  describe "#rate_by scope" do
185
+ before :each do
186
+ @post1.rate 5, @sally
187
+ @post1.rate 3, @bob
188
+ @post4.rate 1, @sally
189
+ end
190
+
187
191
  it "should return proper count of posts rated by Bob" do
188
192
  Post.rate_by(@bob).size.should eql 1
189
193
  end
@@ -193,7 +197,7 @@ describe Post do
193
197
  end
194
198
  end
195
199
 
196
- describe "#with_rating" do
200
+ context 'rates' do
197
201
  before (:each) do
198
202
  @post1.rate 4, @alice
199
203
  @post2.rate 2, @alice
@@ -201,6 +205,11 @@ describe Post do
201
205
  @post4.rate 2, @alice
202
206
  end
203
207
 
208
+ it '#highest_rate' do
209
+ Post.highest_rate.count.should eq 4
210
+ Post.highest_rate.first.id.should eq @post3.id
211
+ end
212
+
204
213
  it "should return proper count of posts with rating 4..5" do
205
214
  Post.rate_in(4..5).size.should eql 2
206
215
  end
@@ -212,25 +221,24 @@ describe Post do
212
221
  it "should return proper count of posts with rating 0..5" do
213
222
  Post.rate_in(0..5).size.should eql 4
214
223
  end
215
- end
216
224
 
217
- describe "#highest_rated" do
218
- it "should return proper count of posts" do
219
- #mongoid has problems with returning count of documents (https://github.com/mongoid/mongoid/issues/817)
220
- posts_count = 0
221
- Post.highest_rate.limit(1).each {|x| posts_count+=1 }
222
- posts_count.should eql 1
225
+ describe "#highest_rate" do
226
+ it "should return proper document" do
227
+ Post.highest_rate.limit(1).first.name.should eql "Post 3"
228
+ end
223
229
  end
224
230
 
225
- it "should return proper count of posts" do
226
- #mongoid has problems with returning count of documents (https://github.com/mongoid/mongoid/issues/817)
227
- posts_count = 0
228
- Post.highest_rate.limit(10).each {|x| posts_count+=1 }
229
- posts_count.should eql 2
230
- end
231
+ describe '#by_rate' do
232
+ it "should return proper count of posts" do
233
+ Post.by_rate.limit(10).count(true).should eq 5
234
+ end
231
235
 
232
- it "should return proper document" do
233
- Post.highest_rate.limit(1).first.name.should eql "Post 1"
236
+ it 'returns articles in proper order' do
237
+ @post5.rate.should be_nil
238
+ @post5[:rate_average].should be_nil
239
+
240
+ Post.by_rate.to_a.should eq [@post3, @post1, @post2, @post4, @post5]
241
+ end
234
242
  end
235
243
  end
236
244
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_rating
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - glebtv
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-25 00:00:00.000000000 Z
11
+ date: 2013-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -151,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  version: '0'
152
152
  requirements: []
153
153
  rubyforge_project:
154
- rubygems_version: 2.0.6
154
+ rubygems_version: 2.1.10
155
155
  signing_key:
156
156
  specification_version: 4
157
157
  summary: Star rating for Mongoid