Module: DynamicReports::Templates

Included in:
View
Defined in:
lib/dynamic_reports/templates.rb

Overview

Options are:

:layout       If set to false, no layout is rendered, otherwise
              the specified layout is used
:locals       A hash with local variables that should be available
              in the template

Instance Method Summary collapse

Instance Method Details

#builder(options = {}, locals = {}, &block) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/dynamic_reports/templates.rb', line 25

def builder(options={}, locals={}, &block)
  require_warn("Builder") unless defined?(::Builder)
  template = options.delete(:template)
  options, template = template, nil if template.is_a?(Hash)
  template = lambda { block } if template.nil?
  render :builder, template, options, locals
end

#chart_url(chart, report) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dynamic_reports/templates.rb', line 81

def chart_url(chart,report)
  columns = chart.columns ? chart.columns : report.columns
  chart_type = chart.type.nil? ?  :line : chart.type.to_sym
  case chart_type
  when :line
    Charts.line_chart(chart,columns,report)
  when :pie
    Charts.pie_chart(chart,columns,report)
  when :bar
    Charts.bar_column_chart(chart,columns,report,:vertical)
  when :column
    Charts.bar_column_chart(chart,columns,report,:horizontal)
  else
    raise StandardError => "Unknown chart type '#{chart.type}'."
  end

end

#commify(object) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/dynamic_reports/templates.rb', line 38

def commify(object)
  if object.is_a?(Numeric)
    object.to_s.gsub(/(\d)(?=(\d{3})+$)/,'\1,')
  else
    object
  end
end

#csv(options = {}, locals = {}) ⇒ Object



8
9
10
11
# File 'lib/dynamic_reports/templates.rb', line 8

def csv(options={}, locals={})
  template = options.delete(:template)
  render :csv, template, options.merge!(:content_type => "csv"), locals
end

#erb(options = {}, locals = {}) ⇒ Object



13
14
15
16
17
# File 'lib/dynamic_reports/templates.rb', line 13

def erb(options={}, locals={})
  require "erb"
  template = options.delete(:template)
  render :erb, template, options, locals
end

#haml(options = {}, locals = {}) ⇒ Object



19
20
21
22
23
# File 'lib/dynamic_reports/templates.rb', line 19

def haml(options={}, locals={})
  require_warn("Haml") unless defined?(::Haml::Engine)
  template = options.delete(:template)
  render :haml, template, options, locals
end

#linkcheck(record, column_object) ⇒ Object



46
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
# File 'lib/dynamic_reports/templates.rb', line 46

def linkcheck(record, column_object)
  val = ''

  if column_object.is_a?(Hash)
    if column_object.keys.include?(:column)
      column = column_object[:column] 
    else
      column = column_object.keys.first # => Josh shortcut :)
    end
  else
    column = column_object
  end

  report.links.each do |link|
    if link[:column] == column
      url = link[:url]
      url.scan(/\{(\w+)\}/).each do |parameter|
        parameter = parameter.to_s
        url = url.gsub("{#{parameter}}", CGI::escape(get_record_value(record, parameter.to_sym).to_s))
      end

      url_attribute_str = []
      link[:link_options].keys.each do |key|
        url_attribute_str << "#{key}='#{link[:link_options][key]}'"
      end if link[:link_options]
      
      val = "<a href='#{url}' #{url_attribute_str.join(' ')}>#{get_record_value(record,column)}</a>"
      break
    end if link[:column]
  end if report.links

  val.blank? ? get_record_value(record,column) : val
end

#titleize(object) ⇒ Object

TODO: Add Report Helpers for injection



34
35
36
# File 'lib/dynamic_reports/templates.rb', line 34

def titleize(object)
  object.to_s.split('_').each{ |word| word.capitalize! }.join(' ')
end