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



95
96
97
# File 'lib/trestle/table/column.rb', line 95

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

#content(instance) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/trestle/table/column.rb', line 79

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



99
100
101
# File 'lib/trestle/table/column.rb', line 99

def data
  options[:data]
end

#headerObject



70
71
72
73
74
75
76
77
# File 'lib/trestle/table/column.rb', line 70

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

  header = @column.header
  header = @template.instance_exec(&header) if header.respond_to?(:call)
  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

#render?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/trestle/table/column.rb', line 52

def render?
  if options.key?(:if)
    if options[:if].respond_to?(:call)
      @template.instance_exec(&options[:if])
    else
      options[:if]
    end
  elsif options.key?(:unless)
    if options[:unless].respond_to?(:call)
      !@template.instance_exec(&options[:unless])
    else
      !options[:unless]
    end
  else
    true
  end
end