docbrown/app/models/poll_skip.rb
Jeremy Friesen 2a26fa3538
Refactoring poll/vote/skip logic (#15441)
* Refactoring poll/vote/skip logic

Favor asking the poll if the user has already voted instead of relying
on the poll vote or skip to know about the implementation
details (e.g. should a poll vote know that there's a related model call
poll skipped?  likely not)

This refactor also tidies up the specs, for which there's quite a bit of
chatter.

Found via the `flay` gem.

* Adding method documentation

Also need to bump gitpod's build as it halted for the prior commit.
2021-11-29 10:52:32 -05:00

22 lines
718 B
Ruby

# @note When we destroy the related user, it's using dependent:
# :delete for the relationship. That means no before/after
# destroy callbacks will be called on this object.
#
# @note When we destroy the related poll, it's using dependent:
# :delete for the relationship. That means no before/after
# destroy callbacks will be called on this object.
class PollSkip < ApplicationRecord
belongs_to :poll
belongs_to :user
validate :one_vote_per_poll_per_user
private
def one_vote_per_poll_per_user
return false unless poll
return false unless poll.vote_previously_recorded_for?(user_id: user_id)
errors.add(:base, "cannot vote more than once in one poll")
end
end