Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
wbotelhos committed Jan 26, 2018
1 parent 038a0d2 commit 286b03b
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 92 deletions.
55 changes: 20 additions & 35 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,47 @@ require: rubocop-rspec

inherit_from: .rubocop_todo.yml

AllCops:
Exclude:
- generators/rating/templates/**/*

Rails:
Enabled: true

Lint/Debugger:
Enabled: false
Layout/AlignParameters:
EnforcedStyle: with_fixed_indentation

Metrics/AbcSize:
Layout/EmptyLinesAroundArguments:
Enabled: false

Metrics/ClassLength:
Rails/ApplicationRecord:
Enabled: false

Metrics/CyclomaticComplexity:
RSpec/DescribeMethod:
Enabled: false

Metrics/LineLength:
Enabled: false
RSpec/ExampleLength:
Max: 10

Metrics/MethodLength:
RSpec/FilePath:
Enabled: false

Metrics/PerceivedComplexity:
RSpec/MultipleExpectations:
Enabled: false

Rails/HasAndBelongsToMany:
Enabled: false
RSpec/NestedGroups:
Max: 5

Rails/HttpPositionalArguments:
Enabled: false
Metrics/LineLength:
Max: 120

Naming/VariableNumber:
EnforcedStyle: snake_case

Style/IfUnlessModifier:
MaxLineLength: 0
Style/Documentation:
Enabled: false

Style/PercentLiteralDelimiters:
PreferredDelimiters:
'%': '()'
'%i': '[]'
'%I': '[]'
'%r': ()
'%r': '()'
'%w': '[]'
'%W': '[]'

Style/AlignParameters:
EnforcedStyle: with_fixed_indentation

Style/Documentation:
Enabled: false

Style/Encoding:
Enabled: false

Style/SpaceBeforeComma:
Enabled: false

Style/SpaceBeforeFirstArg:
Enabled: false
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ def change
create_table :rating_rates do |t|
t.decimal :value, default: 0, precision: 17, scale: 14

t.references :author , index: true, null: false, polymorphic: true
t.references :resource , index: true, null: false, polymorphic: true
t.references :scopeable, index: true, null: true , polymorphic: true
t.references :author, index: true, null: false, polymorphic: true
t.references :resource, index: true, null: false, polymorphic: true
t.references :scopeable, index: true, null: true, polymorphic: true

t.timestamps null: false
end
Expand All @@ -17,13 +17,13 @@ def change
unique: true

create_table :rating_ratings do |t|
t.decimal :average , default: 0, mull: false, precision: 17, scale: 14
t.decimal :average, default: 0, mull: false, precision: 17, scale: 14
t.decimal :estimate, default: 0, mull: false, precision: 17, scale: 14
t.integer :sum , default: 0, mull: false
t.integer :total , default: 0, mull: false
t.integer :sum, default: 0, mull: false
t.integer :total, default: 0, mull: false

t.references :resource , index: true, null: false, polymorphic: true
t.references :scopeable, index: true, null: true , polymorphic: true
t.references :resource, index: true, null: false, polymorphic: true
t.references :scopeable, index: true, null: true, polymorphic: true

t.timestamps null: false
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rating/models/rating/rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Rate < ActiveRecord::Base

after_save :update_rating

belongs_to :author , polymorphic: true
belongs_to :resource , polymorphic: true
belongs_to :author, polymorphic: true
belongs_to :resource, polymorphic: true
belongs_to :scopeable, polymorphic: true

validates :author, :resource, :value, presence: true
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 @@ -4,7 +4,7 @@ module Rating
class Rating < ActiveRecord::Base
self.table_name = 'rating_ratings'

belongs_to :resource , polymorphic: true
belongs_to :resource, polymorphic: true
belongs_to :scopeable, polymorphic: true

validates :average, :estimate, :resource, :sum, :total, presence: true
Expand All @@ -18,7 +18,7 @@ class Rating < ActiveRecord::Base
class << self
def averager_data(resource, scopeable)
total_count = how_many_resource_received_votes_sql?(distinct: false, scopeable: scopeable)
distinct_count = how_many_resource_received_votes_sql?(distinct: true , scopeable: scopeable)
distinct_count = how_many_resource_received_votes_sql?(distinct: true, scopeable: scopeable)
values = { resource_type: resource.class.base_class.name }

values[:scopeable_type] = scopeable.class.base_class.name unless scopeable.nil?
Expand Down
22 changes: 11 additions & 11 deletions spec/models/extension/order_by_rating_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
end
end

context 'filtering by :average' do
context 'as asc' do
context 'when filtering by :average' do
context 'with as asc' do
it 'works' do
expect(Article.order_by_rating(:average, :asc)).to eq [
article_3,
Expand All @@ -35,7 +35,7 @@
end
end

context 'as desc' do
context 'with as desc' do
it 'works' do
expect(Article.order_by_rating(:average, :desc)).to eq [
article_1,
Expand All @@ -54,8 +54,8 @@
end
end

context 'filtering by :estimate' do
context 'as asc' do
context 'when filtering by :estimate' do
context 'with as asc' do
it 'works' do
expect(Article.order_by_rating(:estimate, :asc)).to eq [
article_3,
Expand All @@ -73,7 +73,7 @@
end
end

context 'as desc' do
context 'with as desc' do
it 'works' do
expect(Article.order_by_rating(:estimate, :desc)).to eq [
article_1,
Expand All @@ -92,7 +92,7 @@
end
end

context 'filtering by :sum' do
context 'when filtering by :sum' do
context 'as asc' do
it 'works' do
expect(Article.order_by_rating(:sum, :asc)).to eq [
Expand All @@ -111,7 +111,7 @@
end
end

context 'as desc' do
context 'with as desc' do
it 'works' do
expect(Article.order_by_rating(:sum, :desc)).to eq [
article_1,
Expand All @@ -130,8 +130,8 @@
end
end

context 'filtering by :total' do
context 'as asc' do
context 'when filtering by :total' do
context 'with as asc' do
it 'works' do
result = Article.order_by_rating(:total, :asc)

Expand All @@ -148,7 +148,7 @@
end
end

context 'as desc' do
context 'with as desc' do
it 'works' do
result = Article.order_by_rating(:total, :desc)

Expand Down
8 changes: 4 additions & 4 deletions spec/models/extension/rated_question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

context 'with no scopeable' do
context 'when has no rate for the given resource' do
before { allow(author).to receive(:rate_for).with(article, scope: nil) { nil } }
before { allow(author).to receive(:rate_for).with(article, scope: nil).and_return nil }

specify { expect(author.rated?(article)).to eq false }
end

context 'when has rate for the given resource' do
before { allow(author).to receive(:rate_for).with(article, scope: nil) { double } }
before { allow(author).to receive(:rate_for).with(article, scope: nil).and_return double }

specify { expect(author.rated?(article)).to eq true }
end
Expand All @@ -24,13 +24,13 @@
let!(:category) { build :category }

context 'when has no rate for the given resource' do
before { allow(author).to receive(:rate_for).with(article, scope: category) { nil } }
before { allow(author).to receive(:rate_for).with(article, scope: category).and_return nil }

specify { expect(author.rated?(article, scope: category)).to eq false }
end

context 'when has rate for the given resource' do
before { allow(author).to receive(:rate_for).with(article, scope: category) { double } }
before { allow(author).to receive(:rate_for).with(article, scope: category).and_return double }

specify { expect(author.rated?(article, scope: category)).to eq true }
end
Expand Down
8 changes: 2 additions & 6 deletions spec/models/extension/rated_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,21 @@
end

context 'when destroy author' do
before do
it 'destroys rates of that author' do
expect(Rating::Rate.where(author: author_1).count).to eq 4

author_1.destroy!
end

it 'destroys rates of that author' do
expect(Rating::Rate.where(author: author_1).count).to eq 0
end
end

context 'when destroy resource rated by author' do
before do
it 'destroys rates of that resource' do
expect(Rating::Rate.where(resource: article_1).count).to eq 4

article_1.destroy!
end

it 'destroys rates of that resource' do
expect(Rating::Rate.where(resource: article_1).count).to eq 0
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/models/rating/averager_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
include_context 'with_database_records'

context 'with no scopeable' do
subject { described_class.averager_data article_1, nil }
subject(:result) { described_class.averager_data article_1, nil }

it 'returns the values average of given resource type' do
expect(subject.as_json['rating_avg']).to eq 30.5
expect(result.as_json['rating_avg']).to eq 30.5
end

it 'returns the average of number of records for the given resource type' do
expect(subject.as_json['count_avg']).to eq 1.3333333333333333
expect(result.as_json['count_avg']).to eq 1.3333333333333333
end
end

context 'with scopeable' do
subject { described_class.averager_data article_1, category }
subject(:result) { described_class.averager_data article_1, category }

it 'returns the values average of given resource type' do
expect(subject.as_json['rating_avg']).to eq 1.5
expect(result.as_json['rating_avg']).to eq 1.5
end

it 'returns the average of number of records for the given resource type' do
expect(subject.as_json['count_avg']).to eq 2
expect(result.as_json['count_avg']).to eq 2
end
end
end
20 changes: 10 additions & 10 deletions spec/models/rating/data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@
include_context 'with_database_records'

context 'with no scopeable' do
subject { described_class.data article_1, nil }
subject(:result) { described_class.data article_1, nil }

it 'returns the average of value for a resource' do
expect(subject[:average]).to eq 50.5
expect(result[:average]).to eq 50.5
end

it 'returns the sum of values for a resource' do
expect(subject[:sum]).to eq 101
expect(result[:sum]).to eq 101
end

it 'returns the count of votes for a resource' do
expect(subject[:total]).to eq 2
expect(result[:total]).to eq 2
end

it 'returns the estimate for a resource' do
expect(subject[:estimate]).to eq 42.50000000000001
expect(result[:estimate]).to eq 42.50000000000001
end
end

context 'with scopeable' do
subject { described_class.data article_1, category }
subject(:result) { described_class.data article_1, category }

it 'returns the average of value for a resource' do
expect(subject[:average]).to eq 1.5
expect(result[:average]).to eq 1.5
end

it 'returns the sum of values for a resource' do
expect(subject[:sum]).to eq 3
expect(result[:sum]).to eq 3
end

it 'returns the count of votes for a resource' do
expect(subject[:total]).to eq 2
expect(result[:total]).to eq 2
end

it 'returns the estimate for a resource' do
expect(subject[:estimate]).to eq 1.5
expect(result[:estimate]).to eq 1.5
end
end
end
Loading

0 comments on commit 286b03b

Please sign in to comment.