Module: Decidim::Demographics::PublishResponsesHelper

Defined in:
app/helpers/decidim/demographics/publish_responses_helper.rb

Instance Method Summary collapse

Instance Method Details

#chart_for_question(question_id) ⇒ Object

Renders the chart for the given question. Uses chartkick to render the chart.

Parameters:

  • question_id (Integer)

    the question id for Decidim:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/helpers/decidim/demographics/publish_responses_helper.rb', line 16

def chart_for_question(question_id)
  question = Decidim::Forms::Question.includes(responses: { choices: [:response_option, :matrix_row] }).find(question_id)

  Chartkick.options = {
    library: { animation: { easing: "easeOutQuart" } },
    colors: colors_list
  }

  case question.question_type
  when "single_option", "multiple_option"
    options_column_chart_wrapper(question)
  when "matrix_single", "matrix_multiple"
    matrix_stack_chart_wrapper(question)
  when "sorting"
    sorting_stack_chart_wrapper(question)
  else
    "Unknown question type"
  end
end

#colors_listObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/helpers/decidim/demographics/publish_responses_helper.rb', line 42

def colors_list
  %w(
    #3366CC
    #DC3912
    #FF9900
    #109618
    #3B3EAC
    #0099C6
    #DD4477
    #66AA00
    #B82E2E
    #316395
  )
end

#options_column_chart_wrapper(question) ⇒ Object



36
37
38
39
40
# File 'app/helpers/decidim/demographics/publish_responses_helper.rb', line 36

def options_column_chart_wrapper(question)
  tally = question.responses.map { |response| response.choices.map { |choice| translated_attribute(choice.response_option.body) } }.tally

  column_chart(tally, download: true)
end

#question_response_is_publicable(question_type) ⇒ Object



6
7
8
9
10
# File 'app/helpers/decidim/demographics/publish_responses_helper.rb', line 6

def question_response_is_publicable(question_type)
  ignored_question_types = %w(short_response long_response separator files).freeze

  ignored_question_types.exclude?(question_type)
end