rating 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +7 -0
  3. data/README.md +111 -5
  4. data/lib/generators/rating/install_generator.rb +7 -3
  5. data/lib/generators/rating/templates/db/migrate/create_rating_tables.rb +10 -6
  6. data/lib/rating/models/rating/extension.rb +32 -13
  7. data/lib/rating/models/rating/rate.rb +9 -8
  8. data/lib/rating/models/rating/rating.rb +34 -17
  9. data/lib/rating/version.rb +1 -1
  10. data/spec/factories/article.rb +1 -1
  11. data/spec/factories/author.rb +7 -0
  12. data/spec/factories/category.rb +7 -0
  13. data/spec/factories/user.rb +1 -1
  14. data/spec/models/extension/after_create_spec.rb +16 -7
  15. data/spec/models/extension/order_by_rating_spec.rb +75 -0
  16. data/spec/models/extension/rate_for_spec.rb +15 -3
  17. data/spec/models/extension/rate_spec.rb +15 -3
  18. data/spec/models/extension/rated_question_spec.rb +24 -6
  19. data/spec/models/extension/rated_records_spec.rb +26 -0
  20. data/spec/models/extension/rated_spec.rb +33 -13
  21. data/spec/models/extension/rates_records_spec.rb +26 -0
  22. data/spec/models/extension/rates_spec.rb +33 -17
  23. data/spec/models/extension/rating_records_spec.rb +28 -0
  24. data/spec/models/extension/rating_spec.rb +31 -15
  25. data/spec/models/rate/create_spec.rb +104 -37
  26. data/spec/models/rate/rate_for_spec.rb +26 -6
  27. data/spec/models/rate_spec.rb +2 -1
  28. data/spec/models/rating/averager_data_spec.rb +24 -5
  29. data/spec/models/rating/data_spec.rb +38 -11
  30. data/spec/models/rating/update_rating_spec.rb +24 -6
  31. data/spec/models/rating/values_data_spec.rb +31 -8
  32. data/spec/models/rating_spec.rb +1 -0
  33. data/spec/support/db/migrate/create_authors_table.rb +9 -0
  34. data/spec/support/db/migrate/create_category_spec.rb +9 -0
  35. data/spec/support/migrate.rb +3 -1
  36. data/spec/support/models/author.rb +5 -0
  37. data/spec/support/models/category.rb +4 -0
  38. metadata +22 -20
  39. data/spec/support/html_matchers.rb +0 -7
@@ -3,7 +3,7 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Rating::Rating, ':values_data' do
6
- subject { described_class.values_data article_1 }
6
+ let!(:category) { create :category }
7
7
 
8
8
  let!(:user_1) { create :user }
9
9
  let!(:user_2) { create :user }
@@ -17,17 +17,40 @@ RSpec.describe Rating::Rating, ':values_data' do
17
17
  create :rating_rate, author: user_1, resource: article_2, value: 11
18
18
  create :rating_rate, author: user_1, resource: article_3, value: 10
19
19
  create :rating_rate, author: user_2, resource: article_1, value: 4
20
- end
21
20
 
22
- it 'returns the average of value for a resource' do
23
- expect(subject.as_json['rating_avg']).to eq 52.0
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
24
23
  end
25
24
 
26
- it 'returns the sum of values for a resource' do
27
- expect(subject.as_json['rating_sum']).to eq 104
25
+ context 'with no scopeable' do
26
+ subject { described_class.values_data article_1, nil }
27
+
28
+ it 'returns the average of value for a resource' do
29
+ expect(subject.as_json['rating_avg']).to eq 52.0
30
+ end
31
+
32
+ it 'returns the sum of values for a resource' do
33
+ expect(subject.as_json['rating_sum']).to eq 104
34
+ end
35
+
36
+ it 'returns the count of votes for a resource' do
37
+ expect(subject.as_json['rating_count']).to eq 2
38
+ end
28
39
  end
29
40
 
30
- it 'returns the count of votes for a resource' do
31
- expect(subject.as_json['rating_count']).to eq 2
41
+ context 'with scopeable' do
42
+ subject { described_class.values_data article_1, category }
43
+
44
+ it 'returns the average of value for a resource' do
45
+ expect(subject.as_json['rating_avg']).to eq 1.5
46
+ end
47
+
48
+ it 'returns the sum of values for a resource' do
49
+ expect(subject.as_json['rating_sum']).to eq 3
50
+ end
51
+
52
+ it 'returns the count of votes for a resource' do
53
+ expect(subject.as_json['rating_count']).to eq 2
54
+ end
32
55
  end
33
56
  end
@@ -8,6 +8,7 @@ RSpec.describe Rating::Rating do
8
8
  it { expect(object).to be_valid }
9
9
 
10
10
  it { is_expected.to belong_to :resource }
11
+ it { is_expected.to belong_to :scopeable }
11
12
 
12
13
  it { is_expected.to validate_presence_of :average }
13
14
  it { is_expected.to validate_presence_of :estimate }
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateAuthorsTable < ActiveRecord::Migration[5.0]
4
+ def change
5
+ create_table :authors do |t|
6
+ t.string :name, null: false
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateCategoriesTable < ActiveRecord::Migration[5.0]
4
+ def change
5
+ create_table :categories do |t|
6
+ t.string :name, null: false
7
+ end
8
+ end
9
+ end
@@ -2,6 +2,8 @@
2
2
 
3
3
  require File.expand_path('../../lib/generators/rating/templates/db/migrate/create_rating_tables.rb', __dir__)
4
4
 
5
+ CreateArticlesTable.new.change
6
+ CreateAuthorsTable.new.change
7
+ CreateCategoriesTable.new.change
5
8
  CreateRatingTables.new.change
6
9
  CreateUsersTable.new.change
7
- CreateArticlesTable.new.change
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Author < ::ActiveRecord::Base
4
+ rating as: :author
5
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Category < ::ActiveRecord::Base
4
+ end
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.1.0
4
+ version: 0.2.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-10-31 00:00:00.000000000 Z
11
+ date: 2017-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -72,20 +72,6 @@ dependencies:
72
72
  - - ">="
73
73
  - !ruby/object:Gem::Version
74
74
  version: '0'
75
- - !ruby/object:Gem::Dependency
76
- name: rspec-html-matchers
77
- requirement: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - ">="
80
- - !ruby/object:Gem::Version
81
- version: '0'
82
- type: :development
83
- prerelease: false
84
- version_requirements: !ruby/object:Gem::Requirement
85
- requirements:
86
- - - ">="
87
- - !ruby/object:Gem::Version
88
- version: '0'
89
75
  - !ruby/object:Gem::Dependency
90
76
  name: rspec-rails
91
77
  requirement: !ruby/object:Gem::Requirement
@@ -142,7 +128,7 @@ dependencies:
142
128
  - - ">="
143
129
  - !ruby/object:Gem::Version
144
130
  version: '0'
145
- description: A true Bayesian rating system with cache enabled.
131
+ description: A true Bayesian rating system with scope and cache enabled.
146
132
  email: wbotelhos@gmail.com
147
133
  executables: []
148
134
  extensions: []
@@ -159,6 +145,8 @@ files:
159
145
  - lib/rating/models/rating/rating.rb
160
146
  - lib/rating/version.rb
161
147
  - spec/factories/article.rb
148
+ - spec/factories/author.rb
149
+ - spec/factories/category.rb
162
150
  - spec/factories/rating/rate.rb
163
151
  - spec/factories/rating/rating.rb
164
152
  - spec/factories/user.rb
@@ -167,8 +155,11 @@ files:
167
155
  - spec/models/extension/rate_for_spec.rb
168
156
  - spec/models/extension/rate_spec.rb
169
157
  - spec/models/extension/rated_question_spec.rb
158
+ - spec/models/extension/rated_records_spec.rb
170
159
  - spec/models/extension/rated_spec.rb
160
+ - spec/models/extension/rates_records_spec.rb
171
161
  - spec/models/extension/rates_spec.rb
162
+ - spec/models/extension/rating_records_spec.rb
172
163
  - spec/models/extension/rating_spec.rb
173
164
  - spec/models/rate/create_spec.rb
174
165
  - spec/models/rate/rate_for_spec.rb
@@ -182,11 +173,14 @@ files:
182
173
  - spec/support/common.rb
183
174
  - spec/support/database_cleaner.rb
184
175
  - spec/support/db/migrate/create_articles_table.rb
176
+ - spec/support/db/migrate/create_authors_table.rb
177
+ - spec/support/db/migrate/create_category_spec.rb
185
178
  - spec/support/db/migrate/create_users_table.rb
186
179
  - spec/support/factory_bot.rb
187
- - spec/support/html_matchers.rb
188
180
  - spec/support/migrate.rb
189
181
  - spec/support/models/article.rb
182
+ - spec/support/models/author.rb
183
+ - spec/support/models/category.rb
190
184
  - spec/support/models/user.rb
191
185
  - spec/support/shoulda.rb
192
186
  homepage: https://github.com/wbotelhos/rating
@@ -212,9 +206,11 @@ rubyforge_project:
212
206
  rubygems_version: 2.6.14
213
207
  signing_key:
214
208
  specification_version: 4
215
- summary: A true Bayesian rating system with cache enabled.
209
+ summary: A true Bayesian rating system with scope and cache enabled.
216
210
  test_files:
217
211
  - spec/factories/article.rb
212
+ - spec/factories/author.rb
213
+ - spec/factories/category.rb
218
214
  - spec/factories/rating/rate.rb
219
215
  - spec/factories/rating/rating.rb
220
216
  - spec/factories/user.rb
@@ -223,8 +219,11 @@ test_files:
223
219
  - spec/models/extension/rate_for_spec.rb
224
220
  - spec/models/extension/rate_spec.rb
225
221
  - spec/models/extension/rated_question_spec.rb
222
+ - spec/models/extension/rated_records_spec.rb
226
223
  - spec/models/extension/rated_spec.rb
224
+ - spec/models/extension/rates_records_spec.rb
227
225
  - spec/models/extension/rates_spec.rb
226
+ - spec/models/extension/rating_records_spec.rb
228
227
  - spec/models/extension/rating_spec.rb
229
228
  - spec/models/rate/create_spec.rb
230
229
  - spec/models/rate/rate_for_spec.rb
@@ -238,10 +237,13 @@ test_files:
238
237
  - spec/support/common.rb
239
238
  - spec/support/database_cleaner.rb
240
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
241
242
  - spec/support/db/migrate/create_users_table.rb
242
243
  - spec/support/factory_bot.rb
243
- - spec/support/html_matchers.rb
244
244
  - spec/support/migrate.rb
245
245
  - spec/support/models/article.rb
246
+ - spec/support/models/author.rb
247
+ - spec/support/models/category.rb
246
248
  - spec/support/models/user.rb
247
249
  - spec/support/shoulda.rb
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rspec-html-matchers'
4
-
5
- RSpec.configure do |config|
6
- config.include RSpecHtmlMatchers
7
- end