Class: Trestle::Table::Automatic

Inherits:
Trestle::Table show all
Defined in:
lib/trestle/table/automatic.rb

Instance Attribute Summary

Attributes inherited from Trestle::Table

#options, #row

Instance Method Summary collapse

Methods inherited from Trestle::Table

#admin, #autolink?, #header?, #renderer, #sortable?, #with_options

Constructor Details

#initialize(admin) ⇒ Automatic

Returns a new instance of Automatic.



4
5
6
# File 'lib/trestle/table/automatic.rb', line 4

def initialize(admin)
  super(sortable: true, admin: admin)
end

Instance Method Details

#actions_columnObject



30
31
32
# File 'lib/trestle/table/automatic.rb', line 30

def actions_column
  ActionsColumn.new
end

#columnsObject



8
9
10
# File 'lib/trestle/table/automatic.rb', line 8

def columns
  content_columns + [actions_column]
end

#content_columnsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/trestle/table/automatic.rb', line 12

def content_columns
  admin.default_table_attributes.map.with_index do |attribute, index|
    case attribute.type
    when :association
      Column.new(attribute.association_name, sort: false)
    when :enum
      Column.new(attribute.name, align: :center, sort: attribute.name) do |instance|
        if value = instance.public_send(attribute.name)
          full_attribute_name = [attribute.name.to_s.pluralize, value].join(".")
          status_tag(admin.human_attribute_name(full_attribute_name, default: value.humanize))
        end
      end
    else
      Column.new(attribute.name, link: index.zero?, align: (:center if [:datetime, :boolean].include?(attribute.type)))
    end
  end
end