Module: MetaReports::ReportsHelper

Defined in:
app/helpers/meta_reports/reports_helper.rb

Instance Method Summary collapse

Instance Method Details

#convert_margins_to_xlsx(margins) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/helpers/meta_reports/reports_helper.rb', line 22

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, tag = :td) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/meta_reports/reports_helper.rb', line 36

def html_cell(cell, tag = :td)
  if cell.is_a? Hash
    tags = cell.reject {|k,v| k == :content || k == :html}
    tags[:class] ||= 'textcenter'
     tag, (cell[:html] || cell[:content]).to_s.html_safe, tags
  elsif cell.is_a? Array
  else
     tag, cell, :class => 'textcenter'
  end
end


134
135
136
137
138
139
140
141
142
143
144
# File 'app/helpers/meta_reports/reports_helper.rb', line 134

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
20
# File 'app/helpers/meta_reports/reports_helper.rb', line 3

def meta_report_color(klass, row = 0)
  color = MetaReports::Report::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 
    # the trailing gsub is to drop any !important directive
    color = color[row%color.length].to_s.gsub(/\s.*$/,'')
  end
  if color.gsub!(/^\$/, '') # we have a variable
    color = COLORS[color.to_sym]
    if color.is_a? Array
      choice = color.gsub!(/Odd/,'') ? 1 : 0
      color = color[choice]
    end
  end
  color
end


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/helpers/meta_reports/reports_helper.rb', line 107

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, meta_reports.short_form_path(report)
    end
  end
end

#prep_pdf_table(table, styling) ⇒ Object



47
48
49
50
51
52
53
54
55
56
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
# File 'app/helpers/meta_reports/reports_helper.rb', line 47

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].each {|sym| data[i][j].delete(sym)}
        _class = data[i][j].delete(: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
        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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/helpers/meta_reports/reports_helper.rb', line 86

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) ⇒ Object



146
147
148
149
# File 'app/helpers/meta_reports/reports_helper.rb', line 146

def set_style(styling, style, row, col)
  styling[style] ||= []
  styling[style] << [row,col]
end

#to_xls_col(column) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'app/helpers/meta_reports/reports_helper.rb', line 151

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