Class: Decidim::Forms::QuestionnaireForm
- Inherits:
-
Decidim::Form
- Object
- Decidim::Form
- Decidim::Forms::QuestionnaireForm
- Includes:
- ActiveModel::Validations::Callbacks
- Defined in:
- app/forms/decidim/forms/questionnaire_form.rb
Overview
This class holds a Form to response a questionnaire from Decidim’s public page.
Instance Method Summary collapse
- #add_responses!(questionnaire:, session_token:, ip_hash:) ⇒ Object
-
#before_validation ⇒ Object
Add other responses to the context so ResponseForm can validate conditional questions.
-
#map_model(model) ⇒ Object
Private: Create the responses from the questionnaire questions.
-
#responses_by_step ⇒ Object
Public: Splits responses by step, keeping the separator.
- #session_token_in_context ⇒ Object
- #total_steps ⇒ Object
Instance Method Details
#add_responses!(questionnaire:, session_token:, ip_hash:) ⇒ Object
31 32 33 34 35 |
# File 'app/forms/decidim/forms/questionnaire_form.rb', line 31 def add_responses!(questionnaire:, session_token:, ip_hash:) self.responses = questionnaire.questions.map do |question| ResponseForm.from_model(Decidim::Forms::Response.where(question:, user: current_user, session_token:, ip_hash:).first_or_initialize) end end |
#before_validation ⇒ Object
Add other responses to the context so ResponseForm can validate conditional questions
38 39 40 |
# File 'app/forms/decidim/forms/questionnaire_form.rb', line 38 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 'app/forms/decidim/forms/questionnaire_form.rb', line 25 def map_model(model) self.responses = model.questions.map do |question| ResponseForm.from_model(Decidim::Forms::Response.new(question:)) end end |
#responses_by_step ⇒ Object
Public: Splits responses by step, keeping the separator.
Returns an array of steps. Splits responses at each separator. Allowing only steps with questions to be counted, excluding the separators.
46 47 48 49 50 51 52 53 |
# File 'app/forms/decidim/forms/questionnaire_form.rb', line 46 def responses_by_step @responses_by_step ||= begin steps = responses.slice_before { |response| response.question.separator? }.map do |group| group.reject { |response| response.question.separator? } end steps.reject(&:empty?) end end |
#session_token_in_context ⇒ Object
59 60 61 62 63 |
# File 'app/forms/decidim/forms/questionnaire_form.rb', line 59 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
55 56 57 |
# File 'app/forms/decidim/forms/questionnaire_form.rb', line 55 def total_steps responses_by_step.count end |