Module: MetaReports::ReportsHelper

Defined in:
lib/meta_reports/helper.rb

Instance Method Summary collapse

Instance Method Details

#convert_margins_to_xlsx(margins) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/meta_reports/helper.rb', line 21

def convert_margins_to_xlsx(margins)
  margins = [*margins].compact.map {|val| val/72.0}
  page_margins = {}
  unless margins.blank?
    len = margins.length
    [:top, :right, :bottom, :left].each_with_index { |dir, i| page_margins[dir] = margins[i%len] }
    if len == 6
      page_margins[:header] = margins[5]
      page_margins[:footer] = margins[6]
    end
  end
  page_margins
end

#html_cell(cell, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/meta_reports/helper.rb', line 35

def html_cell(cell, options = {})
  tag = options[:tag] || :td
  options[:inline_css].nil? && options[:inline_css] = true
  if cell.is_a? Hash
    tags = cell.reject {|k,v| k == :content || k == :html}
    tags[:class] ||= options[:default_class]
    if options[:inline_css]
      color = nil
      tags[:class].split(/\s+/).each do |token|
        if color = meta_report_color(token)
          break
        end
      end
      tags[:style] = "background-color: ##{color}"
    end
     tag, (cell[:html] || cell[:content]).to_s.html_safe, tags
  elsif cell.is_a? Array
  else
    (tag, cell, :class => options[:default_class])
  end
end


152
153
154
155
156
157
158
159
160
161
162
# File 'lib/meta_reports/helper.rb', line 152

def meta_report_alt_links(report, params)
  return nil unless report
  links = []
  if report.format? :pdf
    links << link_to(image_tag('meta_reports/print.png'), params.merge({:format => :pdf}), :target => '_blank')
  end
  if report.format? :xlsx
    links << link_to(image_tag('meta_reports/spreadsheet.png'), params.merge({:format => :xlsx}), :target => '_blank')
  end
  links.join(' ').html_safe
end

#meta_report_color(klass, row = 0) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/meta_reports/helper.rb', line 3

def meta_report_color(klass, row = 0)
  @meta_reports_colors ||= {}
  @meta_reports_colors[klass.to_sym] ||= begin
    color = MetaReports::Color::COLORS[klass.to_sym]
    return nil unless color
    if color.is_a? Array
      # the trailing split first is to drop any !important directive
      color = color[row%color.length].to_s.split.first 
    end
    if color.gsub!(/^\$/, '') # we have a variable
      index = 0
      index = $1 if color.gsub!(/_(\d+)$/,'')
      color = meta_report_color(color, index)
    end
    color
  end
end


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/meta_reports/helper.rb', line 125

def meta_report_link(report, title = report.title)
  return nil unless report
  if report.direct
     :span do
      links = []
      if report.format? :html
        links << link_to(title, meta_reports.short_show_path(report), target: '_blank')
      elsif report.format? :pdf
        links << link_to(title, meta_reports.short_show_path(report, format: :pdf), target: '_blank')
      elsif report.format? :xlsx
        links << link_to(title, meta_reports.short_show_path(report, format: :xlsx), target: '_blank')
      end
      if report.format? :pdf
        links << link_to(image_tag('meta_reports/print.png'), meta_reports.short_show_path(report, format: :pdf), target: '_blank')
      end
      if report.format? :xlsx
        links << link_to(image_tag('meta_reports/spreadsheet.png'), meta_reports.short_show_path(report, format: :xlsx), target: '_blank')
      end
      links.join(' ').html_safe
    end
  else
     :span do
     link_to title, report
    end
  end
end

#prep_pdf_table(table, styling) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/meta_reports/helper.rb', line 57

def prep_pdf_table(table, styling)
  if table.is_a?(Array)
    opts = table.last.is_a?(Hash) && table.pop || {}
    data = table
  else
    data = table.to_a
    opts = table.options
  end
  styling[:column_widths] = opts[:column_widths] if opts[:column_widths]
  row_classes = opts[:row_classes] || {}
  row_colors = {}
  row_classes.each do |i, klass|
    if color = meta_report_color(klass, i)
      row_colors[i] = color
    end
  end
  styling[:row_colors] = row_colors
  data.each_index do |i|
    data[i].each_index do |j|
      if data[i][j].is_a? Hash
        [:html, :title, :id, :link].each {|sym| data[i][j].delete(sym)}
        _class = data[i][j].delete(:class)
        unless _class.blank?
          [:right, :left, :center].each do |sym|
            set_style(styling, sym, i, j) if _class =~ /\b#{sym}\b|[^a-z]#{sym}/i
          end
          if _class =~ /\bbold\b|\bstrong\b/
            set_style(styling, :bold, i, j)
          end
          _class.to_s.split(/\s+/).each do |token|
            if color = meta_report_color(token)
              set_style(styling, :cell_bg_colors, i, j, color)
              break
            end
          end
        end
        unless data[i][j][:image] || data[i][j][:content].is_a?(String)
          data[i][j][:content] = data[i][j][:content].to_s
        end
      elsif !data[i][j].is_a?(String)
        data[i][j] = data[i][j].to_s
      end
    end
  end
  data
end

#prep_xlsx_table(table, styling) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/meta_reports/helper.rb', line 104

def prep_xlsx_table(table, styling)
  data = table.to_a
  data.each_index do |i|
    data[i].each_index do |j|
      if data[i][j].nil?
        data[i][j] = ''
      elsif data[i][j].is_a? Hash
        _class = data[i][j][:class]
        [:right, :left, :center].each do |sym|
          set_style(styling, sym, i, j) if _class =~ /\b#{sym}\b|[^a-z]#{sym}/i
        end
        if _class =~ /\bbold\b|\bstrong\b/
          set_style(styling, :bold, i, j)
        end
        data[i][j] = data[i][j][:content].to_s
      end
    end
  end
  data
end

#set_style(styling, style, row, col, val = nil) ⇒ Object



164
165
166
167
# File 'lib/meta_reports/helper.rb', line 164

def set_style(styling, style, row, col, val = nil)
  styling[style] ||= []
  styling[style] << (val ? [row,col,val] : [row,col])
end

#to_xls_col(column) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/meta_reports/helper.rb', line 169

def to_xls_col(column)
  index = column.to_i.abs
  chars = []
  while index >= 26 do
    chars << ((index % 26) + 65).chr
    index = (index / 26).to_i - 1
  end
  chars << (index + 65).chr
  chars.reverse.join
end