Module: LedgerWeb::Helpers

Includes:
Rack::Utils
Defined in:
lib/ledger_web/helpers.rb

Instance Method Summary collapse

Instance Method Details

#default(key, value) ⇒ Object



40
41
42
43
44
45
# File 'lib/ledger_web/helpers.rb', line 40

def default(key, value)
  if not Report.params.has_key? key
    puts "Setting #{key} to #{value}"
    Report.params[key] = value
  end
end

#expect(expected) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ledger_web/helpers.rb', line 27

def expect(expected)
  not_present = []
  expected.each do |key|
    if not params.has_key? key
      not_present << key
    end
  end
  
  if not_present.length > 0
    raise "Missing params: #{not_present.join(', ')}"
  end
end

#linkify(links, row, value, display_value) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ledger_web/helpers.rb', line 47

def linkify(links, row, value, display_value)
  links.each do |key, val|
    if key.is_a? String
      key = /^#{key}$/
    end
  
    if key.match(value[1].title.to_s)
      url = String.new(links[key])
      row.each_with_index do |v,i|
        url.gsub!(":#{i}", CGI.escape(v[0].to_s))
      end
  
      url.gsub!(':title', CGI.escape(value[1].title.to_s))
      url.gsub!(':now', CGI.escape(DateTime.now.strftime('%Y-%m-%d')))
      display_value = "<a href='#{url}'>#{escape_html(display_value)}</a>"
    else
      display_value = escape_html(display_value)
    end

  end
  display_value
end

#partial(template, locals = {}) ⇒ Object



9
10
11
# File 'lib/ledger_web/helpers.rb', line 9

def partial (template, locals = {})
  erb(template, :layout => false, :locals => locals)
end

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



18
19
20
21
22
23
24
25
# File 'lib/ledger_web/helpers.rb', line 18

def query(options={}, &block)
  q = capture(&block)
  report = LedgerWeb::Report.from_query(q)
  if options[:pivot]
    report = report.pivot(options[:pivot], options[:pivot_sort_order])
  end
  return report
end

#table(report, options = {}) ⇒ Object



13
14
15
16
# File 'lib/ledger_web/helpers.rb', line 13

def table(report, options = {})
  links = options[:links] || {}
  partial(:table, :report => report, :links => links)
end

#visualization(report, options = {}, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ledger_web/helpers.rb', line 70

def visualization(report, options={}, &block)
  vis = capture(&block)
  @vis_count ||= 0
  @vis_count += 1
  @_out_buf.concat(
    partial(
      :visualization,
      :report => report,
      :visualization_code => vis, 
      :div_id => "vis_#{@vis_count}"
    )
  )
end