rating 0.9.0 → 0.12.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 (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +36 -10
  3. data/README.md +40 -16
  4. data/lib/rating/models/rating/extension.rb +1 -2
  5. data/lib/rating/models/rating/rate.rb +3 -7
  6. data/lib/rating/models/rating/rating.rb +25 -5
  7. data/lib/rating/version.rb +1 -1
  8. metadata +56 -122
  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/models/extension/after_create_spec.rb +0 -47
  20. data/spec/models/extension/order_by_rating_spec.rb +0 -180
  21. data/spec/models/extension/rate_for_spec.rb +0 -41
  22. data/spec/models/extension/rate_spec.rb +0 -60
  23. data/spec/models/extension/rated_question_spec.rb +0 -48
  24. data/spec/models/extension/rated_records_spec.rb +0 -12
  25. data/spec/models/extension/rated_spec.rb +0 -50
  26. data/spec/models/extension/rates_records_spec.rb +0 -12
  27. data/spec/models/extension/rates_spec.rb +0 -50
  28. data/spec/models/extension/rating_records_spec.rb +0 -12
  29. data/spec/models/extension/rating_spec.rb +0 -40
  30. data/spec/models/extension/rating_warm_up_spec.rb +0 -115
  31. data/spec/models/extension/unscoped_rating_spec.rb +0 -121
  32. data/spec/models/rate/create_spec.rb +0 -364
  33. data/spec/models/rate/rate_for_spec.rb +0 -107
  34. data/spec/models/rate_spec.rb +0 -28
  35. data/spec/models/rating/averager_data_spec.rb +0 -32
  36. data/spec/models/rating/data_spec.rb +0 -48
  37. data/spec/models/rating/update_rating_spec.rb +0 -48
  38. data/spec/models/rating/values_data_spec.rb +0 -40
  39. data/spec/models/rating_spec.rb +0 -24
  40. data/spec/rails_helper.rb +0 -17
  41. data/spec/support/common.rb +0 -22
  42. data/spec/support/database_cleaner.rb +0 -19
  43. data/spec/support/db/migrate/add_comment_on_rating_rates_table.rb +0 -8
  44. data/spec/support/db/migrate/add_extra_fields_on_rating_rates_table.rb +0 -8
  45. data/spec/support/db/migrate/create_articles_table.rb +0 -9
  46. data/spec/support/db/migrate/create_authors_table.rb +0 -9
  47. data/spec/support/db/migrate/create_categories_table.rb +0 -12
  48. data/spec/support/db/migrate/create_comments_table.rb +0 -7
  49. data/spec/support/db/migrate/create_globals_table.rb +0 -8
  50. data/spec/support/db/migrate/create_rates_table.rb +0 -19
  51. data/spec/support/db/migrate/create_rating_table.rb +0 -20
  52. data/spec/support/db/migrate/create_review_ratings_table.rb +0 -17
  53. data/spec/support/db/migrate/create_reviews_table.rb +0 -15
  54. data/spec/support/factory_bot.rb +0 -9
  55. data/spec/support/migrate.rb +0 -20
  56. data/spec/support/models/article.rb +0 -7
  57. data/spec/support/models/author.rb +0 -5
  58. data/spec/support/models/category.rb +0 -6
  59. data/spec/support/models/comment.rb +0 -5
  60. data/spec/support/models/global.rb +0 -7
  61. data/spec/support/models/review.rb +0 -5
  62. data/spec/support/models/review_rating.rb +0 -4
  63. data/spec/support/shared_context/with_database_records.rb +0 -20
  64. data/spec/support/shoulda.rb +0 -10
@@ -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,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,20 +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
-
11
- CreateGlobalsTable.new.change
12
- CreateCategoriesTable.new.change
13
-
14
- CreateCommentsTable.new.change
15
-
16
- CreateReviewRatingsTable.new.change
17
- CreateReviewsTable.new.change
18
-
19
- AddCommentOnRatingRatesTable.new.change
20
- 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,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