Class: Spout::Models::Tables::ChoicesVsChoices

Inherits:
Default
  • Object
show all
Defined in:
lib/spout/models/tables/choices_vs_choices.rb

Overview

Generates a table of

Instance Attribute Summary

Attributes inherited from Default

#chart_variable, #subjects, #subtitle, #totals, #variable

Instance Method Summary collapse

Methods inherited from Default

#initialize, #to_hash, #valid?

Constructor Details

This class inherits a constructor from Spout::Models::Tables::Default

Instance Method Details

#footersObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/spout/models/tables/choices_vs_choices.rb', line 22

def footers
  total_values = filtered_domain_options(@chart_variable).collect do |option|
    total_count = @filtered_subjects.count { |s| s.send(@chart_variable.id) == option.value }
    { text: (Spout::Helpers::TableFormatting.format_number(total_count, :count)), style: "font-weight:bold" }
  end
  footer_row = [{ text: "Total", style: "font-weight:bold" }] + total_values
  if @totals
    footer_row += [{ text: Spout::Helpers::TableFormatting.format_number(@filtered_subjects.count, :count), style: "font-weight:bold" }]
  end
  [footer_row]
end

#headersObject



14
15
16
17
18
19
20
# File 'lib/spout/models/tables/choices_vs_choices.rb', line 14

def headers
  header_row = [""] + filtered_domain_options(@chart_variable).collect(&:display_name)
  if @totals
    header_row += ["Total"]
  end
  [header_row]
end

#rowsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/spout/models/tables/choices_vs_choices.rb', line 34

def rows
  rows_result = filtered_domain_options(@variable).collect do |option|
    row_subjects = @filtered_subjects.select { |s| s.send(@variable.id) == option.value }
    row_cells = filtered_domain_options(@chart_variable).collect do |chart_option|
      count = row_subjects.count { |s| s.send(@chart_variable.id) == chart_option.value }
      count > 0 ? Spout::Helpers::TableFormatting.format_number(count, :count) : { text: "-", class: "text-muted" }
    end

    row = [option.display_name] + row_cells

    if @totals
      total = row_subjects.count
      row += [total == 0 ? { text: "-", class: "text-muted" } : { text: Spout::Helpers::TableFormatting.format_number(total, :count), style: "font-weight:bold" }]
    end
    row
  end

  if @filtered_subjects.count { |s| s.send(@variable.id).is_a?(Spout::Models::Empty) } > 0
    unknown_values = filtered_domain_options(@chart_variable).collect do |chart_option|
      { text: Spout::Helpers::TableFormatting.format_number(@filtered_subjects.count { |s| s.send(@chart_variable.id) == chart_option.value && s.send(@variable.id).is_a?(Spout::Models::Empty) }, :count), class: "text-muted" }
    end
    unknown_row = [{ text: "Unknown", class: "text-muted" }] + unknown_values
    if @totals
      unknown_row += [{ text: Spout::Helpers::TableFormatting.format_number(@filtered_subjects.count { |s| s.send(@variable.id).is_a?(Spout::Models::Empty) }, :count), style: "font-weight:bold", class: "text-muted" }]
    end
    rows_result << unknown_row
  end
  rows_result
end

#titleObject



10
11
12
# File 'lib/spout/models/tables/choices_vs_choices.rb', line 10

def title
  "#{@variable.display_name} vs #{@chart_variable.display_name}"
end