Class: Decidim::Forms::QuestionnaireForm
- Inherits:
-
Decidim::Form
- Object
- AttributeObject::Form
- Decidim::Form
- Decidim::Forms::QuestionnaireForm
- Includes:
- ActiveModel::Validations::Callbacks
- Defined in:
- decidim-forms/app/forms/decidim/forms/questionnaire_form.rb
Overview
This class holds a Form to answer a questionnaire from Decidim’s public page.
Constant Summary
Constants included from AttributeObject::TypeMap
AttributeObject::TypeMap::Boolean, AttributeObject::TypeMap::Decimal
Instance Attribute Summary
Attributes inherited from AttributeObject::Form
Instance Method Summary collapse
-
#before_validation ⇒ Object
Add other responses to the context so AnswerForm can validate conditional questions.
-
#map_model(model) ⇒ Object
Private: Create the responses from the questionnaire questions.
-
#responses_by_step ⇒ Object
Public: Splits reponses by step, keeping the separator.
- #session_token_in_context ⇒ Object
- #total_steps ⇒ Object
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 Method Details
#before_validation ⇒ Object
Add other responses to the context so AnswerForm can validate conditional questions
32 33 34 |
# File 'decidim-forms/app/forms/decidim/forms/questionnaire_form.rb', line 32 def before_validation context.responses = attributes[:responses] end |
#map_model(model) ⇒ Object
Private: Create the responses from the questionnaire questions
Returns nothing.
25 26 27 28 29 |
# File 'decidim-forms/app/forms/decidim/forms/questionnaire_form.rb', line 25 def map_model(model) self.responses = model.questions.map do |question| AnswerForm.from_model(Decidim::Forms::Answer.new(question:)) end end |
#responses_by_step ⇒ Object
Public: Splits reponses by step, keeping the separator.
Returns an array of steps. Each step is a list of the questions in that step, including the separator.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'decidim-forms/app/forms/decidim/forms/questionnaire_form.rb', line 40 def responses_by_step @responses_by_step ||= begin steps = responses.chunk_while do |a, b| !a.question.separator? || b.question.separator? end.to_a steps = [[]] if steps == [] steps end end |
#session_token_in_context ⇒ Object
56 57 58 59 60 |
# File 'decidim-forms/app/forms/decidim/forms/questionnaire_form.rb', line 56 def session_token_in_context return if context&.session_token errors.add(:tos_agreement, I18n.t("activemodel.errors.models.questionnaire.request_invalid")) end |
#total_steps ⇒ Object
52 53 54 |
# File 'decidim-forms/app/forms/decidim/forms/questionnaire_form.rb', line 52 def total_steps responses_by_step.count end |