class Datagrid::Renderer

@!visibility private

Public Class Methods

for(template) click to toggle source
# File lib/datagrid/renderer.rb, line 7
def self.for(template)
  new(template)
end
new(template) click to toggle source
# File lib/datagrid/renderer.rb, line 11
def initialize(template)
  @template = template
end

Public Instance Methods

form_for(grid, options = {}) click to toggle source
# File lib/datagrid/renderer.rb, line 30
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) click to toggle source
# File lib/datagrid/renderer.rb, line 15
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 = {}) click to toggle source
# File lib/datagrid/renderer.rb, line 50
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 = {}) click to toggle source
# File lib/datagrid/renderer.rb, line 73
def order_for(grid, column, options = {})
  _render_partial('order_for', options[:partials],
                  { :grid => grid, :column => column })
end
order_path(grid, column, descending, request) click to toggle source
# File lib/datagrid/renderer.rb, line 78
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
row(grid, asset, **options, &block) click to toggle source
# File lib/datagrid/renderer.rb, line 65
def row(grid, asset, **options, &block)
  Datagrid::Helper::HtmlRow.new(self, grid, asset, options).tap do |row|
    if block_given?
      return @template.capture(row, &block)
    end
  end
end
rows(grid, assets = grid.assets, **options, &block) click to toggle source
# File lib/datagrid/renderer.rb, line 57
def rows(grid, assets = grid.assets, **options, &block)
  result = assets.map do |asset|
    row(grid, asset, **options, &block)
  end.to_a.join

  _safe(result)
end
table(grid, assets, **options) click to toggle source
# File lib/datagrid/renderer.rb, line 38
def table(grid, assets, **options)
  options[:html] ||= {}
  options[:html][:class] ||= "datagrid #{@template.dom_class(grid)}"

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

Private Instance Methods

_render_partial(partial_name, partials_path, locals = {}) click to toggle source
# File lib/datagrid/renderer.rb, line 93
def _render_partial(partial_name, partials_path, locals = {})
  @template.render({
    :partial => File.join(partials_path || 'datagrid', partial_name),
    :locals => locals
  })
end
_safe(string) click to toggle source
# File lib/datagrid/renderer.rb, line 89
def _safe(string)
  string.respond_to?(:html_safe) ? string.html_safe : string
end