vote_validator.rb 522 B

12345678910111213
  1. # frozen_string_literal: true
  2. class VoteValidator < ActiveModel::Validator
  3. def validate(vote)
  4. vote.errors.add(:base, I18n.t('polls.errors.expired')) if vote.poll.expired?
  5. if vote.poll.multiple? && vote.poll.votes.where(account: vote.account, choice: vote.choice).exists?
  6. vote.errors.add(:base, I18n.t('polls.errors.already_voted'))
  7. elsif !vote.poll.multiple? && vote.poll.votes.where(account: vote.account).exists?
  8. vote.errors.add(:base, I18n.t('polls.errors.already_voted'))
  9. end
  10. end
  11. end