Class: Datagrid::Renderer

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

Overview

:nodoc:

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

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



29
30
31
32
33
34
35
# File 'lib/datagrid/renderer.rb', line 29

def form_for(grid, options = {})
  options[:method] ||= :get
  options[:html] ||= {}
  options[:html][:class] ||= "datagrid-form #{@template.dom_class(grid)}"
  options[:as] ||= grid.param_name
  _render_partial('form', options[:partials], {: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
# 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 = grid.html_value(column, @template, asset)

  url = column.options[:url] && column.options[:url].call(asset)
  if url
    @template.link_to(value, url)
  else
    value
  end
end

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



51
52
53
54
55
56
# File 'lib/datagrid/renderer.rb', line 51

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

  _render_partial('head', options[:partials],
                  { :grid => grid, :options => options })
end

#order_for(grid, column, options = {}) ⇒ Object



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

def order_for(grid, column, options = {})
  _render_partial('order_for', options[:partials],
                  { :grid => grid, :column => column })
end

#order_path(grid, column, descending, request) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/datagrid/renderer.rb', line 81

def order_path(grid, column, descending, request)
  column = grid.column_by_name(column)
  query = request ? request.query_parameters : {}
  ActionDispatch::Http::URL.path_for(
    path: request ? request.path : '/',
    params: query.merge(grid.query_params(order: column.name, descending: descending))
  )
end

#rows(grid, assets = grid.assets, **options, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/datagrid/renderer.rb', line 58

def rows(grid, assets = grid.assets, **options, &block)
  result = assets.map do |asset|
    if block_given?
      @template.capture do
        yield(Datagrid::Helper::HtmlRow.new(@template, grid, asset))
      end
    else
      _render_partial( 'row', options[:partials], {
        :grid => grid,
        :options => options,
        :asset => asset
      })
    end
  end.to_a.join

  _safe(result)
end

#table(grid, *args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/datagrid/renderer.rb', line 37

def table(grid, *args)
  options = args.extract_options!
  options[:html] ||= {}
  options[:html][:class] ||= "datagrid #{@template.dom_class(grid)}"
  assets = args.any? ? args.shift : grid.assets

  _render_partial('table', options[:partials],
                  {
                    :grid => grid,
                    :options => options,
                    :assets => assets
                  })
end