Class: Trestle::Table::Column::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/trestle/table/column.rb

Instance Method Summary collapse

Constructor Details

#initialize(column, template) ⇒ Renderer

Returns a new instance of Renderer.



44
45
46
# File 'lib/trestle/table/column.rb', line 44

def initialize(column, template)
  @column, @template = column, template
end

Instance Method Details

#classesObject



76
77
78
# File 'lib/trestle/table/column.rb', line 76

def classes
  [options[:class], ("text-#{options[:align]}" if options[:align])].compact
end

#content(instance) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/trestle/table/column.rb', line 60

def content(instance)
  value = column_value(instance)
  content = @template.format_value(value, options)

  if value.respond_to?(:id) && options[:link] != false
    # Column value was a model instance (e.g. from an association).
    # Automatically link to instance's admin if available
    content = @template.admin_link_to(content, value)
  elsif options[:link]
    # Explicitly link to the specified admin, or the table's admin
    content = @template.admin_link_to(content, instance, admin: options[:admin] || table.admin)
  end

  content
end

#dataObject



80
81
82
# File 'lib/trestle/table/column.rb', line 80

def data
  options[:data]
end

#headerObject



52
53
54
55
56
57
58
# File 'lib/trestle/table/column.rb', line 52

def header
  return if options.key?(:header) && options[:header].in?([nil, false])

  header = @column.header
  header = @template.sort_link(header, @column.sort_field, @column.sort_options) if @column.sortable?
  header
end

#render(instance) ⇒ Object



48
49
50
# File 'lib/trestle/table/column.rb', line 48

def render(instance)
  @template.(:td, content(instance), class: classes, data: data)
end