Files
grecia/app/components/polls/questions/answers_component.rb
Javi Martín 1d955b7a20 Simplify using helper methods in components
The `use_helpers` method was added in ViewComponent 3.8.0, and it's
included by default in all components since version 3.11.0.

Note we sometimes delegated the `can?` method to the controller instead
of the helpers, for no particularly reason. We're unifying that code as
well.
2024-03-02 17:34:25 +01:00

31 lines
671 B
Ruby

class Polls::Questions::AnswersComponent < ApplicationComponent
attr_reader :question
use_helpers :can?, :current_user, :user_signed_in?
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