Class: Decidim::Forms::QuestionnaireForm

Inherits:
Decidim::Form
  • Object
show all
Defined in:
app/forms/decidim/forms/questionnaire_form.rb

Overview

This class holds a Form to answer a questionnaire from Decidim’s public page.

Instance Method Summary collapse

Instance Method Details

#map_model(model) ⇒ Object

Private: Create the responses from the questionnaire questions

Returns nothing.



20
21
22
23
24
# File 'app/forms/decidim/forms/questionnaire_form.rb', line 20

def map_model(model)
  self.responses = model.questions.map do |question|
    AnswerForm.from_model(Decidim::Forms::Answer.new(question: question))
  end
end

#responses_by_stepObject

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.



30
31
32
33
34
35
36
37
38
39
40
# File 'app/forms/decidim/forms/questionnaire_form.rb', line 30

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_contextObject



46
47
48
49
50
# File 'app/forms/decidim/forms/questionnaire_form.rb', line 46

def session_token_in_context
  return if context&.session_token

  errors.add(:tos_agreement, I18n.t("activemodel.errors.models.questionnaire.request_invalid"))
end

#total_stepsObject



42
43
44
# File 'app/forms/decidim/forms/questionnaire_form.rb', line 42

def total_steps
  responses_by_step.count
end