diff --git a/.rubocop.yml b/.rubocop.yml index 9fbdc59..efd0435 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,9 +1,15 @@ -require: rubocop-rspec +require: + - rubocop-performance + - rubocop-rspec inherit_from: .rubocop_todo.yml -Rails: - Enabled: true +Layout/AlignArguments: + EnforcedStyle: with_fixed_indentation + +Layout/AlignHash: + EnforcedColonStyle: table + Enabled: false Layout/AlignParameters: EnforcedStyle: with_fixed_indentation @@ -11,14 +17,32 @@ Layout/AlignParameters: Layout/EmptyLinesAroundArguments: Enabled: false -Rails/ApplicationRecord: +Layout/MultilineMethodCallBraceLayout: + EnforcedStyle: new_line + +Metrics/AbcSize: + Max: 50 + +Metrics/BlockLength: Enabled: false -RSpec/DescribeMethod: +Metrics/LineLength: + Max: 120 + +Metrics/MethodLength: + Max: 50 + +Naming/VariableNumber: + EnforcedStyle: snake_case + +Rails: + Enabled: true + +RSpec/AnyInstance: Enabled: false RSpec/ExampleLength: - Max: 10 + Enabled: false RSpec/FilePath: Enabled: false @@ -29,12 +53,6 @@ RSpec/MultipleExpectations: RSpec/NestedGroups: Max: 5 -Metrics/LineLength: - Max: 120 - -Naming/VariableNumber: - EnforcedStyle: snake_case - Style/Documentation: Enabled: false @@ -46,3 +64,12 @@ Style/PercentLiteralDelimiters: '%r': '()' '%w': '[]' '%W': '[]' + +Style/PerlBackrefs: + Enabled: false + +Style/TrailingCommaInArrayLiteral: + EnforcedStyleForMultiline: comma + +Style/TrailingCommaInHashLiteral: + EnforcedStyleForMultiline: comma diff --git a/lib/generators/rating/install_generator.rb b/lib/generators/rating/install_generator.rb index c810816..b188101 100644 --- a/lib/generators/rating/install_generator.rb +++ b/lib/generators/rating/install_generator.rb @@ -2,7 +2,7 @@ module Rating class InstallGenerator < Rails::Generators::Base - source_root File.expand_path('../templates', __FILE__) + source_root File.expand_path('templates', __dir__) desc 'Creates Rating migration' diff --git a/lib/rating/config.rb b/lib/rating/config.rb index 192008f..06150b4 100644 --- a/lib/rating/config.rb +++ b/lib/rating/config.rb @@ -29,8 +29,8 @@ module Rating { rate: { case_sensitive: config.dig('validations', 'rate', 'case_sensitive') || false, - scope: config.dig('validations', 'rate', 'scope') || default_scope - } + scope: config.dig('validations', 'rate', 'scope') || default_scope, + }, }.deep_stringify_keys end end diff --git a/lib/rating/models/rating/extension.rb b/lib/rating/models/rating/extension.rb index 95bad21..6816fe2 100644 --- a/lib/rating/models/rating/extension.rb +++ b/lib/rating/models/rating/extension.rb @@ -73,7 +73,7 @@ module Rating class_name: '::Rating::Rate', dependent: :destroy - scope :order_by_rating, ->(column = :estimate, direction = :desc, scope: nil) { + scope :order_by_rating, lambda { |column = :estimate, direction = :desc, scope: nil| includes(:rating_records) .where(Rating.table_name => { scopeable_id: scope&.id, scopeable_type: scope&.class&.base_class&.name }) .order("#{Rating.table_name}.#{column} #{direction}") diff --git a/lib/rating/models/rating/rate.rb b/lib/rating/models/rating/rate.rb index ec79e61..24bef7b 100644 --- a/lib/rating/models/rating/rate.rb +++ b/lib/rating/models/rating/rate.rb @@ -16,7 +16,7 @@ module Rating validates :author_id, uniqueness: { case_sensitive: ::Rating::Config.validations['rate']['case_sensitive'], - scope: ::Rating::Config.validations['rate']['scope'].map(&:to_sym) + scope: ::Rating::Config.validations['rate']['scope'].map(&:to_sym), } def self.create(author:, extra_scopes:, metadata:, resource:, scopeable: nil, value:) diff --git a/lib/rating/models/rating/rating.rb b/lib/rating/models/rating/rating.rb index f727221..7236a1a 100644 --- a/lib/rating/models/rating/rating.rb +++ b/lib/rating/models/rating/rating.rb @@ -13,7 +13,7 @@ module Rating validates :resource_id, uniqueness: { case_sensitive: false, - scope: %i[resource_type scopeable_id scopeable_type] + scope: %i[resource_type scopeable_id scopeable_type], } class << self @@ -43,7 +43,7 @@ module Rating average: values.rating_avg, estimate: estimate(averager, values), sum: values.rating_sum, - total: values.rating_count + total: values.rating_count, } end diff --git a/rating.gemspec b/rating.gemspec index b97ba4a..f1ce840 100644 --- a/rating.gemspec +++ b/rating.gemspec @@ -1,6 +1,6 @@ # frozen_string_literal: true -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) diff --git a/spec/config/rate_table_spec.rb b/spec/config/rate_table_spec.rb index 72968fa..a050a2f 100644 --- a/spec/config/rate_table_spec.rb +++ b/spec/config/rate_table_spec.rb @@ -3,11 +3,15 @@ require 'rails_helper' RSpec.describe Rating::Config, '.rate_table' do - context 'when rating.yml does not exist' do - it { expect(subject.rate_table).to eq 'rating_rates' } - end if ENV['CONFIG_ENABLED'] != 'true' + if ENV['CONFIG_ENABLED'] != 'true' + context 'when rating.yml does not exist' do + it { expect(subject.rate_table).to eq 'rating_rates' } + end + end - context 'when rating.yml exists' do - it { expect(subject.rate_table).to eq 'reviews' } - end if ENV['CONFIG_ENABLED'] == 'true' + if ENV['CONFIG_ENABLED'] == 'true' + context 'when rating.yml exists' do + it { expect(subject.rate_table).to eq 'reviews' } + end + end end diff --git a/spec/config/rating_table_spec.rb b/spec/config/rating_table_spec.rb index 9d9bdc9..c9fa426 100644 --- a/spec/config/rating_table_spec.rb +++ b/spec/config/rating_table_spec.rb @@ -3,11 +3,15 @@ require 'rails_helper' RSpec.describe Rating::Config, '.rating_table' do - context 'when rating.yml does not exist' do - it { expect(subject.rating_table).to eq 'rating_ratings' } - end if ENV['CONFIG_ENABLED'] != 'true' + if ENV['CONFIG_ENABLED'] != 'true' + context 'when rating.yml does not exist' do + it { expect(subject.rating_table).to eq 'rating_ratings' } + end + end - context 'when rating.yml exists' do - it { expect(subject.rating_table).to eq 'review_ratings' } - end if ENV['CONFIG_ENABLED'] == 'true' + if ENV['CONFIG_ENABLED'] == 'true' + context 'when rating.yml exists' do + it { expect(subject.rating_table).to eq 'review_ratings' } + end + end end diff --git a/spec/config/validations_spec.rb b/spec/config/validations_spec.rb index 0775f14..1336744 100644 --- a/spec/config/validations_spec.rb +++ b/spec/config/validations_spec.rb @@ -3,25 +3,31 @@ require 'rails_helper' RSpec.describe Rating::Config, '.validations' do - context 'when rating.yml does not exist' do - it do - expect(subject.validations).to eq({ - rate: { - case_sensitive: false, - scope: %w[author_type resource_id resource_type scopeable_id scopeable_type] - } - }.deep_stringify_keys) + if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] != 'true' + context 'when rating.yml does not exist' do + it do + expect(subject.validations).to eq({ + rate: { + case_sensitive: false, + scope: %w[author_type resource_id resource_type scopeable_id scopeable_type], + }, + }.deep_stringify_keys + ) + end end - end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] != 'true' + end - context 'when rating.yml exists' do - it do - expect(subject.validations).to eq({ - rate: { - case_sensitive: false, - scope: %w[author_type resource_id resource_type scopeable_id scopeable_type scope_1 scope_2] - } - }.deep_stringify_keys) + if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' + context 'when rating.yml exists' do + it do + expect(subject.validations).to eq({ + rate: { + case_sensitive: false, + scope: %w[author_type resource_id resource_type scopeable_id scopeable_type scope_1 scope_2], + }, + }.deep_stringify_keys + ) + end end - end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' + end end diff --git a/spec/factories/rating/rate.rb b/spec/factories/rating/rate.rb index c4a71a1..618f352 100644 --- a/spec/factories/rating/rate.rb +++ b/spec/factories/rating/rate.rb @@ -2,7 +2,7 @@ FactoryBot.define do factory :rating_rate, class: Rating::Rate do - value 100 + value { 100 } author { create :author } resource { create :article } diff --git a/spec/factories/rating/rating.rb b/spec/factories/rating/rating.rb index 250bb15..3bba839 100644 --- a/spec/factories/rating/rating.rb +++ b/spec/factories/rating/rating.rb @@ -2,10 +2,10 @@ FactoryBot.define do factory :rating_rating, class: Rating::Rating do - average 100 - estimate 100 - sum 100 - total 1 + average { 100 } + estimate { 100 } + sum { 100 } + total { 1 } association :resource, factory: :article, strategy: :build end diff --git a/spec/models/extension/order_by_rating_spec.rb b/spec/models/extension/order_by_rating_spec.rb index c895220..46449a0 100644 --- a/spec/models/extension/order_by_rating_spec.rb +++ b/spec/models/extension/order_by_rating_spec.rb @@ -11,7 +11,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do expect(Article.order_by_rating).to eq [ article_1, article_2, - article_3 + article_3, ] end end @@ -22,14 +22,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do expect(Article.order_by_rating(:average, :asc)).to eq [ article_3, article_2, - article_1 + article_1, ] end context 'with scope' do it 'works' do expect(Article.order_by_rating(:average, :asc, scope: category)).to eq [ - article_1 + article_1, ] end end @@ -40,14 +40,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do expect(Article.order_by_rating(:average, :desc)).to eq [ article_1, article_2, - article_3 + article_3, ] end context 'with scope' do it 'works' do expect(Article.order_by_rating(:average, :desc, scope: category)).to eq [ - article_1 + article_1, ] end end @@ -60,14 +60,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do expect(Article.order_by_rating(:estimate, :asc)).to eq [ article_3, article_2, - article_1 + article_1, ] end context 'with scope' do it 'works' do expect(Article.order_by_rating(:estimate, :asc, scope: category)).to eq [ - article_1 + article_1, ] end end @@ -78,14 +78,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do expect(Article.order_by_rating(:estimate, :desc)).to eq [ article_1, article_2, - article_3 + article_3, ] end context 'with scope' do it 'works' do expect(Article.order_by_rating(:estimate, :desc, scope: category)).to eq [ - article_1 + article_1, ] end end @@ -98,14 +98,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do expect(Article.order_by_rating(:sum, :asc)).to eq [ article_3, article_2, - article_1 + article_1, ] end context 'with scope' do it 'works' do expect(Article.order_by_rating(:sum, :asc, scope: category)).to eq [ - article_1 + article_1, ] end end @@ -116,14 +116,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do expect(Article.order_by_rating(:sum, :desc)).to eq [ article_1, article_2, - article_3 + article_3, ] end context 'with scope' do it 'works' do expect(Article.order_by_rating(:sum, :desc, scope: category)).to eq [ - article_1 + article_1, ] end end @@ -142,7 +142,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do context 'with scope' do it 'works' do expect(Article.order_by_rating(:total, :asc, scope: category)).to eq [ - article_1 + article_1, ] end end @@ -159,7 +159,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do context 'with scope' do it 'works' do expect(Article.order_by_rating(:total, :desc, scope: category)).to eq [ - article_1 + article_1, ] end end diff --git a/spec/models/extension/rated_question_spec.rb b/spec/models/extension/rated_question_spec.rb index 5189245..3825fe9 100644 --- a/spec/models/extension/rated_question_spec.rb +++ b/spec/models/extension/rated_question_spec.rb @@ -32,15 +32,17 @@ RSpec.describe Rating::Extension, ':rated?' do end end - context 'with extra scopes' do - before { author.rate resource, 1, extra_scopes: { scope_1: 'scope_1' } } + if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' + context 'with extra scopes' do + before { author.rate resource, 1, extra_scopes: { scope_1: 'scope_1' } } - context 'when has no rate for the given resource with given extra scopes' do - specify { expect(author.rated?(resource, extra_scopes: { scope_1: 'missing' })).to eq false } - end + context 'when has no rate for the given resource with given extra scopes' do + specify { expect(author.rated?(resource, extra_scopes: { scope_1: 'missing' })).to eq false } + end - context 'when has rate for the given resource with given extra scopes' do - specify { expect(author.rated?(resource, extra_scopes: { scope_1: 'scope_1' })).to eq true } + context 'when has rate for the given resource with given extra scopes' do + specify { expect(author.rated?(resource, extra_scopes: { scope_1: 'scope_1' })).to eq true } + end end - end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' + end end diff --git a/spec/models/extension/rated_spec.rb b/spec/models/extension/rated_spec.rb index 1526090..44739ca 100644 --- a/spec/models/extension/rated_spec.rb +++ b/spec/models/extension/rated_spec.rb @@ -38,11 +38,13 @@ RSpec.describe Rating::Extension, ':rated' do end end - context 'with extra scopes' do - let!(:extra_scopes_rate) { author_1.rate article_1, 1, extra_scopes: { scope_1: 'scope_1' } } + if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' + context 'with extra scopes' do + let!(:extra_scopes_rate) { author_1.rate article_1, 1, extra_scopes: { scope_1: 'scope_1' } } - it 'returns records considering the extra scopes' do - expect(author_1.rated(extra_scopes: { scope_1: 'scope_1' })).to eq [extra_scopes_rate] + it 'returns records considering the extra scopes' do + expect(author_1.rated(extra_scopes: { scope_1: 'scope_1' })).to eq [extra_scopes_rate] + end end - end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' + end end diff --git a/spec/models/extension/rates_spec.rb b/spec/models/extension/rates_spec.rb index df6c916..65ff6c5 100644 --- a/spec/models/extension/rates_spec.rb +++ b/spec/models/extension/rates_spec.rb @@ -38,11 +38,13 @@ RSpec.describe Rating::Extension, ':rates' do end end - context 'with extra scopes' do - let!(:extra_scopes_rate) { author_1.rate article_1, 1, extra_scopes: { scope_1: 'scope_1' } } + if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' + context 'with extra scopes' do + let!(:extra_scopes_rate) { author_1.rate article_1, 1, extra_scopes: { scope_1: 'scope_1' } } - it 'returns records considering the extra scopes' do - expect(article_1.rates(extra_scopes: { scope_1: 'scope_1' })).to eq [extra_scopes_rate] + it 'returns records considering the extra scopes' do + expect(article_1.rates(extra_scopes: { scope_1: 'scope_1' })).to eq [extra_scopes_rate] + end end - end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' + end end diff --git a/spec/models/rate/create_spec.rb b/spec/models/rate/create_spec.rb index fb5603d..2016c15 100644 --- a/spec/models/rate/create_spec.rb +++ b/spec/models/rate/create_spec.rb @@ -194,112 +194,11 @@ RSpec.describe Rating::Rate, ':create' do end end - context 'with extra scopes' do - let!(:category) { create :category } + if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' + context 'with extra scopes' do + let!(:category) { create :category } - it 'creates a rate entry' do - described_class.create( - author: author, - extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, - metadata: {}, - resource: article, - scopeable: category, - value: 1 - ) - - rate = described_class.last - - expect(rate.author).to eq author - expect(rate.resource).to eq article - expect(rate.scope_1).to eq 'scope_1' - expect(rate.scope_2).to eq 'scope_2' - expect(rate.scopeable).to eq category - expect(rate.value).to eq 1 - end - - it 'creates a rating entry' do - described_class.create( - author: author, - extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, - metadata: {}, - resource: article, - scopeable: category, - value: 1 - ) - - rating = Rating::Rating.last - - expect(rating.average).to eq 1 - expect(rating.estimate).to eq 1 - expect(rating.resource).to eq article - expect(rating.scopeable).to eq category - expect(rating.sum).to eq 1 - expect(rating.total).to eq 1 - end - - context 'when rate already exists' do - before do - described_class.create( - author: author, - extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, - metadata: {}, - resource: article, - scopeable: category, - value: 1 - ) - end - - it 'updates the rate entry' do - described_class.create( - author: author, - extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, - metadata: {}, - resource: article, - scopeable: category, - value: 2 - ) - - rates = described_class.all - - expect(rates.size).to eq 1 - - rate = rates[0] - - expect(rate.author).to eq author - expect(rate.resource).to eq article - expect(rate.scope_1).to eq 'scope_1' - expect(rate.scope_2).to eq 'scope_2' - expect(rate.scopeable).to eq category - expect(rate.value).to eq 2 - end - - it 'updates the unique rating entry' do - described_class.create( - author: author, - extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, - metadata: {}, - resource: article, - scopeable: category, - value: 2 - ) - - ratings = Rating::Rating.all - - expect(ratings.size).to eq 1 - - rating = ratings[0] - - expect(rating.average).to eq 2 - expect(rating.estimate).to eq 2 - expect(rating.resource).to eq article - expect(rating.scopeable).to eq category - expect(rating.sum).to eq 2 - expect(rating.total).to eq 1 - end - end - - context 'when rate already exists but with at least one extra scope different' do - before do + it 'creates a rate entry' do described_class.create( author: author, extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, @@ -309,22 +208,7 @@ RSpec.describe Rating::Rate, ':create' do value: 1 ) - described_class.create( - author: author, - extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_missing' }, - metadata: {}, - resource: article, - scopeable: category, - value: 2 - ) - end - - it 'creates a new rate entry' do - rates = described_class.all - - expect(rates.size).to eq 2 - - rate = rates[0] + rate = described_class.last expect(rate.author).to eq author expect(rate.resource).to eq article @@ -332,31 +216,149 @@ RSpec.describe Rating::Rate, ':create' do expect(rate.scope_2).to eq 'scope_2' expect(rate.scopeable).to eq category expect(rate.value).to eq 1 - - rate = rates[1] - - expect(rate.author).to eq author - expect(rate.resource).to eq article - expect(rate.scope_1).to eq 'scope_1' - expect(rate.scope_2).to eq 'scope_missing' - expect(rate.scopeable).to eq category - expect(rate.value).to eq 2 end - it 'updates the unique rating entry' do - ratings = Rating::Rating.all + it 'creates a rating entry' do + described_class.create( + author: author, + extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, + metadata: {}, + resource: article, + scopeable: category, + value: 1 + ) - expect(ratings.size).to eq 1 + rating = Rating::Rating.last - rating = ratings[0] - - expect(rating.average).to eq 1.5 - expect(rating.estimate).to eq 1.5 + expect(rating.average).to eq 1 + expect(rating.estimate).to eq 1 expect(rating.resource).to eq article expect(rating.scopeable).to eq category - expect(rating.sum).to eq 3 - expect(rating.total).to eq 2 + expect(rating.sum).to eq 1 + expect(rating.total).to eq 1 + end + + context 'when rate already exists' do + before do + described_class.create( + author: author, + extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, + metadata: {}, + resource: article, + scopeable: category, + value: 1 + ) + end + + it 'updates the rate entry' do + described_class.create( + author: author, + extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, + metadata: {}, + resource: article, + scopeable: category, + value: 2 + ) + + rates = described_class.all + + expect(rates.size).to eq 1 + + rate = rates[0] + + expect(rate.author).to eq author + expect(rate.resource).to eq article + expect(rate.scope_1).to eq 'scope_1' + expect(rate.scope_2).to eq 'scope_2' + expect(rate.scopeable).to eq category + expect(rate.value).to eq 2 + end + + it 'updates the unique rating entry' do + described_class.create( + author: author, + extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, + metadata: {}, + resource: article, + scopeable: category, + value: 2 + ) + + ratings = Rating::Rating.all + + expect(ratings.size).to eq 1 + + rating = ratings[0] + + expect(rating.average).to eq 2 + expect(rating.estimate).to eq 2 + expect(rating.resource).to eq article + expect(rating.scopeable).to eq category + expect(rating.sum).to eq 2 + expect(rating.total).to eq 1 + end + end + + context 'when rate already exists but with at least one extra scope different' do + before do + described_class.create( + author: author, + extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, + metadata: {}, + resource: article, + scopeable: category, + value: 1 + ) + + described_class.create( + author: author, + extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_missing' }, + metadata: {}, + resource: article, + scopeable: category, + value: 2 + ) + end + + it 'creates a new rate entry' do + rates = described_class.all + + expect(rates.size).to eq 2 + + rate = rates[0] + + expect(rate.author).to eq author + expect(rate.resource).to eq article + expect(rate.scope_1).to eq 'scope_1' + expect(rate.scope_2).to eq 'scope_2' + expect(rate.scopeable).to eq category + expect(rate.value).to eq 1 + + rate = rates[1] + + expect(rate.author).to eq author + expect(rate.resource).to eq article + expect(rate.scope_1).to eq 'scope_1' + expect(rate.scope_2).to eq 'scope_missing' + expect(rate.scopeable).to eq category + expect(rate.value).to eq 2 + end + + it 'updates the unique rating entry' do + ratings = Rating::Rating.all + + expect(ratings.size).to eq 1 + + rating = ratings[0] + + expect(rating.average).to eq 1.5 + expect(rating.estimate).to eq 1.5 + expect(rating.resource).to eq article + expect(rating.scopeable).to eq category + expect(rating.sum).to eq 3 + expect(rating.total).to eq 2 + end end end - end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' + end end diff --git a/spec/models/rate/rate_for_spec.rb b/spec/models/rate/rate_for_spec.rb index fb37898..864224e 100644 --- a/spec/models/rate/rate_for_spec.rb +++ b/spec/models/rate/rate_for_spec.rb @@ -51,55 +51,57 @@ RSpec.describe Rating::Rate, ':rate_for' do end end - context 'with extra scopes' do - let!(:category) { create :category } + if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' + context 'with extra scopes' do + let!(:category) { create :category } - context 'when matches all attributes including the extra scopes' do - let!(:record) do - described_class.create( - author: author, - extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, - metadata: {}, - resource: article, - scopeable: category, - value: 1 - ) + context 'when matches all attributes including the extra scopes' do + let!(:record) do + described_class.create( + author: author, + extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, + metadata: {}, + resource: article, + scopeable: category, + value: 1 + ) + end + + it 'returns the record' do + result = described_class.rate_for( + author: author, + extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, + resource: article, + scopeable: category + ) + + expect(result).to eq record + end end - it 'returns the record' do - result = described_class.rate_for( - author: author, - extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, - resource: article, - scopeable: category - ) + context 'when matches all attributes but at least one extra scopes' do + before do + described_class.create( + author: author, + extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, + metadata: {}, + resource: article, + scopeable: category, + value: 1 + ) + end - expect(result).to eq record + it 'does not return the record' do + result = described_class.rate_for( + author: author, + extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_missing' }, + resource: article, + scopeable: category + ) + + expect(result).to eq nil + end end end - - context 'when matches all attributes but at least one extra scopes' do - before do - described_class.create( - author: author, - extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, - metadata: {}, - resource: article, - scopeable: category, - value: 1 - ) - end - - it 'does not return the record' do - result = described_class.rate_for( - author: author, - extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_missing' }, - resource: article, - scopeable: category - ) - - expect(result).to eq nil - end - end - end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' + end end diff --git a/spec/models/rate_spec.rb b/spec/models/rate_spec.rb index 3f7456d..95231d6 100644 --- a/spec/models/rate_spec.rb +++ b/spec/models/rate_spec.rb @@ -16,7 +16,7 @@ RSpec.describe Rating::Rate do it { is_expected.to validate_presence_of :value } it do - is_expected.to validate_numericality_of(:value).is_less_than_or_equal_to(100).is_less_than_or_equal_to 100 + expect(subject).to validate_numericality_of(:value).is_less_than_or_equal_to(100).is_less_than_or_equal_to 100 end it do