up: add uniqueness on voting based on resource id

main
Washington Botelho 2018-01-14 22:43:13 -02:00
parent de3ac30c24
commit 46530d5999
No known key found for this signature in database
GPG Key ID: BECE10A8106CC7A0
3 changed files with 12 additions and 1 deletions

View File

@ -10,6 +10,11 @@ module Rating
validates :average, :estimate, :resource, :sum, :total, presence: true validates :average, :estimate, :resource, :sum, :total, presence: true
validates :average, :estimate, :sum, :total, numericality: true validates :average, :estimate, :sum, :total, numericality: true
validates :resource_id, uniqueness: {
case_sensitive: false,
scope: %i[resource_type scopeable_id scopeable_type]
}
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)

View File

@ -7,6 +7,6 @@ FactoryBot.define do
sum 100 sum 100
total 1 total 1
resource { create :article } association :resource, factory: :article, strategy: :build
end end
end end

View File

@ -15,4 +15,10 @@ RSpec.describe Rating::Rating do
it { is_expected.to validate_presence_of :resource } it { is_expected.to validate_presence_of :resource }
it { is_expected.to validate_presence_of :sum } it { is_expected.to validate_presence_of :sum }
it { is_expected.to validate_presence_of :total } it { is_expected.to validate_presence_of :total }
it do
expect(object).to validate_uniqueness_of(:resource_id)
.scoped_to(%i[resource_type scopeable_id scopeable_type])
.case_insensitive
end
end end