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, table:, template:) ⇒ Renderer

Returns a new instance of Renderer.



34
35
36
# File 'lib/trestle/table/column.rb', line 34

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

Instance Method Details

#classesObject



87
88
89
# File 'lib/trestle/table/column.rb', line 87

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

#content(instance) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/trestle/table/column.rb', line 71

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



91
92
93
# File 'lib/trestle/table/column.rb', line 91

def data
  options[:data]
end

#headerObject



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

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

  if @table.sortable? && @column.sortable?
    link_options = @column.sort_options.slice(:default, :default_order)
    @template.sort_link(header_text, @column.sort_field, **link_options)
  else
    header_text
  end
end

#render(instance) ⇒ Object



38
39
40
# File 'lib/trestle/table/column.rb', line 38

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

#render?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/trestle/table/column.rb', line 42

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