Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
wbotelhos committed Oct 3, 2019
1 parent 149e3f8 commit 63a0f7e
Show file tree
Hide file tree
Showing 19 changed files with 316 additions and 265 deletions.
51 changes: 39 additions & 12 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
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

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
Expand All @@ -29,12 +53,6 @@ RSpec/MultipleExpectations:
RSpec/NestedGroups:
Max: 5

Metrics/LineLength:
Max: 120

Naming/VariableNumber:
EnforcedStyle: snake_case

Style/Documentation:
Enabled: false

Expand All @@ -46,3 +64,12 @@ Style/PercentLiteralDelimiters:
'%r': '()'
'%w': '[]'
'%W': '[]'

Style/PerlBackrefs:
Enabled: false

Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: comma

Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: comma
2 changes: 1 addition & 1 deletion lib/generators/rating/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
4 changes: 2 additions & 2 deletions lib/rating/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def validations
{
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
Expand Down
2 changes: 1 addition & 1 deletion lib/rating/models/rating/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def rating(options = {})
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}")
Expand Down
2 changes: 1 addition & 1 deletion lib/rating/models/rating/rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Rate < ActiveRecord::Base

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:)
Expand Down
4 changes: 2 additions & 2 deletions lib/rating/models/rating/rating.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Rating < ActiveRecord::Base

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
Expand Down Expand Up @@ -43,7 +43,7 @@ def data(resource, scopeable)
average: values.rating_avg,
estimate: estimate(averager, values),
sum: values.rating_sum,
total: values.rating_count
total: values.rating_count,
}
end

Expand Down
2 changes: 1 addition & 1 deletion rating.gemspec
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
16 changes: 10 additions & 6 deletions spec/config/rate_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 10 additions & 6 deletions spec/config/rating_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 24 additions & 18 deletions spec/config/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/factories/rating/rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

FactoryBot.define do
factory :rating_rate, class: Rating::Rate do
value 100
value { 100 }

author { create :author }
resource { create :article }
Expand Down
8 changes: 4 additions & 4 deletions spec/factories/rating/rating.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions spec/models/extension/order_by_rating_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
expect(Article.order_by_rating).to eq [
article_1,
article_2,
article_3
article_3,
]
end
end
Expand All @@ -22,14 +22,14 @@
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
Expand All @@ -40,14 +40,14 @@
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
Expand All @@ -60,14 +60,14 @@
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
Expand All @@ -78,14 +78,14 @@
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
Expand All @@ -98,14 +98,14 @@
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
Expand All @@ -116,14 +116,14 @@
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
Expand All @@ -142,7 +142,7 @@
context 'with scope' do
it 'works' do
expect(Article.order_by_rating(:total, :asc, scope: category)).to eq [
article_1
article_1,
]
end
end
Expand All @@ -159,7 +159,7 @@
context 'with scope' do
it 'works' do
expect(Article.order_by_rating(:total, :desc, scope: category)).to eq [
article_1
article_1,
]
end
end
Expand Down
Loading

0 comments on commit 63a0f7e

Please sign in to comment.