feat: adds extra scopes option

main
Washington Botelho 2018-03-01 17:50:28 -03:00
parent 1365f948db
commit f9de473ea4
No known key found for this signature in database
GPG Key ID: 5DE4F42A8F073617
10 changed files with 99 additions and 42 deletions

View File

@ -5,24 +5,36 @@ module Rating
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
def rate(resource, value, author: self, metadata: {}, scope: nil) def rate(resource, value, author: self, extra_scopes: {}, metadata: {}, scope: nil)
Rate.create author: author, metadata: metadata, resource: resource, scopeable: scope, value: value Rate.create(
author: author,
extra_scopes: extra_scopes,
metadata: metadata,
resource: resource,
scopeable: scope,
value: value
)
end end
def rate_for(resource, scope: nil) def rate_for(resource, extra_scopes: {}, scope: nil)
Rate.rate_for author: self, resource: resource, scopeable: scope Rate.rate_for author: self, extra_scopes: extra_scopes, resource: resource, scopeable: scope
end end
def rated?(resource, scope: nil) # TODO: use exists for performance
!rate_for(resource, scope: scope).nil? def rated?(resource, extra_scopes: {}, scope: nil)
!rate_for(resource, extra_scopes: extra_scopes, scope: scope).nil?
end end
def rates(scope: nil) def rates(extra_scopes: {}, scope: nil)
rates_records.where scopeable: scope attributes = { scopeable: scope }.merge(extra_scopes)
rates_records.where attributes
end end
def rated(scope: nil) def rated(extra_scopes: {}, scope: nil)
rated_records.where scopeable: scope attributes = { scopeable: scope }.merge(extra_scopes)
rated_records.where attributes
end end
def rating(scope: nil) def rating(scope: nil)

View File

@ -8,7 +8,7 @@ RSpec.describe Rating::Extension, ':rate_for' do
context 'with no scopeable' do context 'with no scopeable' do
it 'delegates to rate object' do it 'delegates to rate object' do
expect(Rating::Rate).to receive(:rate_for).with author: author, resource: article, scopeable: nil expect(Rating::Rate).to receive(:rate_for).with author: author, extra_scopes: {}, resource: article, scopeable: nil
author.rate_for article author.rate_for article
end end
@ -18,9 +18,24 @@ RSpec.describe Rating::Extension, ':rate_for' do
let!(:category) { build :category } let!(:category) { build :category }
it 'delegates to rate object' do it 'delegates to rate object' do
expect(Rating::Rate).to receive(:rate_for).with author: author, resource: article, scopeable: category expect(Rating::Rate).to receive(:rate_for).with author: author, extra_scopes: {}, resource: article, scopeable: category
author.rate_for article, scope: category author.rate_for article, scope: category
end end
end end
context 'with extra_scopes' do
let!(:category) { build :category }
it 'delegates to rate object' do
expect(Rating::Rate).to receive(:rate_for).with(
author: author,
extra_scopes: { scope_1: 'scope_1' },
resource: article,
scopeable: category
)
author.rate_for article, extra_scopes: { scope_1: 'scope_1' }, scope: category
end
end
end end

View File

@ -9,7 +9,7 @@ RSpec.describe Rating::Extension, ':rate' do
context 'with no scopeable' do context 'with no scopeable' do
it 'delegates to rate object' do it 'delegates to rate object' do
expect(Rating::Rate).to receive(:create).with( expect(Rating::Rate).to receive(:create).with(
author: author, metadata: {}, resource: article, scopeable: nil, value: 3 author: author, extra_scopes: {}, metadata: {}, resource: article, scopeable: nil, value: 3
) )
author.rate article, 3 author.rate article, 3
@ -21,7 +21,7 @@ RSpec.describe Rating::Extension, ':rate' do
it 'delegates to rate object' do it 'delegates to rate object' do
expect(Rating::Rate).to receive(:create).with( expect(Rating::Rate).to receive(:create).with(
author: author, metadata: {}, resource: article, scopeable: category, value: 3 author: author, extra_scopes: {}, metadata: {}, resource: article, scopeable: category, value: 3
) )
author.rate article, 3, scope: category author.rate article, 3, scope: category
@ -31,7 +31,7 @@ RSpec.describe Rating::Extension, ':rate' do
context 'with no metadata' do context 'with no metadata' do
it 'delegates an empty hash to rate object' do it 'delegates an empty hash to rate object' do
expect(Rating::Rate).to receive(:create).with( expect(Rating::Rate).to receive(:create).with(
author: author, resource: article, metadata: {}, scopeable: nil, value: 3 author: author, extra_scopes: {}, resource: article, metadata: {}, scopeable: nil, value: 3
) )
author.rate article, 3 author.rate article, 3
@ -39,14 +39,22 @@ RSpec.describe Rating::Extension, ':rate' do
end end
context 'with metadata' do context 'with metadata' do
let!(:category) { build :category }
it 'delegates to rate object' do it 'delegates to rate object' do
expect(Rating::Rate).to receive(:create).with( expect(Rating::Rate).to receive(:create).with(
author: author, metadata: { comment: 'comment' }, resource: article, scopeable: nil, value: 3 author: author, extra_scopes: {}, metadata: { comment: 'comment' }, resource: article, scopeable: nil, value: 3
) )
author.rate article, 3, metadata: { comment: 'comment' } author.rate article, 3, metadata: { comment: 'comment' }
end end
end end
context 'with extra_scopes' do
it 'delegates to rate object' do
expect(Rating::Rate).to receive(:create).with(
author: author, extra_scopes: { scope_1: 'scope_1' }, metadata: { comment: 'comment' }, resource: article, scopeable: nil, value: 3
)
author.rate article, 3, extra_scopes: { scope_1: 'scope_1' }, metadata: { comment: 'comment' }
end
end
end end

View File

@ -4,35 +4,43 @@ require 'rails_helper'
RSpec.describe Rating::Extension, ':rated?' do RSpec.describe Rating::Extension, ':rated?' do
let!(:author) { create :author } let!(:author) { create :author }
let!(:article) { create :article } let!(:resource) { create :article }
context 'with no scopeable' do context 'with no scopeable' do
context 'when has no rate for the given resource' do before { author.rate resource, 1 }
before { allow(author).to receive(:rate_for).with(article, scope: nil).and_return nil }
specify { expect(author.rated?(article)).to eq false } context 'when has no rate for the given resource' do
specify { expect(author.rated?(create(: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).and_return double } specify { expect(author.rated?(resource)).to eq true }
specify { expect(author.rated?(article)).to eq true }
end end
end end
context 'with scopeable' do context 'with scopeable' do
let!(:category) { build :category } let!(:category) { create :category }
before { author.rate resource, 1, scope: 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).and_return nil } specify { expect(author.rated?(resource, scope: create(: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).and_return double } specify { expect(author.rated?(resource, scope: category)).to eq true }
end
end
specify { expect(author.rated?(article, scope: category)).to eq true } context 'with extra scopes' do
before { author.rate resource, 1, extra_scopes: { scope_1: 'scope_1' } }
context 'when has no rate for the given resource with given extra scopes' do
specify { expect(author.rated?(resource, extra_scopes: { scope_1: 'missing' })).to eq false }
end end
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 }
end end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
end end

View File

@ -37,4 +37,12 @@ RSpec.describe Rating::Extension, ':rated' 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
context 'with extra scopes' do
let!(:extra_scopes_rate) { author_1.rate article_1, 1, extra_scopes: { scope_1: 'scope_1' } }
it 'returns records considering the extra scopes' do
expect(author_1.rated(extra_scopes: { scope_1: 'scope_1' })).to eq [extra_scopes_rate]
end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
end end

View File

@ -37,4 +37,12 @@ RSpec.describe Rating::Extension, ':rates' 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
context 'with extra scopes' do
let!(:extra_scopes_rate) { author_1.rate article_1, 1, extra_scopes: { scope_1: 'scope_1' } }
it 'returns records considering the extra scopes' do
expect(article_1.rates(extra_scopes: { scope_1: 'scope_1' })).to eq [extra_scopes_rate]
end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
end end

View File

@ -6,8 +6,6 @@ RSpec.describe Rating::Rate, ':create' do
let!(:author) { create :author } let!(:author) { create :author }
let!(:article) { create :article } let!(:article) { create :article }
before(:all) { AddExtraScopesOnRatingRatesTable.new.change }
context 'with no scopeable' do context 'with no scopeable' do
before { described_class.create author: author, extra_scopes: {}, metadata: {}, resource: article, value: 3 } before { described_class.create author: author, extra_scopes: {}, metadata: {}, resource: article, value: 3 }
@ -360,5 +358,5 @@ RSpec.describe Rating::Rate, ':create' do
expect(rating.total).to eq 2 expect(rating.total).to eq 2
end end
end end
end end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
end end

View File

@ -6,8 +6,6 @@ RSpec.describe Rating::Rate, ':rate_for' do
let!(:author) { create :author } let!(:author) { create :author }
let!(:article) { create :article } let!(:article) { create :article }
before(:all) { AddExtraScopesOnRatingRatesTable.new.change }
context 'with no scopeable' do context 'with no scopeable' do
context 'when rate does not exist' do context 'when rate does not exist' do
it { expect(described_class.rate_for(author: author, resource: article)).to eq nil } it { expect(described_class.rate_for(author: author, resource: article)).to eq nil }
@ -103,5 +101,5 @@ RSpec.describe Rating::Rate, ':rate_for' do
expect(result).to eq nil expect(result).to eq nil
end end
end end
end end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
end end

View File

@ -20,8 +20,9 @@ RSpec.describe Rating::Rate do
end end
it do it do
expect(object).to validate_uniqueness_of(:author_id) scopes = %i[author_type resource_id resource_type scopeable_id scopeable_type]
.scoped_to(%i[author_type resource_id resource_type scopeable_id scopeable_type]) scopes += %i[scope_1 scope_2] if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
.case_insensitive
expect(object).to validate_uniqueness_of(:author_id).scoped_to(scopes).case_insensitive
end end
end end

View File

@ -14,3 +14,4 @@ CreateRatingTable.new.change
CreateReviewRatingsTable.new.change CreateReviewRatingsTable.new.change
CreateReviewsTable.new.change CreateReviewsTable.new.change
AddCommentOnRatingRatesTable.new.change AddCommentOnRatingRatesTable.new.change
AddExtraScopesOnRatingRatesTable.new.change