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
if ENV['CONFIG_ENABLED'] != 'true'
context 'when rating.yml does not exist' do context 'when rating.yml does not exist' do
it { expect(subject.rate_table).to eq 'rating_rates' } it { expect(subject.rate_table).to eq 'rating_rates' }
end if ENV['CONFIG_ENABLED'] != 'true' end
end
if ENV['CONFIG_ENABLED'] == 'true'
context 'when rating.yml exists' do context 'when rating.yml exists' do
it { expect(subject.rate_table).to eq 'reviews' } it { expect(subject.rate_table).to eq 'reviews' }
end if ENV['CONFIG_ENABLED'] == 'true' 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
if ENV['CONFIG_ENABLED'] != 'true'
context 'when rating.yml does not exist' do context 'when rating.yml does not exist' do
it { expect(subject.rating_table).to eq 'rating_ratings' } it { expect(subject.rating_table).to eq 'rating_ratings' }
end if ENV['CONFIG_ENABLED'] != 'true' end
end
if ENV['CONFIG_ENABLED'] == 'true'
context 'when rating.yml exists' do context 'when rating.yml exists' do
it { expect(subject.rating_table).to eq 'review_ratings' } it { expect(subject.rating_table).to eq 'review_ratings' }
end if ENV['CONFIG_ENABLED'] == 'true' 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
if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] != 'true'
context 'when rating.yml does not exist' do context 'when rating.yml does not exist' do
it do it do
expect(subject.validations).to eq({ expect(subject.validations).to eq({
rate: { rate: {
case_sensitive: false, case_sensitive: false,
scope: %w[author_type resource_id resource_type scopeable_id scopeable_type] scope: %w[author_type resource_id resource_type scopeable_id scopeable_type],
} },
}.deep_stringify_keys) }.deep_stringify_keys
)
end
end
end end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] != 'true'
if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
context 'when rating.yml exists' do context 'when rating.yml exists' do
it do it do
expect(subject.validations).to eq({ expect(subject.validations).to eq({
rate: { rate: {
case_sensitive: false, case_sensitive: false,
scope: %w[author_type resource_id resource_type scopeable_id scopeable_type scope_1 scope_2] 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
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
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,6 +32,7 @@ RSpec.describe Rating::Extension, ':rated?' do
end end
end end
if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
context 'with extra scopes' do context 'with extra scopes' do
before { author.rate resource, 1, extra_scopes: { scope_1: 'scope_1' } } before { author.rate resource, 1, extra_scopes: { scope_1: 'scope_1' } }
@ -42,5 +43,6 @@ RSpec.describe Rating::Extension, ':rated?' do
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 if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' end
end
end end

View File

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

View File

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

View File

@ -194,6 +194,7 @@ RSpec.describe Rating::Rate, ':create' do
end end
end end
if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
context 'with extra scopes' do context 'with extra scopes' do
let!(:category) { create :category } let!(:category) { create :category }
@ -358,5 +359,6 @@ RSpec.describe Rating::Rate, ':create' do
expect(rating.total).to eq 2 expect(rating.total).to eq 2
end end
end end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' end
end
end end

View File

@ -51,6 +51,7 @@ RSpec.describe Rating::Rate, ':rate_for' do
end end
end end
if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
context 'with extra scopes' do context 'with extra scopes' do
let!(:category) { create :category } let!(:category) { create :category }
@ -101,5 +102,6 @@ RSpec.describe Rating::Rate, ':rate_for' do
expect(result).to eq nil expect(result).to eq nil
end end
end end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' end
end
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