Class: Decidim::Forms::AnswerForm
- Inherits:
-
Decidim::Form
- Object
- Decidim::Form
- Decidim::Forms::AnswerForm
- Includes:
- AttachmentAttributes, TranslationsHelper
- Defined in:
- app/forms/decidim/forms/answer_form.rb
Overview
This class holds a Form to save the questionnaire answers from Decidim’s public page
Instance Attribute Summary collapse
Instance Method Summary collapse
- #custom_choices ⇒ Object
- #display_conditions_fulfilled? ⇒ Boolean
- #has_attachments? ⇒ Boolean
- #has_error_in_attachments? ⇒ Boolean
- #label ⇒ Object
- #mandatory_conditions_fulfilled? ⇒ Boolean
-
#map_model(model) ⇒ Object
Public: Map the correct fields.
- #optional_conditions_fulfilled? ⇒ Boolean
- #selected_choices ⇒ Object
- #sorting? ⇒ Boolean
Instance Attribute Details
Instance Method Details
#custom_choices ⇒ Object
57 58 59 |
# File 'app/forms/decidim/forms/answer_form.rb', line 57 def custom_choices choices.select(&:custom_body) end |
#display_conditions_fulfilled? ⇒ Boolean
61 62 63 64 65 |
# File 'app/forms/decidim/forms/answer_form.rb', line 61 def display_conditions_fulfilled? return optional_conditions_fulfilled? unless question.display_conditions.where(mandatory: true).any? mandatory_conditions_fulfilled? end |
#has_attachments? ⇒ Boolean
83 84 85 |
# File 'app/forms/decidim/forms/answer_form.rb', line 83 def question. && errors[:add_documents].empty? && add_documents.present? end |
#has_error_in_attachments? ⇒ Boolean
87 88 89 |
# File 'app/forms/decidim/forms/answer_form.rb', line 87 def errors[:add_documents].present? end |
#label ⇒ Object
34 35 36 37 38 39 |
# File 'app/forms/decidim/forms/answer_form.rb', line 34 def label base = translated_attribute(question.body) base += " #{mandatory_label}" if question.mandatory? base += " (#{max_choices_label})" if question.max_choices base end |
#mandatory_conditions_fulfilled? ⇒ Boolean
67 68 69 70 71 72 |
# File 'app/forms/decidim/forms/answer_form.rb', line 67 def mandatory_conditions_fulfilled? question.display_conditions.where(mandatory: true).all? do |condition| answer = context.responses&.find { |r| r.question_id&.to_i == condition.condition_question.id } condition.fulfilled?(answer) end end |
#map_model(model) ⇒ Object
Public: Map the correct fields.
Returns nothing.
44 45 46 47 48 49 50 51 |
# File '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 |
#optional_conditions_fulfilled? ⇒ Boolean
74 75 76 77 78 79 80 81 |
# File 'app/forms/decidim/forms/answer_form.rb', line 74 def optional_conditions_fulfilled? return true unless question.display_conditions.where(mandatory: false).any? question.display_conditions.where(mandatory: false).any? do |condition| answer = context.responses&.find { |r| r.question_id&.to_i == condition.condition_question.id } condition.fulfilled?(answer) end end |
#selected_choices ⇒ Object
53 54 55 |
# File 'app/forms/decidim/forms/answer_form.rb', line 53 def selected_choices choices.select(&:body) end |
#sorting? ⇒ Boolean
91 92 93 |
# File 'app/forms/decidim/forms/answer_form.rb', line 91 def sorting? question.question_type == "sorting" end |