main
Washington Botelho 2018-01-26 19:58:12 -02:00
parent 038a0d20cb
commit 286b03be63
No known key found for this signature in database
GPG Key ID: 5DE4F42A8F073617
11 changed files with 82 additions and 99 deletions

View File

@ -2,62 +2,47 @@ require: rubocop-rspec
inherit_from: .rubocop_todo.yml inherit_from: .rubocop_todo.yml
AllCops:
Exclude:
- generators/rating/templates/**/*
Rails: Rails:
Enabled: true Enabled: true
Lint/Debugger: Layout/AlignParameters:
EnforcedStyle: with_fixed_indentation
Layout/EmptyLinesAroundArguments:
Enabled: false Enabled: false
Metrics/AbcSize: Rails/ApplicationRecord:
Enabled: false Enabled: false
Metrics/ClassLength: RSpec/DescribeMethod:
Enabled: false Enabled: false
Metrics/CyclomaticComplexity: RSpec/ExampleLength:
Max: 10
RSpec/FilePath:
Enabled: false Enabled: false
RSpec/MultipleExpectations:
Enabled: false
RSpec/NestedGroups:
Max: 5
Metrics/LineLength: Metrics/LineLength:
Enabled: false Max: 120
Metrics/MethodLength: Naming/VariableNumber:
Enabled: false EnforcedStyle: snake_case
Metrics/PerceivedComplexity:
Enabled: false
Rails/HasAndBelongsToMany:
Enabled: false
Rails/HttpPositionalArguments:
Enabled: false
Style/IfUnlessModifier:
MaxLineLength: 0
Style/PercentLiteralDelimiters:
PreferredDelimiters:
'%i': '[]'
'%I': '[]'
'%r': ()
'%w': '[]'
'%W': '[]'
Style/AlignParameters:
EnforcedStyle: with_fixed_indentation
Style/Documentation: Style/Documentation:
Enabled: false Enabled: false
Style/Encoding: Style/PercentLiteralDelimiters:
Enabled: false PreferredDelimiters:
'%': '()'
Style/SpaceBeforeComma: '%i': '[]'
Enabled: false '%I': '[]'
'%r': '()'
Style/SpaceBeforeFirstArg: '%w': '[]'
Enabled: false '%W': '[]'

View File

@ -5,9 +5,9 @@ class CreateRatingTables < ActiveRecord::Migration[5.0]
create_table :rating_rates do |t| create_table :rating_rates do |t|
t.decimal :value, default: 0, precision: 17, scale: 14 t.decimal :value, default: 0, precision: 17, scale: 14
t.references :author , index: true, null: false, polymorphic: true t.references :author, index: true, null: false, polymorphic: true
t.references :resource , 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 :scopeable, index: true, null: true, polymorphic: true
t.timestamps null: false t.timestamps null: false
end end
@ -17,13 +17,13 @@ class CreateRatingTables < ActiveRecord::Migration[5.0]
unique: true unique: true
create_table :rating_ratings do |t| 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.decimal :estimate, default: 0, mull: false, precision: 17, scale: 14
t.integer :sum , default: 0, mull: false t.integer :sum, default: 0, mull: false
t.integer :total , default: 0, mull: false t.integer :total, default: 0, mull: false
t.references :resource , 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 :scopeable, index: true, null: true, polymorphic: true
t.timestamps null: false t.timestamps null: false
end end

View File

@ -6,8 +6,8 @@ module Rating
after_save :update_rating after_save :update_rating
belongs_to :author , polymorphic: true belongs_to :author, polymorphic: true
belongs_to :resource , polymorphic: true belongs_to :resource, polymorphic: true
belongs_to :scopeable, polymorphic: true belongs_to :scopeable, polymorphic: true
validates :author, :resource, :value, presence: true validates :author, :resource, :value, presence: true

View File

@ -4,7 +4,7 @@ module Rating
class Rating < ActiveRecord::Base class Rating < ActiveRecord::Base
self.table_name = 'rating_ratings' self.table_name = 'rating_ratings'
belongs_to :resource , polymorphic: true belongs_to :resource, polymorphic: true
belongs_to :scopeable, polymorphic: true belongs_to :scopeable, polymorphic: true
validates :average, :estimate, :resource, :sum, :total, presence: true validates :average, :estimate, :resource, :sum, :total, presence: true
@ -18,7 +18,7 @@ module Rating
class << self class << self
def averager_data(resource, scopeable) def averager_data(resource, scopeable)
total_count = how_many_resource_received_votes_sql?(distinct: false, scopeable: 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 = { resource_type: resource.class.base_class.name }
values[:scopeable_type] = scopeable.class.base_class.name unless scopeable.nil? values[:scopeable_type] = scopeable.class.base_class.name unless scopeable.nil?

View File

@ -16,8 +16,8 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
end end
end end
context 'filtering by :average' do context 'when filtering by :average' do
context 'as asc' do context 'with as asc' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:average, :asc)).to eq [ expect(Article.order_by_rating(:average, :asc)).to eq [
article_3, article_3,
@ -35,7 +35,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
end end
end end
context 'as desc' do context 'with as desc' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:average, :desc)).to eq [ expect(Article.order_by_rating(:average, :desc)).to eq [
article_1, article_1,
@ -54,8 +54,8 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
end end
end end
context 'filtering by :estimate' do context 'when filtering by :estimate' do
context 'as asc' do context 'with as asc' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:estimate, :asc)).to eq [ expect(Article.order_by_rating(:estimate, :asc)).to eq [
article_3, article_3,
@ -73,7 +73,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
end end
end end
context 'as desc' do context 'with as desc' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:estimate, :desc)).to eq [ expect(Article.order_by_rating(:estimate, :desc)).to eq [
article_1, article_1,
@ -92,7 +92,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
end end
end end
context 'filtering by :sum' do context 'when filtering by :sum' do
context 'as asc' do context 'as asc' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:sum, :asc)).to eq [ expect(Article.order_by_rating(:sum, :asc)).to eq [
@ -111,7 +111,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
end end
end end
context 'as desc' do context 'with as desc' do
it 'works' do it 'works' do
expect(Article.order_by_rating(:sum, :desc)).to eq [ expect(Article.order_by_rating(:sum, :desc)).to eq [
article_1, article_1,
@ -130,8 +130,8 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
end end
end end
context 'filtering by :total' do context 'when filtering by :total' do
context 'as asc' do context 'with as asc' do
it 'works' do it 'works' do
result = Article.order_by_rating(:total, :asc) result = Article.order_by_rating(:total, :asc)
@ -148,7 +148,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
end end
end end
context 'as desc' do context 'with as desc' do
it 'works' do it 'works' do
result = Article.order_by_rating(:total, :desc) result = Article.order_by_rating(:total, :desc)

View File

@ -8,13 +8,13 @@ RSpec.describe Rating::Extension, ':rated?' do
context 'with no scopeable' do context 'with no scopeable' do
context 'when has no rate for the given resource' 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 } specify { expect(author.rated?(article)).to eq false }
end end
context 'when has rate for the given resource' do 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 } specify { expect(author.rated?(article)).to eq true }
end end
@ -24,13 +24,13 @@ RSpec.describe Rating::Extension, ':rated?' do
let!(:category) { build :category } let!(:category) { build :category }
context 'when has no rate for the given resource' do 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 } specify { expect(author.rated?(article, scope: category)).to eq false }
end end
context 'when has rate for the given resource' do 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 } specify { expect(author.rated?(article, scope: category)).to eq true }
end end

View File

@ -19,25 +19,21 @@ RSpec.describe Rating::Extension, ':rated' do
end end
context 'when destroy author' do 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 expect(Rating::Rate.where(author: author_1).count).to eq 4
author_1.destroy! author_1.destroy!
end
it 'destroys rates of that author' do
expect(Rating::Rate.where(author: author_1).count).to eq 0 expect(Rating::Rate.where(author: author_1).count).to eq 0
end end
end end
context 'when destroy resource rated by author' do 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 expect(Rating::Rate.where(resource: article_1).count).to eq 4
article_1.destroy! article_1.destroy!
end
it 'destroys rates of that resource' do
expect(Rating::Rate.where(resource: article_1).count).to eq 0 expect(Rating::Rate.where(resource: article_1).count).to eq 0
end end
end end

View File

@ -7,26 +7,26 @@ RSpec.describe Rating::Rating, ':averager_data' do
include_context 'with_database_records' include_context 'with_database_records'
context 'with no scopeable' do 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 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 end
it 'returns the average of number of records for the given resource type' do 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
end end
context 'with scopeable' do 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 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 end
it 'returns the average of number of records for the given resource type' do 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 end
end end

View File

@ -7,42 +7,42 @@ RSpec.describe Rating::Rating, ':data' do
include_context 'with_database_records' include_context 'with_database_records'
context 'with no scopeable' do 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 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 end
it 'returns the sum of values for a resource' do it 'returns the sum of values for a resource' do
expect(subject[:sum]).to eq 101 expect(result[:sum]).to eq 101
end end
it 'returns the count of votes for a resource' do it 'returns the count of votes for a resource' do
expect(subject[:total]).to eq 2 expect(result[:total]).to eq 2
end end
it 'returns the estimate for a resource' do it 'returns the estimate for a resource' do
expect(subject[:estimate]).to eq 42.50000000000001 expect(result[:estimate]).to eq 42.50000000000001
end end
end end
context 'with scopeable' do 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 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 end
it 'returns the sum of values for a resource' do it 'returns the sum of values for a resource' do
expect(subject[:sum]).to eq 3 expect(result[:sum]).to eq 3
end end
it 'returns the count of votes for a resource' do it 'returns the count of votes for a resource' do
expect(subject[:total]).to eq 2 expect(result[:total]).to eq 2
end end
it 'returns the estimate for a resource' do 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 end
end end

View File

@ -7,34 +7,34 @@ RSpec.describe Rating::Rating, ':values_data' do
include_context 'with_database_records' include_context 'with_database_records'
context 'with no scopeable' do context 'with no scopeable' do
subject { described_class.values_data article_1, nil } subject(:result) { described_class.values_data article_1, nil }
it 'returns the average of value for a resource' do it 'returns the average of value for a resource' do
expect(subject.as_json['rating_avg']).to eq 50.5 expect(result.as_json['rating_avg']).to eq 50.5
end end
it 'returns the sum of values for a resource' do it 'returns the sum of values for a resource' do
expect(subject.as_json['rating_sum']).to eq 101 expect(result.as_json['rating_sum']).to eq 101
end end
it 'returns the count of votes for a resource' do it 'returns the count of votes for a resource' do
expect(subject.as_json['rating_count']).to eq 2 expect(result.as_json['rating_count']).to eq 2
end end
end end
context 'with scopeable' do context 'with scopeable' do
subject { described_class.values_data article_1, category } subject(:result) { described_class.values_data article_1, category }
it 'returns the average of value for a resource' do it 'returns the average of value for a resource' do
expect(subject.as_json['rating_avg']).to eq 1.5 expect(result.as_json['rating_avg']).to eq 1.5
end end
it 'returns the sum of values for a resource' do it 'returns the sum of values for a resource' do
expect(subject.as_json['rating_sum']).to eq 3 expect(result.as_json['rating_sum']).to eq 3
end end
it 'returns the count of votes for a resource' do it 'returns the count of votes for a resource' do
expect(subject.as_json['rating_count']).to eq 2 expect(result.as_json['rating_count']).to eq 2
end end
end end
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
RSpec.shared_context 'with_database_records' do RSpec.shared_context 'with_database_records' do
let!(:category) { create :category } let!(:category) { create :category }