rating 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +23 -9
  3. data/README.md +16 -5
  4. data/lib/rating/models/rating/extension.rb +1 -2
  5. data/lib/rating/models/rating/rate.rb +2 -2
  6. data/lib/rating/models/rating/rating.rb +2 -2
  7. data/lib/rating/version.rb +1 -1
  8. metadata +56 -130
  9. data/spec/config/rate_table_spec.rb +0 -17
  10. data/spec/config/rating_table_spec.rb +0 -17
  11. data/spec/config/validations_spec.rb +0 -33
  12. data/spec/factories/article.rb +0 -7
  13. data/spec/factories/author.rb +0 -7
  14. data/spec/factories/category.rb +0 -7
  15. data/spec/factories/comment.rb +0 -5
  16. data/spec/factories/global.rb +0 -6
  17. data/spec/factories/rating/rate.rb +0 -10
  18. data/spec/factories/rating/rating.rb +0 -12
  19. data/spec/factories/toy.rb +0 -6
  20. data/spec/models/extension/after_create_spec.rb +0 -47
  21. data/spec/models/extension/order_by_rating_spec.rb +0 -180
  22. data/spec/models/extension/rate_for_spec.rb +0 -41
  23. data/spec/models/extension/rate_spec.rb +0 -60
  24. data/spec/models/extension/rated_question_spec.rb +0 -48
  25. data/spec/models/extension/rated_records_spec.rb +0 -12
  26. data/spec/models/extension/rated_spec.rb +0 -50
  27. data/spec/models/extension/rates_records_spec.rb +0 -12
  28. data/spec/models/extension/rates_spec.rb +0 -50
  29. data/spec/models/extension/rating_records_spec.rb +0 -12
  30. data/spec/models/extension/rating_spec.rb +0 -40
  31. data/spec/models/extension/rating_warm_up_spec.rb +0 -115
  32. data/spec/models/extension/unscoped_rating_spec.rb +0 -99
  33. data/spec/models/extension/where_spec.rb +0 -33
  34. data/spec/models/rate/create_spec.rb +0 -422
  35. data/spec/models/rate/rate_for_spec.rb +0 -107
  36. data/spec/models/rate_spec.rb +0 -28
  37. data/spec/models/rating/averager_data_spec.rb +0 -32
  38. data/spec/models/rating/data_spec.rb +0 -48
  39. data/spec/models/rating/update_rating_spec.rb +0 -48
  40. data/spec/models/rating/values_data_spec.rb +0 -40
  41. data/spec/models/rating_spec.rb +0 -24
  42. data/spec/rails_helper.rb +0 -17
  43. data/spec/support/common.rb +0 -22
  44. data/spec/support/database_cleaner.rb +0 -19
  45. data/spec/support/db/migrate/add_comment_on_rating_rates_table.rb +0 -8
  46. data/spec/support/db/migrate/add_extra_fields_on_rating_rates_table.rb +0 -8
  47. data/spec/support/db/migrate/create_articles_table.rb +0 -9
  48. data/spec/support/db/migrate/create_authors_table.rb +0 -9
  49. data/spec/support/db/migrate/create_categories_table.rb +0 -12
  50. data/spec/support/db/migrate/create_comments_table.rb +0 -7
  51. data/spec/support/db/migrate/create_globals_table.rb +0 -8
  52. data/spec/support/db/migrate/create_rates_table.rb +0 -19
  53. data/spec/support/db/migrate/create_rating_table.rb +0 -20
  54. data/spec/support/db/migrate/create_review_ratings_table.rb +0 -17
  55. data/spec/support/db/migrate/create_reviews_table.rb +0 -15
  56. data/spec/support/db/migrate/create_toys_table.rb +0 -8
  57. data/spec/support/factory_bot.rb +0 -9
  58. data/spec/support/migrate.rb +0 -21
  59. data/spec/support/models/article.rb +0 -7
  60. data/spec/support/models/author.rb +0 -5
  61. data/spec/support/models/category.rb +0 -6
  62. data/spec/support/models/comment.rb +0 -5
  63. data/spec/support/models/global.rb +0 -7
  64. data/spec/support/models/review.rb +0 -5
  65. data/spec/support/models/review_rating.rb +0 -4
  66. data/spec/support/models/toy.rb +0 -5
  67. data/spec/support/shared_context/with_database_records.rb +0 -20
  68. data/spec/support/shoulda.rb +0 -10
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
- require 'support/shared_context/with_database_records'
5
-
6
- RSpec.describe Rating::Rating, ':averager_data' do
7
- include_context 'with_database_records'
8
-
9
- context 'with no scopeable' do
10
- subject(:result) { described_class.averager_data article_1, nil }
11
-
12
- it 'returns the values average of given resource type' do
13
- expect(result.as_json['rating_avg'].to_f.to_s).to eq '30.5'
14
- end
15
-
16
- it 'returns the average of number of records for the given resource type' do
17
- expect(result.as_json['count_avg'].to_s).to eq '1.333333333333333333'
18
- end
19
- end
20
-
21
- context 'with scopeable' do
22
- subject(:result) { described_class.averager_data article_1, category }
23
-
24
- it 'returns the values average of given resource type' do
25
- expect(result.as_json['rating_avg'].to_f.to_s).to eq '1.5'
26
- end
27
-
28
- it 'returns the average of number of records for the given resource type' do
29
- expect(result.as_json['count_avg'].to_f.to_s).to eq '2.0'
30
- end
31
- end
32
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
- require 'support/shared_context/with_database_records'
5
-
6
- RSpec.describe Rating::Rating, ':data' do
7
- include_context 'with_database_records'
8
-
9
- context 'with no scopeable' do
10
- subject(:result) { described_class.data article_1, nil }
11
-
12
- it 'returns the average of value for a resource' do
13
- expect(result[:average]).to eq 50.5
14
- end
15
-
16
- it 'returns the sum of values for a resource' do
17
- expect(result[:sum]).to eq 101
18
- end
19
-
20
- it 'returns the count of votes for a resource' do
21
- expect(result[:total]).to eq 2
22
- end
23
-
24
- it 'returns the estimate for a resource' do
25
- expect(result[:estimate].to_s).to eq '42.5000000000000000012000000505'
26
- end
27
- end
28
-
29
- context 'with scopeable' do
30
- subject(:result) { described_class.data article_1, category }
31
-
32
- it 'returns the average of value for a resource' do
33
- expect(result[:average]).to eq 1.5
34
- end
35
-
36
- it 'returns the sum of values for a resource' do
37
- expect(result[:sum]).to eq 3
38
- end
39
-
40
- it 'returns the count of votes for a resource' do
41
- expect(result[:total]).to eq 2
42
- end
43
-
44
- it 'returns the estimate for a resource' do
45
- expect(result[:estimate]).to eq 1.5
46
- end
47
- end
48
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
- require 'support/shared_context/with_database_records'
5
-
6
- RSpec.describe Rating::Rating, ':update_rating' do
7
- context 'with no scopeable' do
8
- include_context 'with_database_records'
9
-
10
- it 'updates the rating data of the given resource' do
11
- record = described_class.find_by(resource: article_1)
12
-
13
- expect(record.average).to eq 50.5
14
- expect(record.estimate).to eq 42.5
15
- expect(record.sum).to eq 101
16
- expect(record.total).to eq 2
17
- end
18
- end
19
-
20
- context 'with scopeable' do
21
- include_context 'with_database_records'
22
-
23
- it 'updates the rating data of the given resource respecting the scope' do
24
- record = described_class.find_by(resource: article_1, scopeable: category)
25
-
26
- expect(record.average).to eq 1.5
27
- expect(record.estimate).to eq 1.5
28
- expect(record.sum).to eq 3
29
- expect(record.total).to eq 2
30
- end
31
- end
32
-
33
- context 'when rate table has no record' do
34
- let!(:resource) { create :article }
35
- let!(:scope) { nil }
36
-
37
- it 'calculates with counts values as zero' do
38
- described_class.update_rating resource, scope
39
-
40
- record = described_class.last
41
-
42
- expect(record.average).to eq 0
43
- expect(record.estimate).to eq 0
44
- expect(record.sum).to eq 0
45
- expect(record.total).to eq 0
46
- end
47
- end
48
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
- require 'support/shared_context/with_database_records'
5
-
6
- RSpec.describe Rating::Rating, ':values_data' do
7
- include_context 'with_database_records'
8
-
9
- context 'with no scopeable' do
10
- subject(:result) { described_class.values_data article_1, nil }
11
-
12
- it 'returns the average of value for a resource' do
13
- expect(result.as_json['rating_avg'].to_f.to_s).to eq '50.5'
14
- end
15
-
16
- it 'returns the sum of values for a resource' do
17
- expect(result.as_json['rating_sum'].to_f.to_s).to eq '101.0'
18
- end
19
-
20
- it 'returns the count of votes for a resource' do
21
- expect(result.as_json['rating_count'].to_f.to_s).to eq '2.0'
22
- end
23
- end
24
-
25
- context 'with scopeable' do
26
- subject(:result) { described_class.values_data article_1, category }
27
-
28
- it 'returns the average of value for a resource' do
29
- expect(result.as_json['rating_avg'].to_f.to_s).to eq '1.5'
30
- end
31
-
32
- it 'returns the sum of values for a resource' do
33
- expect(result.as_json['rating_sum'].to_f.to_s).to eq '3.0'
34
- end
35
-
36
- it 'returns the count of votes for a resource' do
37
- expect(result.as_json['rating_count'].to_f.to_s).to eq '2.0'
38
- end
39
- end
40
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Rating::Rating do
6
- let!(:object) { build :rating_rating }
7
-
8
- it { expect(object).to be_valid }
9
-
10
- it { is_expected.to belong_to :resource }
11
- it { is_expected.to belong_to :scopeable }
12
-
13
- it { is_expected.to validate_presence_of :average }
14
- it { is_expected.to validate_presence_of :estimate }
15
- it { is_expected.to validate_presence_of :resource }
16
- it { is_expected.to validate_presence_of :sum }
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
24
- end
data/spec/rails_helper.rb DELETED
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- ENV['RAILS_ENV'] ||= 'test'
4
-
5
- require 'active_record/railtie'
6
- require 'mysql2'
7
- require 'pry-byebug'
8
- require 'rating'
9
-
10
- client = Mysql2::Client.new(host: :localhost, username: :root)
11
-
12
- client.query 'DROP DATABASE IF EXISTS rating_test;'
13
- client.query 'CREATE DATABASE IF NOT EXISTS rating_test;'
14
-
15
- ActiveRecord::Base.establish_connection adapter: :mysql2, database: :rating_test, username: :root
16
-
17
- Dir[File.expand_path('support/**/*.rb', __dir__)].each { |file| require file }
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rspec/rails'
4
-
5
- RSpec.configure do |config|
6
- config.filter_run_when_matching :focus
7
-
8
- config.disable_monkey_patching!
9
- config.infer_spec_type_from_file_location!
10
-
11
- config.expect_with :rspec do |expectations|
12
- expectations.include_chain_clauses_in_custom_matcher_descriptions = true
13
- end
14
-
15
- config.mock_with :rspec do |mocks|
16
- mocks.verify_partial_doubles = true
17
- end
18
-
19
- config.infer_base_class_for_anonymous_controllers = false
20
- config.order = :random
21
- config.profile_examples = true
22
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'database_cleaner'
4
-
5
- RSpec.configure do |config|
6
- config.before :suite do
7
- DatabaseCleaner.strategy = :transaction
8
-
9
- DatabaseCleaner.clean_with :truncation
10
- end
11
-
12
- config.before { DatabaseCleaner.start }
13
-
14
- config.around do |spec|
15
- DatabaseCleaner.cleaning do
16
- spec.run
17
- end
18
- end
19
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class AddCommentOnRatingRatesTable < ActiveRecord::Migration[5.0]
4
- def change
5
- add_column :rating_rates, :comment, :text
6
- add_column :reviews, :comment, :text
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class AddExtraScopesOnRatingRatesTable < ActiveRecord::Migration[5.0]
4
- def change
5
- add_column :rating_rates, :scope_1, :string
6
- add_column :rating_rates, :scope_2, :string
7
- end
8
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class CreateArticlesTable < ActiveRecord::Migration[5.0]
4
- def change
5
- create_table :articles do |t|
6
- t.string :name, null: false
7
- end
8
- end
9
- end
@@ -1,9 +0,0 @@
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
@@ -1,12 +0,0 @@
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
-
8
- t.references :article, foreign_key: true, index: true
9
- t.references :global, foreign_key: true, index: true
10
- end
11
- end
12
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class CreateCommentsTable < ActiveRecord::Migration[5.0]
4
- def change
5
- create_table :comments
6
- end
7
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class CreateGlobalsTable < ActiveRecord::Migration[5.0]
4
- def change
5
- create_table :globals do |t|
6
- end
7
- end
8
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class CreateRateTable < ActiveRecord::Migration[5.0]
4
- def change
5
- create_table :rating_rates do |t|
6
- t.decimal :value, default: 0, precision: 25, scale: 16
7
-
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
-
12
- t.timestamps null: false
13
- end
14
-
15
- change_column :rating_rates, :author_type, :string, limit: 10
16
- change_column :rating_rates, :resource_type, :string, limit: 10
17
- change_column :rating_rates, :scopeable_type, :string, limit: 10
18
- end
19
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class CreateRatingTable < ActiveRecord::Migration[5.0]
4
- def change
5
- create_table :rating_ratings do |t|
6
- t.decimal :average, default: 0, mull: false, precision: 25, scale: 16
7
- t.decimal :estimate, default: 0, mull: false, precision: 25, scale: 16
8
- t.integer :sum, default: 0, mull: false
9
- t.integer :total, default: 0, mull: false
10
-
11
- t.references :resource, index: true, null: false, polymorphic: true
12
- t.references :scopeable, index: true, null: true, polymorphic: true
13
-
14
- t.timestamps null: false
15
- end
16
-
17
- change_column :rating_ratings, :resource_type, :string, limit: 10
18
- change_column :rating_ratings, :scopeable_type, :string, limit: 10
19
- end
20
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class CreateReviewRatingsTable < ActiveRecord::Migration[5.0]
4
- def change
5
- create_table :review_ratings do |t|
6
- t.decimal :average, default: 0, mull: false, precision: 25, scale: 16
7
- t.decimal :estimate, default: 0, mull: false, precision: 25, scale: 16
8
- t.integer :sum, default: 0, mull: false
9
- t.integer :total, default: 0, mull: false
10
-
11
- t.references :resource, index: true, null: false, polymorphic: true
12
- t.references :scopeable, index: true, null: true, polymorphic: true
13
-
14
- t.timestamps null: false
15
- end
16
- end
17
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class CreateReviewsTable < ActiveRecord::Migration[5.0]
4
- def change
5
- create_table :reviews do |t|
6
- t.decimal :value, default: 0, precision: 25, scale: 16
7
-
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
-
12
- t.timestamps null: false
13
- end
14
- end
15
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class CreateToysTable < ActiveRecord::Migration[5.0]
4
- def change
5
- create_table :toys do |t|
6
- end
7
- end
8
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'factory_bot'
4
-
5
- Dir[File.expand_path('../factories/**/*.rb', __dir__)].each { |file| require file }
6
-
7
- RSpec.configure do |config|
8
- config.include FactoryBot::Syntax::Methods
9
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- Dir[File.expand_path('db/migrate/*.rb', __dir__)].each { |file| require file }
4
-
5
- CreateRateTable.new.change
6
- CreateRatingTable.new.change
7
-
8
- CreateArticlesTable.new.change
9
- CreateAuthorsTable.new.change
10
- CreateToysTable.new.change
11
-
12
- CreateGlobalsTable.new.change
13
- CreateCategoriesTable.new.change
14
-
15
- CreateCommentsTable.new.change
16
-
17
- CreateReviewRatingsTable.new.change
18
- CreateReviewsTable.new.change
19
-
20
- AddCommentOnRatingRatesTable.new.change
21
- AddExtraScopesOnRatingRatesTable.new.change if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Article < ::ActiveRecord::Base
4
- rating scoping: :categories
5
-
6
- has_many :categories
7
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Author < ::ActiveRecord::Base
4
- rating as: :author
5
- end
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Category < ::ActiveRecord::Base
4
- belongs_to :article
5
- belongs_to :global
6
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Comment < ::ActiveRecord::Base
4
- rating
5
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Global < ::ActiveRecord::Base
4
- rating scoping: :categories, unscoped_rating: true
5
-
6
- has_many :categories
7
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Review < ::ActiveRecord::Base
4
- belongs_to :scopeable, polymorphic: true
5
- end
@@ -1,4 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class ReviewRating < ::ActiveRecord::Base
4
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Toy < ::ActiveRecord::Base
4
- rating where: 'value > 1 and value < 5'
5
- end
@@ -1,20 +0,0 @@
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
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'shoulda-matchers'
4
-
5
- Shoulda::Matchers.configure do |config|
6
- config.integrate do |with|
7
- with.library :rails
8
- with.test_framework :rspec
9
- end
10
- end