mongoid_rating 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -7
- data/README.md +4 -1
- data/lib/mongoid_rating/model.rb +4 -0
- data/lib/mongoid_rating/version.rb +1 -1
- data/spec/article_spec.rb +184 -162
- data/spec/spec_helper.rb +4 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 72536149cfcd00af58a019ae87fd58ff6ef7df12
|
4
|
+
data.tar.gz: 71e4a9e92c6fadb02837169e164210e6e0ce9f39
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e626fef6faa5f24f34aaece3536875a5342f365b3772a5e6010ae177dddaf33f76c433b7612cc7c3c3349437fe16fa8d69eba9d0a84acbac815018c4bb806bbc
|
7
|
+
data.tar.gz: b10bba1aeaaf6c92dc291d4407548e1a804dfa5f05d2d3144d4a1a241c12afe56eb6b935919de7f808b53e7832968423364ce1d22f4881c13b31b48fec545998
|
data/.travis.yml
CHANGED
@@ -8,15 +8,8 @@ rvm:
|
|
8
8
|
- 1.9.3
|
9
9
|
- 2.0.0
|
10
10
|
- jruby-19mode
|
11
|
-
- rbx-19mode
|
12
11
|
|
13
12
|
gemfile:
|
14
13
|
- Gemfile
|
15
|
-
- gemfiles/mongoid-3.0.gemfile
|
16
|
-
- gemfiles/mongoid-3.1.gemfile
|
17
14
|
- gemfiles/mongoid-4.0.gemfile
|
18
15
|
|
19
|
-
matrix:
|
20
|
-
exclude:
|
21
|
-
- rvm: rbx-19mode
|
22
|
-
gemfile: gemfiles/mongoid-4.0.gemfile
|
data/README.md
CHANGED
@@ -34,6 +34,8 @@ make model rateable:
|
|
34
34
|
class Post
|
35
35
|
include Mongoid::Document
|
36
36
|
rateable :rate
|
37
|
+
# needed if you use rails_admin so it would not complain
|
38
|
+
accepts_nested_attributes_for :rate_data
|
37
39
|
end
|
38
40
|
ps = Post.create()
|
39
41
|
user = User.create()
|
@@ -57,6 +59,7 @@ Check if user rated:
|
|
57
59
|
|
58
60
|
Scopes:
|
59
61
|
|
62
|
+
Post.highest_rate
|
60
63
|
Post.rate_in(2..5)
|
61
64
|
Post.rate_in(2..5).first
|
62
65
|
=> #<Post rate_count: 1, rate_sum: 5.0, rate_average: 5.0>
|
@@ -73,7 +76,7 @@ Posts rated by user:
|
|
73
76
|
(c) 2013 glebtv, MIT license
|
74
77
|
|
75
78
|
Partially based on
|
76
|
-
|
79
|
+
[mongoid-rateable](https://github.com/proton/mongoid_rateable)
|
77
80
|
which is Copyright (c) 2011 Peter Savichev (proton), MIT license
|
78
81
|
|
79
82
|
## Contributing
|
data/lib/mongoid_rating/model.rb
CHANGED
@@ -47,6 +47,10 @@ module Mongoid
|
|
47
47
|
scope :#{field}_in, ->(range) {
|
48
48
|
where(#{savg}.gte => range.begin, #{savg}.lte => range.end)
|
49
49
|
}
|
50
|
+
|
51
|
+
scope :by_#{field}, -> {
|
52
|
+
order_by([#{savg}, :desc])
|
53
|
+
}
|
50
54
|
scope :highest_#{field}, -> {
|
51
55
|
where(#{savg}.ne => nil).order_by([#{savg}, :desc])
|
52
56
|
}
|
data/spec/article_spec.rb
CHANGED
@@ -6,212 +6,218 @@ describe Article do
|
|
6
6
|
@bob = User.create :name => "Bob"
|
7
7
|
@sally = User.create :name => "Sally"
|
8
8
|
@alice = User.create :name => "Alice"
|
9
|
-
@article = Article.create :name => "Article"
|
10
|
-
@article1 = Article.create :name => "Article 1"
|
11
|
-
@article2 = Article.create :name => "Article 2"
|
12
9
|
end
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
it { should respond_to :overall_average }
|
20
|
-
it { should respond_to :overall_data }
|
21
|
-
it { should respond_to :overall_by }
|
22
|
-
|
23
|
-
describe "#overall_values" do
|
24
|
-
it "should be an array with overall values" do
|
25
|
-
@article.overall_values.should be_an_instance_of Array
|
26
|
-
@article.overall! 1, @bob
|
27
|
-
@article.overall_values.should eq [1]
|
11
|
+
context 'basic' do
|
12
|
+
before(:each) do
|
13
|
+
@article = Article.create :name => "Article"
|
14
|
+
@article1 = Article.create :name => "Article 1"
|
15
|
+
@article2 = Article.create :name => "Article 2"
|
28
16
|
end
|
29
|
-
end
|
30
17
|
|
31
|
-
|
32
|
-
|
33
|
-
|
18
|
+
subject { @article }
|
19
|
+
it { should respond_to :overall }
|
20
|
+
it { should respond_to :overall! }
|
21
|
+
it { should respond_to :unoverall! }
|
22
|
+
it { should respond_to :did_overall? }
|
23
|
+
it { should respond_to :overall_average }
|
24
|
+
it { should respond_to :overall_data }
|
25
|
+
it { should respond_to :overall_by }
|
26
|
+
|
27
|
+
describe "#overall_values" do
|
28
|
+
it "should be an array with overall values" do
|
29
|
+
@article.overall_values.should be_an_instance_of Array
|
30
|
+
@article.overall! 1, @bob
|
31
|
+
@article.overall_values.should eq [1]
|
32
|
+
end
|
34
33
|
end
|
35
34
|
|
36
|
-
|
37
|
-
|
38
|
-
@article.overall! 1, @
|
39
|
-
@article.overall_count.should eql 2
|
40
|
-
@article.overall.should eql 1.0
|
35
|
+
context "when overall" do
|
36
|
+
before (:each) do
|
37
|
+
@article.overall! 1, @bob
|
41
38
|
end
|
42
39
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
40
|
+
describe "#overall" do
|
41
|
+
it "should track #overall properly" do
|
42
|
+
@article.overall! 1, @sally
|
43
|
+
@article.overall_count.should eql 2
|
44
|
+
@article.overall.should eql 1.0
|
45
|
+
end
|
48
46
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
47
|
+
it "should not mark fields as dirty" do
|
48
|
+
@article.overall_count_changed?.should be_false
|
49
|
+
@article.overall_sum_changed?.should be_false
|
50
|
+
@article.overall_average_changed?.should be_false
|
51
|
+
end
|
53
52
|
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
it "should limit #overalls by user properly" do
|
54
|
+
@article.overall! 5, @bob
|
55
|
+
@article.overall.should eql 5.0
|
56
|
+
end
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
end
|
58
|
+
context "when overall_value in rating range" do
|
59
|
+
it { expect { @article.overall 1, @sally }.not_to raise_error }
|
60
|
+
end
|
62
61
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
end
|
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() }
|
65
|
+
end
|
68
66
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
end
|
67
|
+
describe "when using positive values" do
|
68
|
+
let(:num) { rand(1..5) }
|
69
|
+
let(:exp) { ((num + 1) / 2.0) - 1 }
|
70
|
+
it { expect { @article.overall num, @sally }.to change { @article.overall }.by(exp) }
|
71
|
+
end
|
75
72
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
specify { @article1.overall_by?(@bob).should be_false }
|
82
|
-
specify { @article.overall_by?(@alice).should be_false }
|
73
|
+
describe "when using negative values" do
|
74
|
+
let(:num) { -rand(1..5) }
|
75
|
+
let(:exp) { ((num + 1) / 2.0) - 1 }
|
76
|
+
it { expect { @article.overall num, @sally }.to change { @article.overall }.by(exp) }
|
77
|
+
end
|
83
78
|
end
|
84
79
|
|
85
|
-
describe "
|
86
|
-
|
87
|
-
@article.
|
80
|
+
describe "#overall_by?" do
|
81
|
+
describe "for Bob" do
|
82
|
+
specify { @article.overall_by?(@bob).should be_true }
|
83
|
+
end
|
84
|
+
describe "for Bob" do
|
85
|
+
specify { @article1.overall_by?(@bob).should be_false }
|
86
|
+
specify { @article.overall_by?(@alice).should be_false }
|
88
87
|
end
|
89
88
|
|
90
|
-
describe "
|
91
|
-
|
89
|
+
describe "when overall by someone else" do
|
90
|
+
before do
|
91
|
+
@article.overall 1, @alice
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "for Alice" do
|
95
|
+
specify { @article.overall_by?(@alice).should be_true }
|
96
|
+
end
|
92
97
|
end
|
93
|
-
end
|
94
98
|
|
95
|
-
|
96
|
-
|
97
|
-
|
99
|
+
describe "when not overall by someone else" do
|
100
|
+
describe "for Sally" do
|
101
|
+
specify { @article.overall_by?(@sally).should be_false }
|
102
|
+
end
|
98
103
|
end
|
99
104
|
end
|
100
|
-
end
|
101
105
|
|
102
|
-
|
103
|
-
|
106
|
+
describe "#unoverall" do
|
107
|
+
before { @article.unoverall @bob }
|
104
108
|
|
105
|
-
|
106
|
-
|
107
|
-
|
109
|
+
it "should have null #overall_count" do
|
110
|
+
@article.overall_count.should eql 0
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should have null #overall" do
|
114
|
+
@article.overall.should be_nil
|
115
|
+
end
|
108
116
|
|
109
|
-
|
110
|
-
|
117
|
+
it "#overall_by?(@bob) should be false after unoverall" do
|
118
|
+
@article.overall_by?(@bob).should be_false
|
119
|
+
end
|
111
120
|
end
|
112
121
|
|
113
|
-
|
114
|
-
|
122
|
+
describe "#overall_count" do
|
123
|
+
it "should know how many overalls have been cast" do
|
124
|
+
@article.overall 1, @sally
|
125
|
+
@article.overall_count.should eql 2
|
126
|
+
end
|
115
127
|
end
|
116
|
-
end
|
117
128
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
129
|
+
describe "#rating" do
|
130
|
+
it "should calculate the average overall" do
|
131
|
+
@article.overall 4, @sally
|
132
|
+
@article.overall.should eq 2.5
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should calculate the average overall if the result is zero" do
|
136
|
+
@article.overall -1, @sally
|
137
|
+
@article.overall.should eq 0.0
|
138
|
+
end
|
122
139
|
end
|
123
140
|
end
|
124
141
|
|
125
|
-
|
126
|
-
|
127
|
-
@article.
|
128
|
-
@article.overall.should eq 2.5
|
142
|
+
context "when not overall" do
|
143
|
+
describe "#overalls" do
|
144
|
+
specify { @article.overall_count.should eql 0 }
|
129
145
|
end
|
130
146
|
|
131
|
-
|
132
|
-
@article.overall
|
133
|
-
@article.overall.should eq 0.0
|
147
|
+
describe "#rating" do
|
148
|
+
specify { @article.overall.should be_nil }
|
134
149
|
end
|
135
|
-
end
|
136
|
-
end
|
137
150
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
151
|
+
describe "#unoverall" do
|
152
|
+
before do
|
153
|
+
@article.unoverall @sally
|
154
|
+
end
|
142
155
|
|
143
|
-
|
144
|
-
|
145
|
-
|
156
|
+
it "should have null #overall_count" do
|
157
|
+
@article.overall_count.should eql 0
|
158
|
+
end
|
146
159
|
|
147
|
-
|
148
|
-
|
149
|
-
|
160
|
+
it "should have null #overalls" do
|
161
|
+
@article.overall.should be_nil
|
162
|
+
end
|
150
163
|
end
|
164
|
+
end
|
151
165
|
|
152
|
-
|
153
|
-
|
166
|
+
context "when saving the collection" do
|
167
|
+
before (:each) do
|
168
|
+
@article.overall 3, @bob
|
169
|
+
@article.overall -5, @sally
|
170
|
+
@article.save
|
171
|
+
@f_article = Article.where(:name => "Article").first
|
154
172
|
end
|
155
173
|
|
156
|
-
it "
|
157
|
-
@article.overall.
|
174
|
+
it "disallows incorrect rates" do
|
175
|
+
expect { @article.overall 8, @bob }.to raise_error
|
176
|
+
expect { @article.overall -10, @sally }.to raise_error
|
158
177
|
end
|
159
|
-
end
|
160
|
-
end
|
161
178
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
@f_article = Article.where(:name => "Article").first
|
168
|
-
end
|
179
|
+
describe "#overall_by?" do
|
180
|
+
describe "for Bob" do
|
181
|
+
specify { @f_article.overall_by?(@bob).should be_true }
|
182
|
+
specify { @f_article.overall_by(@bob).should eq 3 }
|
183
|
+
end
|
169
184
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
185
|
+
describe "for Sally" do
|
186
|
+
specify { @f_article.overall_by?(@sally).should be_true }
|
187
|
+
specify { @f_article.overall_by(@sally).should eq -5 }
|
188
|
+
end
|
174
189
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
190
|
+
describe "for Alice" do
|
191
|
+
specify { @f_article.overall_by?(@alice).should be_false}
|
192
|
+
specify { @f_article.overall_by(@alice).should be_nil }
|
193
|
+
end
|
179
194
|
end
|
180
195
|
|
181
|
-
describe "
|
182
|
-
specify { @f_article.
|
183
|
-
specify { @f_article.overall_by(@sally).should eq -5 }
|
196
|
+
describe "#overall" do
|
197
|
+
specify { @f_article.overall.should eql -1.0 }
|
184
198
|
end
|
185
199
|
|
186
|
-
describe "
|
187
|
-
specify { @f_article.
|
188
|
-
specify { @f_article.overall_by(@alice).should be_nil }
|
200
|
+
describe "#overall_count" do
|
201
|
+
specify { @f_article.overall_count.should eql 2 }
|
189
202
|
end
|
190
203
|
end
|
191
|
-
|
192
|
-
describe "#overall" do
|
193
|
-
specify { @f_article.overall.should eql -1.0 }
|
194
|
-
end
|
195
|
-
|
196
|
-
describe "#overall_count" do
|
197
|
-
specify { @f_article.overall_count.should eql 2 }
|
198
|
-
end
|
199
204
|
end
|
200
205
|
|
201
206
|
describe "#scopes" do
|
202
|
-
before
|
203
|
-
@article.delete
|
207
|
+
before :each do
|
204
208
|
@article1 = Article.create(:name => "Article 1")
|
205
209
|
@article2 = Article.create(:name => "Article 2")
|
206
210
|
@article3 = Article.create(:name => "Article 3")
|
207
211
|
@article4 = Article.create(:name => "Article 4")
|
208
212
|
@article5 = Article.create(:name => "Article 5")
|
209
|
-
@article1.overall 5, @sally
|
210
|
-
@article1.overall 3, @bob
|
211
|
-
@article4.overall 1, @sally
|
212
213
|
end
|
213
214
|
|
214
215
|
describe "#overall_by" do
|
216
|
+
before :each do
|
217
|
+
@article1.overall 5, @sally
|
218
|
+
@article1.overall 3, @bob
|
219
|
+
@article4.overall 1, @sally
|
220
|
+
end
|
215
221
|
it "should return proper count of articles overall by Bob" do
|
216
222
|
Article.overall_by(@bob).size.should eql 1
|
217
223
|
end
|
@@ -221,39 +227,55 @@ describe Article do
|
|
221
227
|
end
|
222
228
|
end
|
223
229
|
|
224
|
-
|
230
|
+
context 'rates' do
|
225
231
|
before (:each) do
|
226
232
|
@article1.overall 4, @alice
|
227
233
|
@article2.overall 2, @alice
|
228
234
|
@article3.overall 5, @alice
|
229
|
-
@article4.overall
|
235
|
+
@article4.overall 1, @alice
|
230
236
|
end
|
237
|
+
describe "#overall_in" do
|
238
|
+
it "should return proper count of articles with rating 4..5" do
|
239
|
+
Article.overall_in(4..5).to_a.length.should eql 2
|
240
|
+
end
|
231
241
|
|
232
|
-
|
233
|
-
|
234
|
-
|
242
|
+
it "should return proper count of articles with rating 0..2" do
|
243
|
+
Article.overall_in(0..2).to_a.length.should eql 2
|
244
|
+
end
|
235
245
|
|
236
|
-
|
237
|
-
|
246
|
+
it "should return proper count of articles with rating 0..5" do
|
247
|
+
Article.overall_in(0..5).to_a.length.should eql 4
|
248
|
+
end
|
238
249
|
end
|
239
250
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
251
|
+
describe "#highest_overall" do
|
252
|
+
it "should return proper count of articles" do
|
253
|
+
Article.highest_overall.limit(1).count(true).should eq 1
|
254
|
+
end
|
244
255
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
end
|
256
|
+
it "should return proper count of articles" do
|
257
|
+
Article.highest_overall.limit(10).count(true).should eq 4
|
258
|
+
end
|
249
259
|
|
250
|
-
|
251
|
-
|
260
|
+
it "should return proper document" do
|
261
|
+
Article.highest_overall.limit(1).first.name.should eql "Article 3"
|
262
|
+
end
|
263
|
+
it 'returns articles in proper order' do
|
264
|
+
Article.highest_overall.to_a.should eq [@article3, @article1, @article2, @article4]
|
265
|
+
end
|
252
266
|
end
|
253
267
|
|
254
|
-
|
255
|
-
|
268
|
+
describe '#by_overall' do
|
269
|
+
it "should return proper count of articles" do
|
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
|
+
end
|
273
|
+
|
274
|
+
it 'returns articles in proper order' do
|
275
|
+
|
276
|
+
end
|
256
277
|
end
|
257
278
|
end
|
279
|
+
|
258
280
|
end
|
259
281
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'coveralls'
|
2
|
+
Coveralls.wear!
|
3
|
+
|
1
4
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
5
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
3
6
|
|
@@ -7,16 +10,12 @@ require "rubygems"
|
|
7
10
|
require "rspec"
|
8
11
|
require "mongoid"
|
9
12
|
require "database_cleaner"
|
10
|
-
|
11
|
-
require 'coveralls'
|
12
|
-
Coveralls.wear!
|
13
|
-
|
14
13
|
require "mongoid_rating"
|
15
14
|
|
16
15
|
Dir["#{MODELS}/*.rb"].each { |f| require f }
|
17
16
|
|
18
17
|
Mongoid.configure do |config|
|
19
|
-
config.connect_to "
|
18
|
+
config.connect_to "mongoid_rating_test"
|
20
19
|
end
|
21
20
|
Mongoid.logger = Logger.new($stdout)
|
22
21
|
|
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.0
|
4
|
+
version: 0.1.0
|
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-
|
11
|
+
date: 2013-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|