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
AllCops:
Exclude:
- generators/rating/templates/**/*
Rails:
Enabled: true
Lint/Debugger:
Layout/AlignParameters:
EnforcedStyle: with_fixed_indentation
Layout/EmptyLinesAroundArguments:
Enabled: false
Metrics/AbcSize:
Rails/ApplicationRecord:
Enabled: false
Metrics/ClassLength:
RSpec/DescribeMethod:
Enabled: false
Metrics/CyclomaticComplexity:
RSpec/ExampleLength:
Max: 10
RSpec/FilePath:
Enabled: false
RSpec/MultipleExpectations:
Enabled: false
RSpec/NestedGroups:
Max: 5
Metrics/LineLength:
Enabled: false
Max: 120
Metrics/MethodLength:
Enabled: false
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
Naming/VariableNumber:
EnforcedStyle: snake_case
Style/Documentation:
Enabled: false
Style/Encoding:
Enabled: false
Style/SpaceBeforeComma:
Enabled: false
Style/SpaceBeforeFirstArg:
Enabled: false
Style/PercentLiteralDelimiters:
PreferredDelimiters:
'%': '()'
'%i': '[]'
'%I': '[]'
'%r': '()'
'%w': '[]'
'%W': '[]'

View File

@ -5,9 +5,9 @@ class CreateRatingTables < ActiveRecord::Migration[5.0]
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
@ -17,13 +17,13 @@ class CreateRatingTables < ActiveRecord::Migration[5.0]
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

View File

@ -6,8 +6,8 @@ module Rating
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

View File

@ -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
@ -18,7 +18,7 @@ module Rating
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?

View File

@ -16,8 +16,8 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
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,
@ -35,7 +35,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
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,
@ -54,8 +54,8 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
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,
@ -73,7 +73,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
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,
@ -92,7 +92,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
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 [
@ -111,7 +111,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
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,
@ -130,8 +130,8 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
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)
@ -148,7 +148,7 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
end
end
context 'as desc' do
context 'with as desc' do
it 'works' do
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 '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
@ -24,13 +24,13 @@ RSpec.describe Rating::Extension, ':rated?' do
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

View File

@ -19,25 +19,21 @@ RSpec.describe Rating::Extension, ':rated' do
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

View File

@ -7,26 +7,26 @@ RSpec.describe Rating::Rating, ':averager_data' do
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

View File

@ -7,42 +7,42 @@ RSpec.describe Rating::Rating, ':data' do
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

View File

@ -7,34 +7,34 @@ RSpec.describe Rating::Rating, ':values_data' do
include_context 'with_database_records'
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
expect(subject.as_json['rating_avg']).to eq 50.5
expect(result.as_json['rating_avg']).to eq 50.5
end
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
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
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
expect(subject.as_json['rating_avg']).to eq 1.5
expect(result.as_json['rating_avg']).to eq 1.5
end
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
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

View File

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