Class: Decidim::Forms::QuestionnaireUserAnswers

Inherits:
Rectify::Query
  • Object
show all
Defined in:
decidim-forms/app/queries/decidim/forms/questionnaire_user_answers.rb

Overview

A class used to collect user answers for a questionnaire

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(questionnaire) ⇒ QuestionnaireUserAnswers

Initializes the class.

questionnaire = a Questionnaire object


17
18
19
# File 'decidim-forms/app/queries/decidim/forms/questionnaire_user_answers.rb', line 17

def initialize(questionnaire)
  @questionnaire = questionnaire
end

Class Method Details

.for(questionnaire) ⇒ Object

Syntactic sugar to initialize the class and return the queried objects.

questionnaire - a Questionnaire object


10
11
12
# File 'decidim-forms/app/queries/decidim/forms/questionnaire_user_answers.rb', line 10

def self.for(questionnaire)
  new(questionnaire).query
end

Instance Method Details

#queryObject

Finds and group answers by user for each questionnaire's question.


22
23
24
25
26
# File 'decidim-forms/app/queries/decidim/forms/questionnaire_user_answers.rb', line 22

def query
  answers = Answer.not_separator.joins(:question).where(questionnaire: @questionnaire)

  answers.sort_by { |answer| answer.question.position }.group_by { |a| a.user || a.session_token }.values
end