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

Returns a new instance of 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



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

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

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



46
47
48
49
50
51
# File 'lib/datagrid/renderer.rb', line 46

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
# File 'lib/datagrid/renderer.rb', line 14

def format_value(grid, column, asset)

  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



67
68
69
70
71
# File 'lib/datagrid/renderer.rb', line 67

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



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

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

#order_for(grid, column) ⇒ Object



81
82
83
# File 'lib/datagrid/renderer.rb', line 81

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

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



73
74
75
76
77
78
79
# File 'lib/datagrid/renderer.rb', line 73

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



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/datagrid/renderer.rb', line 53

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