rating 0.2.0 → 0.3.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +64 -24
- data/lib/generators/rating/templates/db/migrate/create_rating_tables.rb +8 -8
- data/lib/rating/models/rating/extension.rb +2 -2
- data/lib/rating/models/rating/rate.rb +5 -3
- data/lib/rating/models/rating/rating.rb +13 -8
- data/lib/rating/version.rb +1 -1
- data/spec/factories/rating/rate.rb +1 -1
- data/spec/factories/rating/rating.rb +1 -1
- data/spec/models/extension/after_create_spec.rb +5 -5
- data/spec/models/extension/order_by_rating_spec.rb +37 -53
- data/spec/models/extension/rate_for_spec.rb +5 -5
- data/spec/models/extension/rate_spec.rb +31 -5
- data/spec/models/extension/rated_question_spec.rb +9 -9
- data/spec/models/extension/rated_records_spec.rb +3 -17
- data/spec/models/extension/rated_spec.rb +9 -27
- data/spec/models/extension/rates_records_spec.rb +2 -16
- data/spec/models/extension/rates_spec.rb +5 -19
- data/spec/models/extension/rating_records_spec.rb +2 -18
- data/spec/models/extension/rating_spec.rb +3 -17
- data/spec/models/rate/create_spec.rb +56 -15
- data/spec/models/rate/rate_for_spec.rb +7 -7
- data/spec/models/rate_spec.rb +2 -2
- data/spec/models/rating/averager_data_spec.rb +8 -24
- data/spec/models/rating/data_spec.rb +12 -28
- data/spec/models/rating/update_rating_spec.rb +2 -18
- data/spec/models/rating/values_data_spec.rb +10 -26
- data/spec/models/rating_spec.rb +6 -0
- data/spec/support/db/migrate/add_comment_on_rating_rates_table.rb +7 -0
- data/spec/support/db/migrate/{create_category_spec.rb → create_categories_table.rb} +0 -0
- data/spec/support/migrate.rb +3 -1
- data/spec/support/shared_context/with_database_records.rb +20 -0
- metadata +35 -37
- data/spec/factories/user.rb +0 -7
- data/spec/support/db/migrate/create_users_table.rb +0 -9
- data/spec/support/models/user.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7828759c72eb95c9dc6562c8c56b2b42c2b266e6
|
4
|
+
data.tar.gz: ad5a258d49d2bbf05f0d2fbc54800e590850d5a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 880d5a7fe68b86e91d9b7eaf1c3f3ab3b7ef40eac8db2942daa8a7beca89e5887abbf92fbdc05b5bc751dd9ea292362bb5513c6adb43f74391a832027e5e15e4
|
7
|
+
data.tar.gz: 94e6666f1174c77da6cd9db33fd2415aa4af621813689da0ff7c1f082c55b39015d2538add63cff10b4972a4a5797859f1b3c42d061d4611b1b14106d6b703fb
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
[](https://travis-ci.org/wbotelhos/rating)
|
4
4
|
[](https://badge.fury.io/rb/rating)
|
5
|
+
[](https://codeclimate.com/github/wbotelhos/rating/maintainability)
|
6
|
+
[](https://liberapay.com/wbotelhos)
|
5
7
|
|
6
8
|
A true Bayesian rating system with scope and cache enabled.
|
7
9
|
|
@@ -66,7 +68,7 @@ rake db:migrate
|
|
66
68
|
Just add the callback `rating` to your model:
|
67
69
|
|
68
70
|
```ruby
|
69
|
-
class
|
71
|
+
class Author < ApplicationRecord
|
70
72
|
rating
|
71
73
|
end
|
72
74
|
```
|
@@ -78,10 +80,10 @@ Now this model can vote or receive votes.
|
|
78
80
|
You can vote on some resource:
|
79
81
|
|
80
82
|
```ruby
|
81
|
-
author =
|
83
|
+
author = Author.last
|
82
84
|
resource = Article.last
|
83
85
|
|
84
|
-
author.rate
|
86
|
+
author.rate resource, 3
|
85
87
|
```
|
86
88
|
|
87
89
|
### rating
|
@@ -109,7 +111,7 @@ It will return a `Rating` object that keeps:
|
|
109
111
|
You can retrieve the rate of some author gave to some resource:
|
110
112
|
|
111
113
|
```ruby
|
112
|
-
author =
|
114
|
+
author = Author.last
|
113
115
|
resource = Article.last
|
114
116
|
|
115
117
|
author.rate_for resource
|
@@ -128,7 +130,7 @@ It will return a `Rate` object that keeps:
|
|
128
130
|
Maybe you want just to know if some author already rated some resource and receive `true` or `false`:
|
129
131
|
|
130
132
|
```ruby
|
131
|
-
author =
|
133
|
+
author = Author.last
|
132
134
|
resource = Article.last
|
133
135
|
|
134
136
|
author.rated? resource
|
@@ -136,12 +138,12 @@ author.rated? resource
|
|
136
138
|
|
137
139
|
### rates
|
138
140
|
|
139
|
-
You can retrieve all rates
|
141
|
+
You can retrieve all rates received by some resource:
|
140
142
|
|
141
143
|
```ruby
|
142
|
-
|
144
|
+
resource = Article.last
|
143
145
|
|
144
|
-
|
146
|
+
resource.rates
|
145
147
|
```
|
146
148
|
|
147
149
|
It will return a collection of `Rate` object.
|
@@ -151,7 +153,7 @@ It will return a collection of `Rate` object.
|
|
151
153
|
In the same way you can retrieve all rates that some author received:
|
152
154
|
|
153
155
|
```ruby
|
154
|
-
author =
|
156
|
+
author = Author.last
|
155
157
|
|
156
158
|
author.rated
|
157
159
|
```
|
@@ -183,7 +185,7 @@ Let's say an article belongs to one or more categories and you want to vote on s
|
|
183
185
|
```ruby
|
184
186
|
category_1 = Category.first
|
185
187
|
category_2 = Category.second
|
186
|
-
author =
|
188
|
+
author = Author.last
|
187
189
|
resource = Article.last
|
188
190
|
```
|
189
191
|
|
@@ -192,28 +194,28 @@ In this situation you should scope the vote of article with some category:
|
|
192
194
|
**rate**
|
193
195
|
|
194
196
|
```ruby
|
195
|
-
author.rate resource, 3,
|
196
|
-
author.rate resource, 5,
|
197
|
+
author.rate resource, 3, scope: category_1
|
198
|
+
author.rate resource, 5, scope: category_2
|
197
199
|
```
|
198
200
|
|
199
|
-
Now `
|
201
|
+
Now `resource` has a rating for `category_1` and another one for `category_2`.
|
200
202
|
|
201
203
|
**rating**
|
202
204
|
|
203
|
-
Recovering the rating values for
|
205
|
+
Recovering the rating values for resource, we have:
|
204
206
|
|
205
207
|
```ruby
|
206
|
-
|
208
|
+
resource.rating
|
207
209
|
# nil
|
208
210
|
```
|
209
211
|
|
210
212
|
But using the scope to make the right query:
|
211
213
|
|
212
214
|
```ruby
|
213
|
-
|
215
|
+
resource.rating scope: category_1
|
214
216
|
# { average: 3, estimate: 3, sum: 3, total: 1 }
|
215
217
|
|
216
|
-
|
218
|
+
resource.rating scope: category_2
|
217
219
|
# { average: 5, estimate: 5, sum: 5, total: 1 }
|
218
220
|
```
|
219
221
|
|
@@ -222,7 +224,7 @@ author.rating scope: category_2
|
|
222
224
|
On the same way you can find your rates with a scoped query:
|
223
225
|
|
224
226
|
```ruby
|
225
|
-
|
227
|
+
author.rated scope: category_1
|
226
228
|
# { value: 3, scopeable: category_1 }
|
227
229
|
```
|
228
230
|
|
@@ -253,19 +255,19 @@ Maybe you want to recover all records with or without scope, so you can add the
|
|
253
255
|
```ruby
|
254
256
|
category_1 = Category.first
|
255
257
|
category_2 = Category.second
|
256
|
-
author =
|
258
|
+
author = Author.last
|
257
259
|
resource = Article.last
|
258
260
|
|
259
261
|
author.rate resource, 1
|
260
|
-
author.rate resource, 3,
|
261
|
-
author.rate resource, 5,
|
262
|
+
author.rate resource, 3, scope: category_1
|
263
|
+
author.rate resource, 5, scope: category_2
|
262
264
|
|
263
265
|
author.rating_records
|
264
266
|
# { average: 1, estimate: 1, scopeable: nil , sum: 1, total: 1 },
|
265
267
|
# { average: 3, estimate: 3, scopeable: category_1, sum: 3, total: 1 },
|
266
268
|
# { average: 5, estimate: 5, scopeable: category_2, sum: 5, total: 1 }
|
267
269
|
|
268
|
-
|
270
|
+
author.rated_records
|
269
271
|
# { value: 1 }, { value: 3, scopeable: category_1 }, { value: 5, scopeable: category_2 }
|
270
272
|
|
271
273
|
article.rates_records
|
@@ -275,12 +277,50 @@ article.rates_records
|
|
275
277
|
### As
|
276
278
|
|
277
279
|
If you have a model that will only be able to rate but not to receive a rate, configure it as `author`.
|
278
|
-
An author model still can be rated, but won't genarate a Rating record with all values as zero to
|
280
|
+
An author model still can be rated, but won't genarate a Rating record with all values as zero to warm up the cache.
|
279
281
|
|
280
282
|
```ruby
|
281
283
|
rating as: :author
|
282
284
|
```
|
283
285
|
|
286
|
+
### Metadata
|
287
|
+
|
288
|
+
Maybe you want include a `comment` together your rating or even a `fingerprint` field to make your rating more secure.
|
289
|
+
So, first you will need to add more fields to the `Rating::Rate` table:
|
290
|
+
|
291
|
+
```ruby
|
292
|
+
class AddCommentAndFingerprintOnRatingRates < ActiveRecord::Migration
|
293
|
+
def change
|
294
|
+
add_column :rating_rates, :comment, :text
|
295
|
+
|
296
|
+
add_reference :rating_rates, :fingerprint, foreign_key: true, index: true, null: false
|
297
|
+
end
|
298
|
+
end
|
299
|
+
```
|
300
|
+
|
301
|
+
As you can seed, we can add any kind of field we want. Now we just provide this values when we make the rate:
|
302
|
+
|
303
|
+
```ruby
|
304
|
+
author = Author.last
|
305
|
+
resource = Article.last
|
306
|
+
comment = 'This is a very nice rating. s2'
|
307
|
+
fingerprint = Fingerprint.new(ip: '127.0.0.1')
|
308
|
+
|
309
|
+
author.rate resource, 3, metadata: { comment: comment, fingerprint: fingerprint }
|
310
|
+
```
|
311
|
+
|
312
|
+
Now you can have this data into your model normally:
|
313
|
+
|
314
|
+
|
315
|
+
```ruby
|
316
|
+
author = Author.last
|
317
|
+
rate = author.rates.last
|
318
|
+
|
319
|
+
rate.comment # 'This is a very nice rating. s2'
|
320
|
+
rate.fingerprint # <Fingerprint id:...>
|
321
|
+
rate.value # 3
|
322
|
+
```
|
323
|
+
|
284
324
|
## Love it!
|
285
325
|
|
286
|
-
Via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=X8HEP2878NDEG&item_name=rating) or [
|
326
|
+
Via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=X8HEP2878NDEG&item_name=rating) or [Support](https://liberapay.com/wbotelhos). Thanks! (:
|
@@ -5,9 +5,9 @@ class CreateRatingTables < ActiveRecord::Migration[5.0]
|
|
5
5
|
create_table :rating_rates do |t|
|
6
6
|
t.decimal :value, default: 0, precision: 17, scale: 14
|
7
7
|
|
8
|
-
t.references :author
|
9
|
-
t.references :resource
|
10
|
-
t.references :scopeable, index: true, null: true
|
8
|
+
t.references :author, index: true, null: false, polymorphic: true
|
9
|
+
t.references :resource, index: true, null: false, polymorphic: true
|
10
|
+
t.references :scopeable, index: true, null: true, polymorphic: true
|
11
11
|
|
12
12
|
t.timestamps null: false
|
13
13
|
end
|
@@ -17,13 +17,13 @@ class CreateRatingTables < ActiveRecord::Migration[5.0]
|
|
17
17
|
unique: true
|
18
18
|
|
19
19
|
create_table :rating_ratings do |t|
|
20
|
-
t.decimal :average
|
20
|
+
t.decimal :average, default: 0, mull: false, precision: 17, scale: 14
|
21
21
|
t.decimal :estimate, default: 0, mull: false, precision: 17, scale: 14
|
22
|
-
t.integer :sum
|
23
|
-
t.integer :total
|
22
|
+
t.integer :sum, default: 0, mull: false
|
23
|
+
t.integer :total, default: 0, mull: false
|
24
24
|
|
25
|
-
t.references :resource
|
26
|
-
t.references :scopeable, index: true, null: true
|
25
|
+
t.references :resource, index: true, null: false, polymorphic: true
|
26
|
+
t.references :scopeable, index: true, null: true, polymorphic: true
|
27
27
|
|
28
28
|
t.timestamps null: false
|
29
29
|
end
|
@@ -5,8 +5,8 @@ module Rating
|
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
included do
|
8
|
-
def rate(resource, value, author: self, scope: nil)
|
9
|
-
Rate.create author: author, resource: resource, scopeable: scope, value: value
|
8
|
+
def rate(resource, value, author: self, metadata: {}, scope: nil)
|
9
|
+
Rate.create author: author, metadata: metadata, resource: resource, scopeable: scope, value: value
|
10
10
|
end
|
11
11
|
|
12
12
|
def rate_for(resource, scope: nil)
|
@@ -6,8 +6,8 @@ module Rating
|
|
6
6
|
|
7
7
|
after_save :update_rating
|
8
8
|
|
9
|
-
belongs_to :author
|
10
|
-
belongs_to :resource
|
9
|
+
belongs_to :author, polymorphic: true
|
10
|
+
belongs_to :resource, polymorphic: true
|
11
11
|
belongs_to :scopeable, polymorphic: true
|
12
12
|
|
13
13
|
validates :author, :resource, :value, presence: true
|
@@ -18,11 +18,13 @@ module Rating
|
|
18
18
|
scope: %i[author_type resource_id resource_type scopeable_id scopeable_type]
|
19
19
|
}
|
20
20
|
|
21
|
-
def self.create(author:, resource:, scopeable: nil, value:)
|
21
|
+
def self.create(author:, metadata:, resource:, scopeable: nil, value:)
|
22
22
|
record = find_or_initialize_by(author: author, resource: resource, scopeable: scopeable)
|
23
23
|
|
24
24
|
return record if record.persisted? && value == record.value
|
25
25
|
|
26
|
+
metadata.each { |k, v| record[k] = v } if metadata.present?
|
27
|
+
|
26
28
|
record.value = value
|
27
29
|
record.save
|
28
30
|
|
@@ -4,16 +4,21 @@ module Rating
|
|
4
4
|
class Rating < ActiveRecord::Base
|
5
5
|
self.table_name = 'rating_ratings'
|
6
6
|
|
7
|
-
belongs_to :resource
|
7
|
+
belongs_to :resource, polymorphic: true
|
8
8
|
belongs_to :scopeable, polymorphic: true
|
9
9
|
|
10
10
|
validates :average, :estimate, :resource, :sum, :total, presence: true
|
11
11
|
validates :average, :estimate, :sum, :total, numericality: true
|
12
12
|
|
13
|
+
validates :resource_id, uniqueness: {
|
14
|
+
case_sensitive: false,
|
15
|
+
scope: %i[resource_type scopeable_id scopeable_type]
|
16
|
+
}
|
17
|
+
|
13
18
|
class << self
|
14
19
|
def averager_data(resource, scopeable)
|
15
20
|
total_count = how_many_resource_received_votes_sql?(distinct: false, scopeable: scopeable)
|
16
|
-
distinct_count = how_many_resource_received_votes_sql?(distinct: true
|
21
|
+
distinct_count = how_many_resource_received_votes_sql?(distinct: true, scopeable: scopeable)
|
17
22
|
values = { resource_type: resource.class.base_class.name }
|
18
23
|
|
19
24
|
values[:scopeable_type] = scopeable.class.base_class.name unless scopeable.nil?
|
@@ -23,7 +28,7 @@ module Rating
|
|
23
28
|
(#{total_count} / CAST(#{distinct_count} AS float)) count_avg,
|
24
29
|
COALESCE(AVG(value), 0) rating_avg
|
25
30
|
FROM #{rate_table_name}
|
26
|
-
WHERE resource_type = :resource_type
|
31
|
+
WHERE resource_type = :resource_type AND #{scope_type_query(scopeable)}
|
27
32
|
).squish
|
28
33
|
|
29
34
|
execute_sql [sql, values]
|
@@ -43,9 +48,9 @@ module Rating
|
|
43
48
|
|
44
49
|
def values_data(resource, scopeable)
|
45
50
|
scope_query = if scopeable.nil?
|
46
|
-
'scopeable_type is NULL
|
51
|
+
'scopeable_type is NULL AND scopeable_id is NULL'
|
47
52
|
else
|
48
|
-
'scopeable_type = ?
|
53
|
+
'scopeable_type = ? AND scopeable_id = ?'
|
49
54
|
end
|
50
55
|
|
51
56
|
sql = %(
|
@@ -54,10 +59,10 @@ module Rating
|
|
54
59
|
COALESCE(SUM(value), 0) rating_sum,
|
55
60
|
COUNT(1) rating_count
|
56
61
|
FROM #{rate_table_name}
|
57
|
-
WHERE resource_type = ?
|
62
|
+
WHERE resource_type = ? AND resource_id = ? AND #{scope_query}
|
58
63
|
).squish
|
59
64
|
|
60
|
-
values =
|
65
|
+
values = [sql, resource.class.base_class.name, resource.id]
|
61
66
|
values += [scopeable.class.base_class.name, scopeable.id] unless scopeable.nil?
|
62
67
|
|
63
68
|
execute_sql values
|
@@ -97,7 +102,7 @@ module Rating
|
|
97
102
|
%((
|
98
103
|
SELECT #{count}
|
99
104
|
FROM #{rate_table_name}
|
100
|
-
WHERE resource_type = :resource_type
|
105
|
+
WHERE resource_type = :resource_type AND #{scope_type_query(scopeable)}
|
101
106
|
))
|
102
107
|
end
|
103
108
|
|
data/lib/rating/version.rb
CHANGED
@@ -3,22 +3,22 @@
|
|
3
3
|
require 'rails_helper'
|
4
4
|
|
5
5
|
RSpec.describe Rating::Extension, ':after_create' do
|
6
|
-
context '
|
7
|
-
let!(:
|
6
|
+
context 'when :as is nil' do
|
7
|
+
let!(:article) { create :article }
|
8
8
|
|
9
9
|
it 'creates a rating record with zero values just to be easy to make the count' do
|
10
|
-
rating = Rating::Rating.find_by(resource:
|
10
|
+
rating = Rating::Rating.find_by(resource: article)
|
11
11
|
|
12
12
|
expect(rating.average).to eq 0
|
13
13
|
expect(rating.estimate).to eq 0
|
14
|
-
expect(rating.resource).to eq
|
14
|
+
expect(rating.resource).to eq article
|
15
15
|
expect(rating.scopeable).to eq nil
|
16
16
|
expect(rating.sum).to eq 0
|
17
17
|
expect(rating.total).to eq 0
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
context '
|
21
|
+
context 'when :as is :author' do
|
22
22
|
let!(:author) { create :author }
|
23
23
|
|
24
24
|
it 'does not creates a rating record' do
|
@@ -1,26 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'rails_helper'
|
4
|
+
require 'support/shared_context/with_database_records'
|
4
5
|
|
5
6
|
RSpec.describe Rating::Extension, ':order_by_rating' do
|
6
|
-
|
7
|
-
|
8
|
-
let!(:user_1) { create :user }
|
9
|
-
let!(:user_2) { create :user }
|
10
|
-
|
11
|
-
let!(:article_1) { create :article }
|
12
|
-
let!(:article_2) { create :article }
|
13
|
-
let!(:article_3) { create :article }
|
14
|
-
|
15
|
-
before do
|
16
|
-
create :rating_rate, author: user_1, resource: article_1, value: 100
|
17
|
-
create :rating_rate, author: user_1, resource: article_2, value: 11
|
18
|
-
create :rating_rate, author: user_1, resource: article_3, value: 10
|
19
|
-
create :rating_rate, author: user_2, resource: article_1, value: 1
|
20
|
-
|
21
|
-
create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1
|
22
|
-
create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2
|
23
|
-
end
|
7
|
+
include_context 'with_database_records'
|
24
8
|
|
25
9
|
context 'with default filters' do
|
26
10
|
it 'sorts by :estimate :desc' do
|
@@ -32,8 +16,8 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
|
|
32
16
|
end
|
33
17
|
end
|
34
18
|
|
35
|
-
context 'filtering by :average' do
|
36
|
-
context 'as asc' do
|
19
|
+
context 'when filtering by :average' do
|
20
|
+
context 'with as asc' do
|
37
21
|
it 'works' do
|
38
22
|
expect(Article.order_by_rating(:average, :asc)).to eq [
|
39
23
|
article_3,
|
@@ -51,7 +35,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
|
|
51
35
|
end
|
52
36
|
end
|
53
37
|
|
54
|
-
context 'as desc' do
|
38
|
+
context 'with as desc' do
|
55
39
|
it 'works' do
|
56
40
|
expect(Article.order_by_rating(:average, :desc)).to eq [
|
57
41
|
article_1,
|
@@ -70,8 +54,8 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
|
|
70
54
|
end
|
71
55
|
end
|
72
56
|
|
73
|
-
context 'filtering by :estimate' do
|
74
|
-
context 'as asc' do
|
57
|
+
context 'when filtering by :estimate' do
|
58
|
+
context 'with as asc' do
|
75
59
|
it 'works' do
|
76
60
|
expect(Article.order_by_rating(:estimate, :asc)).to eq [
|
77
61
|
article_3,
|
@@ -89,7 +73,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
|
|
89
73
|
end
|
90
74
|
end
|
91
75
|
|
92
|
-
context 'as desc' do
|
76
|
+
context 'with as desc' do
|
93
77
|
it 'works' do
|
94
78
|
expect(Article.order_by_rating(:estimate, :desc)).to eq [
|
95
79
|
article_1,
|
@@ -108,7 +92,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
|
|
108
92
|
end
|
109
93
|
end
|
110
94
|
|
111
|
-
context 'filtering by :sum' do
|
95
|
+
context 'when filtering by :sum' do
|
112
96
|
context 'as asc' do
|
113
97
|
it 'works' do
|
114
98
|
expect(Article.order_by_rating(:sum, :asc)).to eq [
|
@@ -127,7 +111,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
|
|
127
111
|
end
|
128
112
|
end
|
129
113
|
|
130
|
-
context 'as desc' do
|
114
|
+
context 'with as desc' do
|
131
115
|
it 'works' do
|
132
116
|
expect(Article.order_by_rating(:sum, :desc)).to eq [
|
133
117
|
article_1,
|
@@ -144,39 +128,39 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
|
|
144
128
|
end
|
145
129
|
end
|
146
130
|
end
|
131
|
+
end
|
147
132
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
133
|
+
context 'when filtering by :total' do
|
134
|
+
context 'with as asc' do
|
135
|
+
it 'works' do
|
136
|
+
result = Article.order_by_rating(:total, :asc)
|
152
137
|
|
153
|
-
|
154
|
-
|
155
|
-
|
138
|
+
expect(result[0..1]).to match_array [article_2, article_3]
|
139
|
+
expect(result.last).to eq article_1
|
140
|
+
end
|
156
141
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
end
|
142
|
+
context 'with scope' do
|
143
|
+
it 'works' do
|
144
|
+
expect(Article.order_by_rating(:total, :asc, scope: category)).to eq [
|
145
|
+
article_1
|
146
|
+
]
|
163
147
|
end
|
164
148
|
end
|
149
|
+
end
|
165
150
|
|
166
|
-
|
167
|
-
|
168
|
-
|
151
|
+
context 'with as desc' do
|
152
|
+
it 'works' do
|
153
|
+
result = Article.order_by_rating(:total, :desc)
|
169
154
|
|
170
|
-
|
171
|
-
|
172
|
-
|
155
|
+
expect(result.first).to eq article_1
|
156
|
+
expect(result[1..2]).to match_array [article_2, article_3]
|
157
|
+
end
|
173
158
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
end
|
159
|
+
context 'with scope' do
|
160
|
+
it 'works' do
|
161
|
+
expect(Article.order_by_rating(:total, :desc, scope: category)).to eq [
|
162
|
+
article_1
|
163
|
+
]
|
180
164
|
end
|
181
165
|
end
|
182
166
|
end
|
@@ -184,12 +168,12 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
|
|
184
168
|
|
185
169
|
context 'with other resource' do
|
186
170
|
it 'works' do
|
187
|
-
expect(
|
171
|
+
expect(Author.order_by_rating(:total, :desc)).to match_array [author_1, author_2]
|
188
172
|
end
|
189
173
|
|
190
174
|
context 'with scope' do
|
191
175
|
it 'returns empty since creation has no scope' do
|
192
|
-
expect(
|
176
|
+
expect(Author.order_by_rating(:total, :desc, scope: category)).to eq []
|
193
177
|
end
|
194
178
|
end
|
195
179
|
end
|