Class: Decidim::Forms::AnswerForm

Inherits:
Decidim::Form show all
Includes:
AttachmentAttributes, TranslationsHelper
Defined in:
decidim-forms/app/forms/decidim/forms/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


30
31
32
# File 'decidim-forms/app/forms/decidim/forms/answer_form.rb', line 30

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

Instance Method Details

#custom_choicesObject


57
58
59
# File 'decidim-forms/app/forms/decidim/forms/answer_form.rb', line 57

def custom_choices
  choices.select(&:custom_body)
end

#display_conditions_fulfilled?Boolean

Returns:


61
62
63
64
65
66
# File 'decidim-forms/app/forms/decidim/forms/answer_form.rb', line 61

def display_conditions_fulfilled?
  question.display_conditions.all? do |condition|
    answer = context.responses&.find { |r| r.question_id&.to_i == condition.condition_question.id }
    condition.fulfilled?(answer)
  end
end

#has_attachments?Boolean

Returns:


68
69
70
# File 'decidim-forms/app/forms/decidim/forms/answer_form.rb', line 68

def has_attachments?
  question.has_attachments? && errors[:add_documents].empty? && add_documents.present?
end

#has_error_in_attachments?Boolean

Returns:


72
73
74
# File 'decidim-forms/app/forms/decidim/forms/answer_form.rb', line 72

def has_error_in_attachments?
  errors[:add_documents].present?
end

#label(idx) ⇒ Object


34
35
36
37
38
39
# File 'decidim-forms/app/forms/decidim/forms/answer_form.rb', line 34

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

#map_model(model) ⇒ Object

Public: Map the correct fields.

Returns nothing.


44
45
46
47
48
49
50
51
# File 'decidim-forms/app/forms/decidim/forms/answer_form.rb', line 44

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


53
54
55
# File 'decidim-forms/app/forms/decidim/forms/answer_form.rb', line 53

def selected_choices
  choices.select(&:body)
end