Class: Spyro::ActionViewExtension::CollectionForHelper::Output::BarGraphTable

Inherits:
Base
  • Object
show all
Defined in:
lib/spyro/collections/outputs/bar_graph_table.rb

Instance Method Summary collapse

Methods inherited from Base

#t

Constructor Details

#initialize(unicollection, helper, parser_class) ⇒ BarGraphTable

Returns a new instance of BarGraphTable.



10
11
12
# File 'lib/spyro/collections/outputs/bar_graph_table.rb', line 10

def initialize unicollection, helper, parser_class
  super unicollection, helper, parser_class
end

Instance Method Details

#build_popover(title, popovers) ⇒ Object



21
22
23
# File 'lib/spyro/collections/outputs/bar_graph_table.rb', line 21

def build_popover title, popovers
  "class='has-popover' data-title='#{title}' data-html='true' data-content='#{popovers.map {|elem| elem.join(': ')}.join('<br/>')}'"
end

#format_prct(value) ⇒ Object



14
15
16
17
18
19
# File 'lib/spyro/collections/outputs/bar_graph_table.rb', line 14

def format_prct value
  @min ||=  @unicollection.meta[:min] || 0
  @max ||= @unicollection.meta[:max] || 100

  [[(value - @min) * 100 / (@max - @min), 0].max, 100].min
end

#renderObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spyro/collections/outputs/bar_graph_table.rb', line 25

def render
  html = @unicollection.rows.map do |row|
    html_row = row[:'data'].map do |elem|
      @unicollection.meta[:'data-primary'].include?(elem.name.to_sym) ? elem.value : nil
    end.compact

    popovers = row[:'data'].map do |elem|
      @unicollection.meta[:'data-secondary'].include?(elem.name.to_sym) ? elem.value : nil
    end.compact

    title = html_row.first
    value = html_row.last
    span = "<span style='position: absolute; left: 10px; color: black'>#{html_row[0..-2].join ' - '}</span><span style='position: absolute; right: 10px; color: black'>#{value}</span>"
    "<li #{build_popover(title, @unicollection.meta[:'data-secondary'].zip(popovers))}><div class='progress progress-info' style='position: relative'><div class='progress-bar' style='width: #{self.format_prct(value)}%'>#{span}</div></div></li>".html_safe
  end.join('')
  "<ul class='list-unstyled'>#{html}</ul>".html_safe
end