Remove double presence validator in PollVote (#3187) [ci skip]
This commit is contained in:
parent
13bfdf2b64
commit
f9fb76ec97
2 changed files with 11 additions and 5 deletions
|
|
@ -6,21 +6,23 @@ class PollVote < ApplicationRecord
|
|||
counter_culture :poll_option
|
||||
counter_culture :poll
|
||||
|
||||
validates :poll_id, presence: true, presence: true, uniqueness: { scope: :user_id } # In the future we'll remove this constraint if/when we allow multi-answer polls
|
||||
validates :poll_id, presence: true, uniqueness: { scope: :user_id } # In the future we'll remove this constraint if/when we allow multi-answer polls
|
||||
validates :poll_option_id, presence: true, uniqueness: { scope: :user_id }
|
||||
validate :one_vote_per_poll_per_user
|
||||
|
||||
after_save :touch_poll_votes_count
|
||||
after_destroy :touch_poll_votes_count
|
||||
|
||||
def poll
|
||||
poll_option.poll
|
||||
end
|
||||
delegate :poll, to: :poll_option, allow_nil: true
|
||||
|
||||
private
|
||||
|
||||
def one_vote_per_poll_per_user
|
||||
errors.add(:base, "cannot vote more than once in one poll") if poll.poll_votes.where(user_id: user_id).any? || poll.poll_skips.where(user_id: user_id).any?
|
||||
return false unless poll
|
||||
|
||||
has_votes = (
|
||||
poll.poll_votes.where(user_id: user_id).any? || poll.poll_skips.where(user_id: user_id).any?)
|
||||
errors.add(:base, "cannot vote more than once in one poll") if has_votes
|
||||
end
|
||||
|
||||
def touch_poll_votes_count
|
||||
|
|
|
|||
|
|
@ -5,6 +5,10 @@ RSpec.describe PollVote, type: :model do
|
|||
let(:user) { create(:user) }
|
||||
let(:poll) { create(:poll, article_id: article.id) }
|
||||
|
||||
it "is not valid as a new object" do
|
||||
expect(PollVote.new.valid?).to be(false)
|
||||
end
|
||||
|
||||
it "limits one vote per user per poll" do
|
||||
create(:poll_vote, poll_option_id: poll.poll_options.last.id, user_id: user.id, poll_id: poll.id)
|
||||
PollVote.create(poll_option_id: poll.poll_options.first.id, user_id: user.id, poll_id: poll.id)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue