Class: Datagrid::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/datagrid/renderer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Renderer



10
11
12
# File 'lib/datagrid/renderer.rb', line 10

def initialize(template)
  @template = template
end

Class Method Details

.for(template) ⇒ Object



6
7
8
# File 'lib/datagrid/renderer.rb', line 6

def self.for(template)
  new(template)
end

Instance Method Details

#_safe(string) ⇒ Object



92
93
94
# File 'lib/datagrid/renderer.rb', line 92

def _safe(string)
  string.respond_to?(:html_safe) ? string.html_safe : string
end

#form_for(grid, options = {}) ⇒ Object



49
50
51
52
53
54
# File 'lib/datagrid/renderer.rb', line 49

def form_for(grid, options = {})
  options[:method] ||= :get
  options[:html] ||= {}
  options[:html][:class] ||= "datagrid-form #{html_class(grid)}"
  @template.render :partial => "datagrid/form", :locals => {:grid => grid, :options => options}
end

#format_value(grid, column, asset) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/datagrid/renderer.rb', line 14

def format_value(grid, column, asset)
  if column.is_a?(String) || column.is_a?(Symbol)
    column = grid.column_by_name(column)
  end

  value = if column.html?
    args = []
    remaining_arity = column.html_block.arity

    if column.data?
      args << column.value(asset,grid)
      remaining_arity -= 1
    end

    args << asset if remaining_arity > 0
    args << grid if remaining_arity > 1

    @template.instance_exec(*args, &column.html_block)
  else
    column.value(asset,grid)
  end

  url = column.options[:url] && column.options[:url].call(asset)
  if url
    @template.link_to(value, url)
  else
    case column.format
    when :url
      @template.link_to(column.label  ? asset.send(column.label) : I18n.t("datagrid.table.url_label", :default => "URL"), value)
    else
      _safe(value)
    end
  end
end

#header(grid, options = {}) ⇒ Object



70
71
72
73
74
# File 'lib/datagrid/renderer.rb', line 70

def header(grid, options = {})
  options[:order] = true unless options.has_key?(:order)

  @template.render :partial => "datagrid/head", :locals => {:grid => grid, :options => options}
end

#html_class(grid) ⇒ Object



88
89
90
# File 'lib/datagrid/renderer.rb', line 88

def html_class(grid)
  grid.class.to_s.underscore.demodulize
end

#order_for(grid, column) ⇒ Object



84
85
86
# File 'lib/datagrid/renderer.rb', line 84

def order_for(grid, column)
  @template.render :partial => "datagrid/order_for", :locals => { :grid => grid, :column => column }
end

#rows(grid, assets, options = {}) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/datagrid/renderer.rb', line 76

def rows(grid, assets, options = {})
  result = assets.map do |asset|
    @template.render :partial => "datagrid/row", :locals => {:grid => grid, :options => options, :asset => asset}
  end.join

  _safe(result)
end

#table(grid, *args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/datagrid/renderer.rb', line 56

def table(grid, *args)
  options = args.extract_options!
  options[:html] ||= {}
  options[:html][:class] ||= "datagrid #{html_class(grid)}"
  assets = args.any? ? args.shift : grid.assets
  paginate = options[:paginate]
  if paginate
    ::Datagrid::Utils.warn_once(":paginate option is deprecated. Looks to https://github.com/bogdan/datagrid/wiki/Frontend.")
    assets = assets.paginate(paginate)
  end

  @template.render :partial => "datagrid/table", :locals => {:grid => grid, :options => options, :assets => assets}
end