class Decidim::Meetings::AnswerForm

This class holds a Form to save the questionnaire answers from Decidim’s public page

def answer

def answer
  @answer ||= Decidim::Meetings::Answer.find_by(decidim_user_id: current_user.id, decidim_question_id: question_id) if current_user
end

def label(idx)

def label(idx)
  base = "#{idx + 1}. #{translated_attribute(question.body)}"
  base += " (#{max_choices_label})" if question.max_choices
  base
end

def mandatory_label

def mandatory_label
  "*"
end

def map_model(model)

Returns nothing.

Public: Map the correct fields.
def map_model(model)
  self.question_id = model.decidim_question_id
  self.question = model.question
  self.choices = model.choices.map do |choice|
    AnswerChoiceForm.from_model(choice)
  end
end

def max_choices

def max_choices
  errors.add(:choices, :too_many) if selected_choices.size > question.max_choices
end

def max_choices_label

def max_choices_label
  I18n.t("questionnaires.question.max_choices", scope: "decidim.forms", n: question.max_choices)
end

def question

def question
  @question ||= Decidim::Meetings::Question.find(question_id)
end

def selected_choices

def selected_choices
  choices.select(&:body)
end