The `reload` method added to max_votes validation is needed because the author gets here with some changes because of the around_action `switch_locale`, which adds some changes to the current user record and therefore, the lock method raises an exception when trying to lock it requiring us to save or discard those record changes.
31 lines
682 B
Ruby
31 lines
682 B
Ruby
class Polls::Questions::AnswersComponent < ApplicationComponent
|
|
attr_reader :question
|
|
delegate :can?, :current_user, :user_signed_in?, to: :helpers
|
|
|
|
def initialize(question)
|
|
@question = question
|
|
end
|
|
|
|
def already_answered?(question_answer)
|
|
user_answer(question_answer).present?
|
|
end
|
|
|
|
def question_answers
|
|
question.question_answers
|
|
end
|
|
|
|
def user_answer(question_answer)
|
|
user_answers.find_by(answer: question_answer.title)
|
|
end
|
|
|
|
def disable_answer?(question_answer)
|
|
question.multiple? && user_answers.count == question.max_votes
|
|
end
|
|
|
|
private
|
|
|
|
def user_answers
|
|
@user_answers ||= question.answers.by_author(current_user)
|
|
end
|
|
end
|