main
Washington Botelho 2019-10-03 19:07:50 -03:00
parent 149e3f878d
commit 63a0f7e83b
No known key found for this signature in database
GPG Key ID: 5DE4F42A8F073617
19 changed files with 325 additions and 274 deletions

View File

@ -1,9 +1,15 @@
require: rubocop-rspec require:
- rubocop-performance
- rubocop-rspec
inherit_from: .rubocop_todo.yml inherit_from: .rubocop_todo.yml
Rails: Layout/AlignArguments:
Enabled: true EnforcedStyle: with_fixed_indentation
Layout/AlignHash:
EnforcedColonStyle: table
Enabled: false
Layout/AlignParameters: Layout/AlignParameters:
EnforcedStyle: with_fixed_indentation EnforcedStyle: with_fixed_indentation
@ -11,14 +17,32 @@ Layout/AlignParameters:
Layout/EmptyLinesAroundArguments: Layout/EmptyLinesAroundArguments:
Enabled: false Enabled: false
Rails/ApplicationRecord: Layout/MultilineMethodCallBraceLayout:
EnforcedStyle: new_line
Metrics/AbcSize:
Max: 50
Metrics/BlockLength:
Enabled: false Enabled: false
RSpec/DescribeMethod: Metrics/LineLength:
Max: 120
Metrics/MethodLength:
Max: 50
Naming/VariableNumber:
EnforcedStyle: snake_case
Rails:
Enabled: true
RSpec/AnyInstance:
Enabled: false Enabled: false
RSpec/ExampleLength: RSpec/ExampleLength:
Max: 10 Enabled: false
RSpec/FilePath: RSpec/FilePath:
Enabled: false Enabled: false
@ -29,12 +53,6 @@ RSpec/MultipleExpectations:
RSpec/NestedGroups: RSpec/NestedGroups:
Max: 5 Max: 5
Metrics/LineLength:
Max: 120
Naming/VariableNumber:
EnforcedStyle: snake_case
Style/Documentation: Style/Documentation:
Enabled: false Enabled: false
@ -46,3 +64,12 @@ Style/PercentLiteralDelimiters:
'%r': '()' '%r': '()'
'%w': '[]' '%w': '[]'
'%W': '[]' '%W': '[]'
Style/PerlBackrefs:
Enabled: false
Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: comma
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: comma

View File

@ -2,7 +2,7 @@
module Rating module Rating
class InstallGenerator < Rails::Generators::Base class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__) source_root File.expand_path('templates', __dir__)
desc 'Creates Rating migration' desc 'Creates Rating migration'

View File

@ -29,8 +29,8 @@ module Rating
{ {
rate: { rate: {
case_sensitive: config.dig('validations', 'rate', 'case_sensitive') || false, 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 }.deep_stringify_keys
end end
end end

View File

@ -73,7 +73,7 @@ module Rating
class_name: '::Rating::Rate', class_name: '::Rating::Rate',
dependent: :destroy 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) includes(:rating_records)
.where(Rating.table_name => { scopeable_id: scope&.id, scopeable_type: scope&.class&.base_class&.name }) .where(Rating.table_name => { scopeable_id: scope&.id, scopeable_type: scope&.class&.base_class&.name })
.order("#{Rating.table_name}.#{column} #{direction}") .order("#{Rating.table_name}.#{column} #{direction}")

View File

@ -16,7 +16,7 @@ module Rating
validates :author_id, uniqueness: { validates :author_id, uniqueness: {
case_sensitive: ::Rating::Config.validations['rate']['case_sensitive'], 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:) def self.create(author:, extra_scopes:, metadata:, resource:, scopeable: nil, value:)

View File

@ -13,7 +13,7 @@ module Rating
validates :resource_id, uniqueness: { validates :resource_id, uniqueness: {
case_sensitive: false, case_sensitive: false,
scope: %i[resource_type scopeable_id scopeable_type] scope: %i[resource_type scopeable_id scopeable_type],
} }
class << self class << self
@ -43,7 +43,7 @@ module Rating
average: values.rating_avg, average: values.rating_avg,
estimate: estimate(averager, values), estimate: estimate(averager, values),
sum: values.rating_sum, sum: values.rating_sum,
total: values.rating_count total: values.rating_count,
} }
end end

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true # 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) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

View File

@ -3,11 +3,15 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Rating::Config, '.rate_table' do RSpec.describe Rating::Config, '.rate_table' do
context 'when rating.yml does not exist' do if ENV['CONFIG_ENABLED'] != 'true'
it { expect(subject.rate_table).to eq 'rating_rates' } context 'when rating.yml does not exist' do
end if ENV['CONFIG_ENABLED'] != 'true' it { expect(subject.rate_table).to eq 'rating_rates' }
end
end
context 'when rating.yml exists' do if ENV['CONFIG_ENABLED'] == 'true'
it { expect(subject.rate_table).to eq 'reviews' } context 'when rating.yml exists' do
end if ENV['CONFIG_ENABLED'] == 'true' it { expect(subject.rate_table).to eq 'reviews' }
end
end
end end

View File

@ -3,11 +3,15 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Rating::Config, '.rating_table' do RSpec.describe Rating::Config, '.rating_table' do
context 'when rating.yml does not exist' do if ENV['CONFIG_ENABLED'] != 'true'
it { expect(subject.rating_table).to eq 'rating_ratings' } context 'when rating.yml does not exist' do
end if ENV['CONFIG_ENABLED'] != 'true' it { expect(subject.rating_table).to eq 'rating_ratings' }
end
end
context 'when rating.yml exists' do if ENV['CONFIG_ENABLED'] == 'true'
it { expect(subject.rating_table).to eq 'review_ratings' } context 'when rating.yml exists' do
end if ENV['CONFIG_ENABLED'] == 'true' it { expect(subject.rating_table).to eq 'review_ratings' }
end
end
end end

View File

@ -3,25 +3,31 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Rating::Config, '.validations' do RSpec.describe Rating::Config, '.validations' do
context 'when rating.yml does not exist' do if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] != 'true'
it do context 'when rating.yml does not exist' do
expect(subject.validations).to eq({ it do
rate: { expect(subject.validations).to eq({
case_sensitive: false, rate: {
scope: %w[author_type resource_id resource_type scopeable_id scopeable_type] case_sensitive: false,
} scope: %w[author_type resource_id resource_type scopeable_id scopeable_type],
}.deep_stringify_keys) },
}.deep_stringify_keys
)
end
end end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] != 'true' end
context 'when rating.yml exists' do if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
it do context 'when rating.yml exists' do
expect(subject.validations).to eq({ it do
rate: { expect(subject.validations).to eq({
case_sensitive: false, rate: {
scope: %w[author_type resource_id resource_type scopeable_id scopeable_type scope_1 scope_2] case_sensitive: false,
} scope: %w[author_type resource_id resource_type scopeable_id scopeable_type scope_1 scope_2],
}.deep_stringify_keys) },
}.deep_stringify_keys
)
end
end end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' end
end end

View File

@ -2,7 +2,7 @@
FactoryBot.define do FactoryBot.define do
factory :rating_rate, class: Rating::Rate do factory :rating_rate, class: Rating::Rate do
value 100 value { 100 }
author { create :author } author { create :author }
resource { create :article } resource { create :article }

View File

@ -2,10 +2,10 @@
FactoryBot.define do FactoryBot.define do
factory :rating_rating, class: Rating::Rating do factory :rating_rating, class: Rating::Rating do
average 100 average { 100 }
estimate 100 estimate { 100 }
sum 100 sum { 100 }
total 1 total { 1 }
association :resource, factory: :article, strategy: :build association :resource, factory: :article, strategy: :build
end end

View File

@ -11,7 +11,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
expect(Article.order_by_rating).to eq [ expect(Article.order_by_rating).to eq [
article_1, article_1,
article_2, article_2,
article_3 article_3,
] ]
end end
end end
@ -22,14 +22,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
expect(Article.order_by_rating(:average, :asc)).to eq [ expect(Article.order_by_rating(:average, :asc)).to eq [
article_3, article_3,
article_2, article_2,
article_1 article_1,
] ]
end end
context 'with scope' do context 'with scope' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:average, :asc, scope: category)).to eq [ expect(Article.order_by_rating(:average, :asc, scope: category)).to eq [
article_1 article_1,
] ]
end end
end end
@ -40,14 +40,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
expect(Article.order_by_rating(:average, :desc)).to eq [ expect(Article.order_by_rating(:average, :desc)).to eq [
article_1, article_1,
article_2, article_2,
article_3 article_3,
] ]
end end
context 'with scope' do context 'with scope' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:average, :desc, scope: category)).to eq [ expect(Article.order_by_rating(:average, :desc, scope: category)).to eq [
article_1 article_1,
] ]
end end
end end
@ -60,14 +60,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
expect(Article.order_by_rating(:estimate, :asc)).to eq [ expect(Article.order_by_rating(:estimate, :asc)).to eq [
article_3, article_3,
article_2, article_2,
article_1 article_1,
] ]
end end
context 'with scope' do context 'with scope' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:estimate, :asc, scope: category)).to eq [ expect(Article.order_by_rating(:estimate, :asc, scope: category)).to eq [
article_1 article_1,
] ]
end end
end end
@ -78,14 +78,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
expect(Article.order_by_rating(:estimate, :desc)).to eq [ expect(Article.order_by_rating(:estimate, :desc)).to eq [
article_1, article_1,
article_2, article_2,
article_3 article_3,
] ]
end end
context 'with scope' do context 'with scope' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:estimate, :desc, scope: category)).to eq [ expect(Article.order_by_rating(:estimate, :desc, scope: category)).to eq [
article_1 article_1,
] ]
end end
end end
@ -98,14 +98,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
expect(Article.order_by_rating(:sum, :asc)).to eq [ expect(Article.order_by_rating(:sum, :asc)).to eq [
article_3, article_3,
article_2, article_2,
article_1 article_1,
] ]
end end
context 'with scope' do context 'with scope' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:sum, :asc, scope: category)).to eq [ expect(Article.order_by_rating(:sum, :asc, scope: category)).to eq [
article_1 article_1,
] ]
end end
end end
@ -116,14 +116,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
expect(Article.order_by_rating(:sum, :desc)).to eq [ expect(Article.order_by_rating(:sum, :desc)).to eq [
article_1, article_1,
article_2, article_2,
article_3 article_3,
] ]
end end
context 'with scope' do context 'with scope' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:sum, :desc, scope: category)).to eq [ expect(Article.order_by_rating(:sum, :desc, scope: category)).to eq [
article_1 article_1,
] ]
end end
end end
@ -142,7 +142,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
context 'with scope' do context 'with scope' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:total, :asc, scope: category)).to eq [ expect(Article.order_by_rating(:total, :asc, scope: category)).to eq [
article_1 article_1,
] ]
end end
end end
@ -159,7 +159,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
context 'with scope' do context 'with scope' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:total, :desc, scope: category)).to eq [ expect(Article.order_by_rating(:total, :desc, scope: category)).to eq [
article_1 article_1,
] ]
end end
end end

View File

@ -32,15 +32,17 @@ RSpec.describe Rating::Extension, ':rated?' do
end end
end end
context 'with extra scopes' do if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
before { author.rate resource, 1, extra_scopes: { scope_1: 'scope_1' } } 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 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 } specify { expect(author.rated?(resource, extra_scopes: { scope_1: 'missing' })).to eq false }
end end
context 'when has rate for the given resource with given extra scopes' do 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 } specify { expect(author.rated?(resource, extra_scopes: { scope_1: 'scope_1' })).to eq true }
end
end end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' end
end end

View File

@ -38,11 +38,13 @@ RSpec.describe Rating::Extension, ':rated' do
end end
end end
context 'with extra scopes' do if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
let!(:extra_scopes_rate) { author_1.rate article_1, 1, extra_scopes: { scope_1: 'scope_1' } } 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 it 'returns records considering the extra scopes' do
expect(author_1.rated(extra_scopes: { scope_1: 'scope_1' })).to eq [extra_scopes_rate] expect(author_1.rated(extra_scopes: { scope_1: 'scope_1' })).to eq [extra_scopes_rate]
end
end end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' end
end end

View File

@ -38,11 +38,13 @@ RSpec.describe Rating::Extension, ':rates' do
end end
end end
context 'with extra scopes' do if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
let!(:extra_scopes_rate) { author_1.rate article_1, 1, extra_scopes: { scope_1: 'scope_1' } } 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 it 'returns records considering the extra scopes' do
expect(article_1.rates(extra_scopes: { scope_1: 'scope_1' })).to eq [extra_scopes_rate] expect(article_1.rates(extra_scopes: { scope_1: 'scope_1' })).to eq [extra_scopes_rate]
end
end end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' end
end end

View File

@ -194,112 +194,11 @@ RSpec.describe Rating::Rate, ':create' do
end end
end end
context 'with extra scopes' do if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
let!(:category) { create :category } context 'with extra scopes' do
let!(:category) { create :category }
it 'creates a rate entry' do 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
described_class.create( described_class.create(
author: author, author: author,
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
@ -309,22 +208,7 @@ RSpec.describe Rating::Rate, ':create' do
value: 1 value: 1
) )
described_class.create( rate = described_class.last
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.author).to eq author
expect(rate.resource).to eq article 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.scope_2).to eq 'scope_2'
expect(rate.scopeable).to eq category expect(rate.scopeable).to eq category
expect(rate.value).to eq 1 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 end
it 'updates the unique rating entry' do it 'creates a rating entry' do
ratings = Rating::Rating.all 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
expect(rating.estimate).to eq 1
expect(rating.average).to eq 1.5
expect(rating.estimate).to eq 1.5
expect(rating.resource).to eq article expect(rating.resource).to eq article
expect(rating.scopeable).to eq category expect(rating.scopeable).to eq category
expect(rating.sum).to eq 3 expect(rating.sum).to eq 1
expect(rating.total).to eq 2 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 end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' end
end end

View File

@ -51,55 +51,57 @@ RSpec.describe Rating::Rate, ':rate_for' do
end end
end end
context 'with extra scopes' do if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
let!(:category) { create :category } context 'with extra scopes' do
let!(:category) { create :category }
context 'when matches all attributes including the extra scopes' do context 'when matches all attributes including the extra scopes' do
let!(:record) do let!(:record) do
described_class.create( described_class.create(
author: author, author: author,
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
metadata: {}, metadata: {},
resource: article, resource: article,
scopeable: category, scopeable: category,
value: 1 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 end
it 'returns the record' do context 'when matches all attributes but at least one extra scopes' do
result = described_class.rate_for( before do
author: author, described_class.create(
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' }, author: author,
resource: article, extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
scopeable: category 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
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

View File

@ -16,7 +16,7 @@ RSpec.describe Rating::Rate do
it { is_expected.to validate_presence_of :value } it { is_expected.to validate_presence_of :value }
it do 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 end
it do it do