up: add uniqueness on voting based on resource id
parent
de3ac30c24
commit
46530d5999
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue