rating 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/README.md +21 -9
- data/lib/rating/models/rating/rating.rb +23 -3
- data/lib/rating/version.rb +1 -1
- data/spec/factories/toy.rb +6 -0
- data/spec/models/extension/unscoped_rating_spec.rb +1 -24
- data/spec/models/extension/where_spec.rb +33 -0
- data/spec/support/db/migrate/create_toys_table.rb +8 -0
- data/spec/support/migrate.rb +1 -0
- data/spec/support/models/toy.rb +5 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf4f916eaf8978771365715fff38db48916a423e061a4fe680cc105d48d3558b
|
4
|
+
data.tar.gz: 409e9990a8171000cbd898744f6859358f75e302f0d169287ba29c94fc293758
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c06d75fec9d344f0cca88d6f250d23b4d3612539f2c12d10d6db3c56b86349f9552e3036a34c065d8ccbe9b5e13f00c2ab34f70167d6d16e7ca2d3f90868351
|
7
|
+
data.tar.gz: c3788409c80bc0a8c9b17df8d40720e35f225a0e8dd5ed6c136260c821f176dd8caa87809dcc68e3b50f892d58d9863f7248c554ea8a69832b05f0cb16aa1249
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
## v0.10.0
|
2
|
+
|
3
|
+
### News
|
4
|
+
|
5
|
+
- Add option `where` to be able adds conditions on `Rating::Rate`.
|
6
|
+
|
1
7
|
## v0.9.0
|
2
8
|
|
3
9
|
### News
|
@@ -7,7 +13,7 @@
|
|
7
13
|
|
8
14
|
### Updates
|
9
15
|
|
10
|
-
- Update
|
16
|
+
- Update Ruby to 2.6.5;
|
11
17
|
- Update Activerecord to the last version;
|
12
18
|
|
13
19
|
## v0.8.0
|
data/README.md
CHANGED
@@ -138,24 +138,20 @@ author.rated? resource
|
|
138
138
|
|
139
139
|
### rates
|
140
140
|
|
141
|
-
|
141
|
+
All rating received.
|
142
142
|
|
143
143
|
```ruby
|
144
|
-
|
145
|
-
|
146
|
-
resource.rates
|
144
|
+
Article.first.rates
|
147
145
|
```
|
148
146
|
|
149
147
|
It will return a collection of `Rate` object.
|
150
148
|
|
151
149
|
### rated
|
152
150
|
|
153
|
-
|
151
|
+
All rating given.
|
154
152
|
|
155
153
|
```ruby
|
156
|
-
|
157
|
-
|
158
|
-
author.rated
|
154
|
+
Author.first.rated
|
159
155
|
```
|
160
156
|
|
161
157
|
It will return a collection of `Rate` object.
|
@@ -349,7 +345,7 @@ If you need to warm up a record with scope, you need to setup the `scoping` rela
|
|
349
345
|
|
350
346
|
```ruby
|
351
347
|
class Resource < ApplicationRecord
|
352
|
-
|
348
|
+
rating scoping: :categories
|
353
349
|
end
|
354
350
|
```
|
355
351
|
|
@@ -410,6 +406,22 @@ author.rate resource, 3
|
|
410
406
|
|
411
407
|
Now the `sum` will be `6` and the `total` will be `3` because all rating will be calculated into just one rating record ignoring the `scopeable` object. The rating record is always saved on the record with `scopeable` as `nil`.
|
412
408
|
|
409
|
+
### where
|
410
|
+
|
411
|
+
The `where` option can be used to filter the `Rating::Rate` records used to create the final `Rating::Rating`. You can filter only approved rates, for exemplo:
|
412
|
+
|
413
|
+
```ruby
|
414
|
+
rating where: 'approved = true'
|
415
|
+
|
416
|
+
author = User.last
|
417
|
+
resource = Article.last
|
418
|
+
|
419
|
+
author.rate resource, 1, extra_scope: { approved: false }
|
420
|
+
author.rate resource, 5, extra_scope: { approved: true }
|
421
|
+
```
|
422
|
+
|
423
|
+
As you can see, now, only the rate with value `5` will be included on the final rating.
|
424
|
+
|
413
425
|
### References
|
414
426
|
|
415
427
|
- [Evan Miller](http://www.evanmiller.org/ranking-items-with-star-ratings.html)
|
@@ -29,7 +29,10 @@ module Rating
|
|
29
29
|
(CAST(#{total_count} AS DECIMAL(17, 14)) / #{distinct_count}) count_avg,
|
30
30
|
COALESCE(AVG(value), 0) rating_avg
|
31
31
|
FROM #{rate_table_name}
|
32
|
-
WHERE
|
32
|
+
WHERE
|
33
|
+
resource_type = :resource_type
|
34
|
+
#{scope_type_query(resource, scopeable)}
|
35
|
+
#{scope_where_query(resource)}
|
33
36
|
).squish
|
34
37
|
|
35
38
|
execute_sql [sql, values]
|
@@ -54,7 +57,11 @@ module Rating
|
|
54
57
|
COALESCE(SUM(value), 0) rating_sum,
|
55
58
|
COUNT(1) rating_count
|
56
59
|
FROM #{rate_table_name}
|
57
|
-
WHERE
|
60
|
+
WHERE
|
61
|
+
resource_type = ?
|
62
|
+
AND resource_id = ?
|
63
|
+
#{scope_type_and_id_query(resource, scopeable)}
|
64
|
+
#{scope_where_query(resource)}
|
58
65
|
).squish
|
59
66
|
|
60
67
|
values = [sql, resource.class.base_class.name, resource.id]
|
@@ -104,7 +111,10 @@ module Rating
|
|
104
111
|
%((
|
105
112
|
SELECT GREATEST(#{count}, 1)
|
106
113
|
FROM #{rate_table_name}
|
107
|
-
WHERE
|
114
|
+
WHERE
|
115
|
+
resource_type = :resource_type
|
116
|
+
#{scope_type_query(resource, scopeable)}
|
117
|
+
#{scope_where_query(resource)}
|
108
118
|
))
|
109
119
|
end
|
110
120
|
|
@@ -124,6 +134,16 @@ module Rating
|
|
124
134
|
|
125
135
|
'AND scopeable_type = ? AND scopeable_id = ?'
|
126
136
|
end
|
137
|
+
|
138
|
+
def scope_where_query(resource)
|
139
|
+
return '' if where_condition(resource).blank?
|
140
|
+
|
141
|
+
"AND #{where_condition(resource)}"
|
142
|
+
end
|
143
|
+
|
144
|
+
def where_condition(resource)
|
145
|
+
resource.rating_options[:where]
|
146
|
+
end
|
127
147
|
end
|
128
148
|
end
|
129
149
|
end
|
data/lib/rating/version.rb
CHANGED
@@ -62,30 +62,7 @@ RSpec.describe Rating::Extension, 'unscoped_rating' do
|
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
-
context 'when is true' do
|
66
|
-
let!(:resource) { create :global }
|
67
|
-
|
68
|
-
it 'groups in the same line record' do
|
69
|
-
author_1.rate resource, 1, scope: scope
|
70
|
-
author_2.rate resource, 2, scope: scope
|
71
|
-
author_1.rate resource, 5
|
72
|
-
|
73
|
-
ratings = Rating::Rating.all.order('id')
|
74
|
-
|
75
|
-
expect(ratings.size).to eq 1
|
76
|
-
|
77
|
-
rating = ratings[0]
|
78
|
-
|
79
|
-
expect(rating.average.to_s).to eq '2.6666666666666667'
|
80
|
-
expect(rating.estimate.to_s).to eq '2.6666666666666667'
|
81
|
-
expect(rating.resource).to eq resource
|
82
|
-
expect(rating.scopeable).to eq nil
|
83
|
-
expect(rating.sum).to eq 8
|
84
|
-
expect(rating.total).to eq 3
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
context 'when is true and have a non scopeable record first on dabase' do
|
65
|
+
context 'when is true and have a non scopeable record first on database' do
|
89
66
|
let!(:resource) { create :global }
|
90
67
|
|
91
68
|
before { ::Rating::Rating.create resource: resource, scopeable: scope }
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails_helper'
|
4
|
+
|
5
|
+
RSpec.describe Rating::Extension, 'unscoped_rating' do
|
6
|
+
let!(:author_1) { create :author }
|
7
|
+
let!(:author_2) { create :author }
|
8
|
+
let!(:author_3) { create :author }
|
9
|
+
let!(:author_4) { create :author }
|
10
|
+
let!(:author_5) { create :author }
|
11
|
+
let!(:resource) { create :toy }
|
12
|
+
|
13
|
+
it 'uses rate with where condition' do
|
14
|
+
author_1.rate resource, 1
|
15
|
+
author_2.rate resource, 2
|
16
|
+
author_3.rate resource, 3
|
17
|
+
author_4.rate resource, 4
|
18
|
+
author_5.rate resource, 5
|
19
|
+
|
20
|
+
ratings = Rating::Rating.all.order('id')
|
21
|
+
|
22
|
+
expect(ratings.size).to eq 1
|
23
|
+
|
24
|
+
rating = ratings[0]
|
25
|
+
|
26
|
+
expect(rating.average.to_s).to eq '3.0'
|
27
|
+
expect(rating.estimate.to_s).to eq '3.0'
|
28
|
+
expect(rating.resource).to eq resource
|
29
|
+
expect(rating.scopeable).to eq nil
|
30
|
+
expect(rating.sum).to eq 9
|
31
|
+
expect(rating.total).to eq 3
|
32
|
+
end
|
33
|
+
end
|
data/spec/support/migrate.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rating
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Washington Botelho
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -164,6 +164,7 @@ files:
|
|
164
164
|
- spec/factories/global.rb
|
165
165
|
- spec/factories/rating/rate.rb
|
166
166
|
- spec/factories/rating/rating.rb
|
167
|
+
- spec/factories/toy.rb
|
167
168
|
- spec/models/extension/after_create_spec.rb
|
168
169
|
- spec/models/extension/order_by_rating_spec.rb
|
169
170
|
- spec/models/extension/rate_for_spec.rb
|
@@ -177,6 +178,7 @@ files:
|
|
177
178
|
- spec/models/extension/rating_spec.rb
|
178
179
|
- spec/models/extension/rating_warm_up_spec.rb
|
179
180
|
- spec/models/extension/unscoped_rating_spec.rb
|
181
|
+
- spec/models/extension/where_spec.rb
|
180
182
|
- spec/models/rate/create_spec.rb
|
181
183
|
- spec/models/rate/rate_for_spec.rb
|
182
184
|
- spec/models/rate_spec.rb
|
@@ -199,6 +201,7 @@ files:
|
|
199
201
|
- spec/support/db/migrate/create_rating_table.rb
|
200
202
|
- spec/support/db/migrate/create_review_ratings_table.rb
|
201
203
|
- spec/support/db/migrate/create_reviews_table.rb
|
204
|
+
- spec/support/db/migrate/create_toys_table.rb
|
202
205
|
- spec/support/factory_bot.rb
|
203
206
|
- spec/support/migrate.rb
|
204
207
|
- spec/support/models/article.rb
|
@@ -208,6 +211,7 @@ files:
|
|
208
211
|
- spec/support/models/global.rb
|
209
212
|
- spec/support/models/review.rb
|
210
213
|
- spec/support/models/review_rating.rb
|
214
|
+
- spec/support/models/toy.rb
|
211
215
|
- spec/support/shared_context/with_database_records.rb
|
212
216
|
- spec/support/shoulda.rb
|
213
217
|
homepage: https://github.com/wbotelhos/rating
|
@@ -249,6 +253,7 @@ test_files:
|
|
249
253
|
- spec/models/extension/rating_warm_up_spec.rb
|
250
254
|
- spec/models/extension/order_by_rating_spec.rb
|
251
255
|
- spec/models/extension/unscoped_rating_spec.rb
|
256
|
+
- spec/models/extension/where_spec.rb
|
252
257
|
- spec/models/extension/rate_spec.rb
|
253
258
|
- spec/models/extension/rate_for_spec.rb
|
254
259
|
- spec/models/extension/after_create_spec.rb
|
@@ -263,6 +268,7 @@ test_files:
|
|
263
268
|
- spec/support/factory_bot.rb
|
264
269
|
- spec/support/models/article.rb
|
265
270
|
- spec/support/models/category.rb
|
271
|
+
- spec/support/models/toy.rb
|
266
272
|
- spec/support/models/author.rb
|
267
273
|
- spec/support/models/review.rb
|
268
274
|
- spec/support/models/comment.rb
|
@@ -274,6 +280,7 @@ test_files:
|
|
274
280
|
- spec/support/db/migrate/create_review_ratings_table.rb
|
275
281
|
- spec/support/db/migrate/create_articles_table.rb
|
276
282
|
- spec/support/db/migrate/create_rating_table.rb
|
283
|
+
- spec/support/db/migrate/create_toys_table.rb
|
277
284
|
- spec/support/db/migrate/add_comment_on_rating_rates_table.rb
|
278
285
|
- spec/support/db/migrate/add_extra_fields_on_rating_rates_table.rb
|
279
286
|
- spec/support/db/migrate/create_reviews_table.rb
|
@@ -286,6 +293,7 @@ test_files:
|
|
286
293
|
- spec/factories/category.rb
|
287
294
|
- spec/factories/rating/rate.rb
|
288
295
|
- spec/factories/rating/rating.rb
|
296
|
+
- spec/factories/toy.rb
|
289
297
|
- spec/factories/author.rb
|
290
298
|
- spec/factories/comment.rb
|
291
299
|
- spec/factories/global.rb
|