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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/README.md +64 -24
  4. data/lib/generators/rating/templates/db/migrate/create_rating_tables.rb +8 -8
  5. data/lib/rating/models/rating/extension.rb +2 -2
  6. data/lib/rating/models/rating/rate.rb +5 -3
  7. data/lib/rating/models/rating/rating.rb +13 -8
  8. data/lib/rating/version.rb +1 -1
  9. data/spec/factories/rating/rate.rb +1 -1
  10. data/spec/factories/rating/rating.rb +1 -1
  11. data/spec/models/extension/after_create_spec.rb +5 -5
  12. data/spec/models/extension/order_by_rating_spec.rb +37 -53
  13. data/spec/models/extension/rate_for_spec.rb +5 -5
  14. data/spec/models/extension/rate_spec.rb +31 -5
  15. data/spec/models/extension/rated_question_spec.rb +9 -9
  16. data/spec/models/extension/rated_records_spec.rb +3 -17
  17. data/spec/models/extension/rated_spec.rb +9 -27
  18. data/spec/models/extension/rates_records_spec.rb +2 -16
  19. data/spec/models/extension/rates_spec.rb +5 -19
  20. data/spec/models/extension/rating_records_spec.rb +2 -18
  21. data/spec/models/extension/rating_spec.rb +3 -17
  22. data/spec/models/rate/create_spec.rb +56 -15
  23. data/spec/models/rate/rate_for_spec.rb +7 -7
  24. data/spec/models/rate_spec.rb +2 -2
  25. data/spec/models/rating/averager_data_spec.rb +8 -24
  26. data/spec/models/rating/data_spec.rb +12 -28
  27. data/spec/models/rating/update_rating_spec.rb +2 -18
  28. data/spec/models/rating/values_data_spec.rb +10 -26
  29. data/spec/models/rating_spec.rb +6 -0
  30. data/spec/support/db/migrate/add_comment_on_rating_rates_table.rb +7 -0
  31. data/spec/support/db/migrate/{create_category_spec.rb → create_categories_table.rb} +0 -0
  32. data/spec/support/migrate.rb +3 -1
  33. data/spec/support/shared_context/with_database_records.rb +20 -0
  34. metadata +35 -37
  35. data/spec/factories/user.rb +0 -7
  36. data/spec/support/db/migrate/create_users_table.rb +0 -9
  37. data/spec/support/models/user.rb +0 -5
@@ -3,19 +3,19 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Rating::Rate, ':rate_for' do
6
- let!(:user) { create :user }
6
+ let!(:author) { create :author }
7
7
  let!(:article) { create :article }
8
8
 
9
9
  context 'with no scopeable' do
10
10
  context 'when rate does not exist' do
11
- specify { expect(described_class.rate_for(author: user, resource: article)).to eq nil }
11
+ specify { expect(described_class.rate_for(author: author, resource: article)).to eq nil }
12
12
  end
13
13
 
14
14
  context 'when rate does not exist' do
15
- before { described_class.create author: user, resource: article, value: 3 }
15
+ before { described_class.create author: author, metadata: {}, resource: article, value: 3 }
16
16
 
17
17
  it 'returns the record' do
18
- expect(described_class.rate_for(author: user, resource: article)).to eq described_class.last
18
+ expect(described_class.rate_for(author: author, resource: article)).to eq described_class.last
19
19
  end
20
20
  end
21
21
  end
@@ -24,14 +24,14 @@ RSpec.describe Rating::Rate, ':rate_for' do
24
24
  let!(:category) { create :category }
25
25
 
26
26
  context 'when rate does not exist' do
27
- specify { expect(described_class.rate_for(author: user, resource: article, scopeable: category)).to eq nil }
27
+ specify { expect(described_class.rate_for(author: author, resource: article, scopeable: category)).to eq nil }
28
28
  end
29
29
 
30
30
  context 'when rate does not exist' do
31
- before { described_class.create author: user, resource: article, scopeable: category, value: 3 }
31
+ before { described_class.create author: author, metadata: {}, resource: article, scopeable: category, value: 3 }
32
32
 
33
33
  it 'returns the record' do
34
- query = described_class.rate_for(author: user, resource: article, scopeable: category)
34
+ query = described_class.rate_for(author: author, resource: article, scopeable: category)
35
35
 
36
36
  expect(query).to eq described_class.last
37
37
  end
@@ -15,11 +15,11 @@ RSpec.describe Rating::Rate do
15
15
  it { is_expected.to validate_presence_of :resource }
16
16
  it { is_expected.to validate_presence_of :value }
17
17
 
18
- specify do
18
+ it do
19
19
  is_expected.to validate_numericality_of(:value).is_less_than_or_equal_to(100).is_less_than_or_equal_to 100
20
20
  end
21
21
 
22
- specify do
22
+ it do
23
23
  expect(object).to validate_uniqueness_of(:author_id)
24
24
  .scoped_to(%i[author_type resource_id resource_type scopeable_id scopeable_type])
25
25
  .case_insensitive
@@ -1,48 +1,32 @@
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::Rating, ':averager_data' do
6
- let!(:category) { create :category }
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 no scopeable' do
26
- subject { described_class.averager_data article_1, nil }
10
+ subject(:result) { described_class.averager_data article_1, nil }
27
11
 
28
12
  it 'returns the values average of given resource type' do
29
- expect(subject.as_json['rating_avg']).to eq 30.5
13
+ expect(result.as_json['rating_avg']).to eq 30.5
30
14
  end
31
15
 
32
16
  it 'returns the average of number of records for the given resource type' do
33
- expect(subject.as_json['count_avg']).to eq 1.3333333333333333
17
+ expect(result.as_json['count_avg']).to eq 1.3333333333333333
34
18
  end
35
19
  end
36
20
 
37
21
  context 'with scopeable' do
38
- subject { described_class.averager_data article_1, category }
22
+ subject(:result) { described_class.averager_data article_1, category }
39
23
 
40
24
  it 'returns the values average of given resource type' do
41
- expect(subject.as_json['rating_avg']).to eq 1.5
25
+ expect(result.as_json['rating_avg']).to eq 1.5
42
26
  end
43
27
 
44
28
  it 'returns the average of number of records for the given resource type' do
45
- expect(subject.as_json['count_avg']).to eq 2
29
+ expect(result.as_json['count_avg']).to eq 2
46
30
  end
47
31
  end
48
32
  end
@@ -1,64 +1,48 @@
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::Rating, ':data' do
6
- let!(:category) { create :category }
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 no scopeable' do
26
- subject { described_class.data article_1, nil }
10
+ subject(:result) { described_class.data article_1, nil }
27
11
 
28
12
  it 'returns the average of value for a resource' do
29
- expect(subject[:average]).to eq 50.5
13
+ expect(result[:average]).to eq 50.5
30
14
  end
31
15
 
32
16
  it 'returns the sum of values for a resource' do
33
- expect(subject[:sum]).to eq 101
17
+ expect(result[:sum]).to eq 101
34
18
  end
35
19
 
36
20
  it 'returns the count of votes for a resource' do
37
- expect(subject[:total]).to eq 2
21
+ expect(result[:total]).to eq 2
38
22
  end
39
23
 
40
24
  it 'returns the estimate for a resource' do
41
- expect(subject[:estimate]).to eq 42.50000000000001
25
+ expect(result[:estimate]).to eq 42.50000000000001
42
26
  end
43
27
  end
44
28
 
45
29
  context 'with scopeable' do
46
- subject { described_class.data article_1, category }
30
+ subject(:result) { described_class.data article_1, category }
47
31
 
48
32
  it 'returns the average of value for a resource' do
49
- expect(subject[:average]).to eq 1.5
33
+ expect(result[:average]).to eq 1.5
50
34
  end
51
35
 
52
36
  it 'returns the sum of values for a resource' do
53
- expect(subject[:sum]).to eq 3
37
+ expect(result[:sum]).to eq 3
54
38
  end
55
39
 
56
40
  it 'returns the count of votes for a resource' do
57
- expect(subject[:total]).to eq 2
41
+ expect(result[:total]).to eq 2
58
42
  end
59
43
 
60
44
  it 'returns the estimate for a resource' do
61
- expect(subject[:estimate]).to eq 1.5
45
+ expect(result[:estimate]).to eq 1.5
62
46
  end
63
47
  end
64
48
  end
@@ -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::Rating, ':update_rating' do
6
- let!(:category) { create :category }
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 no scopeable' do
26
10
  it 'updates the rating data of the given resource' do
@@ -1,56 +1,40 @@
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::Rating, ':values_data' do
6
- let!(:category) { create :category }
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: 4
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 no scopeable' do
26
- subject { described_class.values_data article_1, nil }
10
+ subject(:result) { described_class.values_data article_1, nil }
27
11
 
28
12
  it 'returns the average of value for a resource' do
29
- expect(subject.as_json['rating_avg']).to eq 52.0
13
+ expect(result.as_json['rating_avg']).to eq 50.5
30
14
  end
31
15
 
32
16
  it 'returns the sum of values for a resource' do
33
- expect(subject.as_json['rating_sum']).to eq 104
17
+ expect(result.as_json['rating_sum']).to eq 101
34
18
  end
35
19
 
36
20
  it 'returns the count of votes for a resource' do
37
- expect(subject.as_json['rating_count']).to eq 2
21
+ expect(result.as_json['rating_count']).to eq 2
38
22
  end
39
23
  end
40
24
 
41
25
  context 'with scopeable' do
42
- subject { described_class.values_data article_1, category }
26
+ subject(:result) { described_class.values_data article_1, category }
43
27
 
44
28
  it 'returns the average of value for a resource' do
45
- expect(subject.as_json['rating_avg']).to eq 1.5
29
+ expect(result.as_json['rating_avg']).to eq 1.5
46
30
  end
47
31
 
48
32
  it 'returns the sum of values for a resource' do
49
- expect(subject.as_json['rating_sum']).to eq 3
33
+ expect(result.as_json['rating_sum']).to eq 3
50
34
  end
51
35
 
52
36
  it 'returns the count of votes for a resource' do
53
- expect(subject.as_json['rating_count']).to eq 2
37
+ expect(result.as_json['rating_count']).to eq 2
54
38
  end
55
39
  end
56
40
  end
@@ -15,4 +15,10 @@ RSpec.describe Rating::Rating do
15
15
  it { is_expected.to validate_presence_of :resource }
16
16
  it { is_expected.to validate_presence_of :sum }
17
17
  it { is_expected.to validate_presence_of :total }
18
+
19
+ it do
20
+ expect(object).to validate_uniqueness_of(:resource_id)
21
+ .scoped_to(%i[resource_type scopeable_id scopeable_type])
22
+ .case_insensitive
23
+ end
18
24
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddCommentOnRatingRatesTable < ActiveRecord::Migration[5.0]
4
+ def change
5
+ add_column :rating_rates, :comment, :text
6
+ end
7
+ end
@@ -2,8 +2,10 @@
2
2
 
3
3
  require File.expand_path('../../lib/generators/rating/templates/db/migrate/create_rating_tables.rb', __dir__)
4
4
 
5
+ Dir[File.expand_path('db/migrate/*.rb', __dir__)].each { |file| require file }
6
+
5
7
  CreateArticlesTable.new.change
6
8
  CreateAuthorsTable.new.change
7
9
  CreateCategoriesTable.new.change
8
10
  CreateRatingTables.new.change
9
- CreateUsersTable.new.change
11
+ AddCommentOnRatingRatesTable.new.change
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.shared_context 'with_database_records' do
4
+ let!(:category) { create :category }
5
+
6
+ let!(:author_1) { create :author }
7
+ let!(:author_2) { create :author }
8
+
9
+ let!(:article_1) { create :article }
10
+ let!(:article_2) { create :article }
11
+ let!(:article_3) { create :article }
12
+
13
+ let!(:rate_1) { create :rating_rate, author: author_1, resource: article_1, value: 100 }
14
+ let!(:rate_2) { create :rating_rate, author: author_1, resource: article_2, value: 11 }
15
+ let!(:rate_3) { create :rating_rate, author: author_1, resource: article_3, value: 10 }
16
+ let!(:rate_4) { create :rating_rate, author: author_2, resource: article_1, value: 1 }
17
+
18
+ let!(:rate_5) { create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1 }
19
+ let!(:rate_6) { create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2 }
20
+ end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rating
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.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: 2017-11-03 00:00:00.000000000 Z
11
+ date: 2018-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -149,7 +149,6 @@ files:
149
149
  - spec/factories/category.rb
150
150
  - spec/factories/rating/rate.rb
151
151
  - spec/factories/rating/rating.rb
152
- - spec/factories/user.rb
153
152
  - spec/models/extension/after_create_spec.rb
154
153
  - spec/models/extension/order_by_rating_spec.rb
155
154
  - spec/models/extension/rate_for_spec.rb
@@ -172,16 +171,16 @@ files:
172
171
  - spec/rails_helper.rb
173
172
  - spec/support/common.rb
174
173
  - spec/support/database_cleaner.rb
174
+ - spec/support/db/migrate/add_comment_on_rating_rates_table.rb
175
175
  - spec/support/db/migrate/create_articles_table.rb
176
176
  - spec/support/db/migrate/create_authors_table.rb
177
- - spec/support/db/migrate/create_category_spec.rb
178
- - spec/support/db/migrate/create_users_table.rb
177
+ - spec/support/db/migrate/create_categories_table.rb
179
178
  - spec/support/factory_bot.rb
180
179
  - spec/support/migrate.rb
181
180
  - spec/support/models/article.rb
182
181
  - spec/support/models/author.rb
183
182
  - spec/support/models/category.rb
184
- - spec/support/models/user.rb
183
+ - spec/support/shared_context/with_database_records.rb
185
184
  - spec/support/shoulda.rb
186
185
  homepage: https://github.com/wbotelhos/rating
187
186
  licenses:
@@ -208,42 +207,41 @@ signing_key:
208
207
  specification_version: 4
209
208
  summary: A true Bayesian rating system with scope and cache enabled.
210
209
  test_files:
211
- - spec/factories/article.rb
212
- - spec/factories/author.rb
213
- - spec/factories/category.rb
214
- - spec/factories/rating/rate.rb
215
- - spec/factories/rating/rating.rb
216
- - spec/factories/user.rb
217
- - spec/models/extension/after_create_spec.rb
218
- - spec/models/extension/order_by_rating_spec.rb
219
- - spec/models/extension/rate_for_spec.rb
220
- - spec/models/extension/rate_spec.rb
221
- - spec/models/extension/rated_question_spec.rb
222
- - spec/models/extension/rated_records_spec.rb
223
- - spec/models/extension/rated_spec.rb
224
- - spec/models/extension/rates_records_spec.rb
225
- - spec/models/extension/rates_spec.rb
226
- - spec/models/extension/rating_records_spec.rb
227
- - spec/models/extension/rating_spec.rb
228
- - spec/models/rate/create_spec.rb
229
210
  - spec/models/rate/rate_for_spec.rb
230
- - spec/models/rate_spec.rb
211
+ - spec/models/rate/create_spec.rb
231
212
  - spec/models/rating/averager_data_spec.rb
232
213
  - spec/models/rating/data_spec.rb
233
214
  - spec/models/rating/update_rating_spec.rb
234
215
  - spec/models/rating/values_data_spec.rb
216
+ - spec/models/extension/rated_records_spec.rb
217
+ - spec/models/extension/rating_spec.rb
218
+ - spec/models/extension/rated_question_spec.rb
219
+ - spec/models/extension/order_by_rating_spec.rb
220
+ - spec/models/extension/rate_spec.rb
221
+ - spec/models/extension/rate_for_spec.rb
222
+ - spec/models/extension/after_create_spec.rb
223
+ - spec/models/extension/rates_spec.rb
224
+ - spec/models/extension/rating_records_spec.rb
225
+ - spec/models/extension/rated_spec.rb
226
+ - spec/models/extension/rates_records_spec.rb
235
227
  - spec/models/rating_spec.rb
236
- - spec/rails_helper.rb
237
- - spec/support/common.rb
238
- - spec/support/database_cleaner.rb
239
- - spec/support/db/migrate/create_articles_table.rb
240
- - spec/support/db/migrate/create_authors_table.rb
241
- - spec/support/db/migrate/create_category_spec.rb
242
- - spec/support/db/migrate/create_users_table.rb
243
- - spec/support/factory_bot.rb
228
+ - spec/models/rate_spec.rb
244
229
  - spec/support/migrate.rb
230
+ - spec/support/shoulda.rb
231
+ - spec/support/factory_bot.rb
245
232
  - spec/support/models/article.rb
246
- - spec/support/models/author.rb
247
233
  - spec/support/models/category.rb
248
- - spec/support/models/user.rb
249
- - spec/support/shoulda.rb
234
+ - spec/support/models/author.rb
235
+ - spec/support/common.rb
236
+ - spec/support/db/migrate/create_categories_table.rb
237
+ - spec/support/db/migrate/create_authors_table.rb
238
+ - spec/support/db/migrate/create_articles_table.rb
239
+ - spec/support/db/migrate/add_comment_on_rating_rates_table.rb
240
+ - spec/support/database_cleaner.rb
241
+ - spec/support/shared_context/with_database_records.rb
242
+ - spec/factories/article.rb
243
+ - spec/factories/category.rb
244
+ - spec/factories/rating/rate.rb
245
+ - spec/factories/rating/rating.rb
246
+ - spec/factories/author.rb
247
+ - spec/rails_helper.rb