Class: Decidim::Forms::QuestionnaireUserAnswers

Inherits:
Query
  • Object
show all
Defined in:
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 '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 '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
27
28
29
# File 'app/queries/decidim/forms/questionnaire_user_answers.rb', line 22

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

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