Class: Decidim::Meetings::AnswerForm

Inherits:
Form show all
Includes:
TranslationsHelper
Defined in:
decidim-meetings/app/forms/decidim/meetings/answer_form.rb

Overview

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

Constant Summary

Constants included from AttributeObject::TypeMap

AttributeObject::TypeMap::Boolean, AttributeObject::TypeMap::Decimal

Instance Attribute Summary collapse

Attributes inherited from AttributeObject::Form

#context

Instance Method Summary collapse

Methods included from TranslationsHelper

empty_translatable, ensure_translatable, multi_translation, translated_in_current_locale?

Methods included from TranslatableAttributes

#default_locale?

Methods inherited from AttributeObject::Form

ensure_hash, from_model, from_params, hash_from, infer_model_name, mimic, mimicked_model_name, model_name, #persisted?, #to_key, #to_model, #to_param, #valid?, #with_context

Methods included from AttributeObject::Model

#[], #[]=, #attributes, #attributes_with_values, #initialize, #to_h

Instance Attribute Details

#questionObject



19
20
21
# File 'decidim-meetings/app/forms/decidim/meetings/answer_form.rb', line 19

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

Instance Method Details

#answerObject



23
24
25
# File 'decidim-meetings/app/forms/decidim/meetings/answer_form.rb', line 23

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

#label(idx) ⇒ Object



27
28
29
30
31
# File 'decidim-meetings/app/forms/decidim/meetings/answer_form.rb', line 27

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

#map_model(model) ⇒ Object

Public: Map the correct fields.

Returns nothing.



36
37
38
39
40
41
42
43
# File 'decidim-meetings/app/forms/decidim/meetings/answer_form.rb', line 36

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

#selected_choicesObject



45
46
47
# File 'decidim-meetings/app/forms/decidim/meetings/answer_form.rb', line 45

def selected_choices
  choices.select(&:body)
end