Class: Decidim::Forms::QuestionnaireParticipants

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

Overview

A class used to collect user answers for a questionnaire

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(questionnaire) ⇒ QuestionnaireParticipants

Initializes the class.

questionnaire = a Questionnaire object


17
18
19
# File 'decidim-forms/app/queries/decidim/forms/questionnaire_participants.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_participants.rb', line 10

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

Instance Method Details

#count_participantsObject


38
39
40
# File 'decidim-forms/app/queries/decidim/forms/questionnaire_participants.rb', line 38

def count_participants
  query.select(:session_token).distinct.count
end

#participant(session_token) ⇒ Object


29
30
31
# File 'decidim-forms/app/queries/decidim/forms/questionnaire_participants.rb', line 29

def participant(session_token)
  query.find_by(session_token: session_token)
end

#participantsObject


33
34
35
36
# File 'decidim-forms/app/queries/decidim/forms/questionnaire_participants.rb', line 33

def participants
  subquery = query.select("DISTINCT ON (decidim_forms_answers.session_token) decidim_forms_answers.*")
  Answer.select("*").from(subquery).order(:created_at)
end

#queryObject

Finds all Answers for the questionnaire (unique session_tokens). Because exporters only have access to the Answers this is used as an indirect way to access the participants (see #participants and #participant)


25
26
27
# File 'decidim-forms/app/queries/decidim/forms/questionnaire_participants.rb', line 25

def query
  Answer.where(questionnaire: @questionnaire)
end