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



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/helpers/meta_reports/reports_helper.rb', line 3

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



17
18
19
20
21
22
23
24
25
26
# File 'app/helpers/meta_reports/reports_helper.rb', line 17

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

#prep_pdf_table(table, styling) ⇒ Object



28
29
30
31
32
33
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
63
# File 'app/helpers/meta_reports/reports_helper.rb', line 28

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 = MetaReports::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] = data[i][j][:content].to_s unless data[i][j][:content].is_a? String
        end
      end
    end
  end
  data
end

#prep_xlsx_table(table, styling) ⇒ Object



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 65

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


112
113
114
115
116
117
118
119
120
121
# File 'app/helpers/meta_reports/reports_helper.rb', line 112

def report_alt_links(report, params)
  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


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

def report_link(report, title = report.title)
  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

#set_style(styling, style, row, col) ⇒ Object



123
124
125
126
# File 'app/helpers/meta_reports/reports_helper.rb', line 123

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

#to_xls_col(column) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'app/helpers/meta_reports/reports_helper.rb', line 128

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