Class: ActiveAdmin::Views::TableFor
- Inherits:
-
Arbo::HTML::Table
- Object
- Arbo::HTML::Table
- ActiveAdmin::Views::TableFor
show all
- Defined in:
- lib/active_admin/views/components/table_for.rb
Defined Under Namespace
Classes: Column
Instance Method Summary
collapse
Instance Method Details
#build(obj, *attrs) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/active_admin/views/components/table_for.rb', line 10
def build(obj, *attrs)
options = attrs.
@sortable = options.delete(:sortable)
@collection = obj.respond_to?(:each) && !obj.is_a?(Hash) ? obj : [obj]
@resource_class = options.delete(:i18n)
@resource_class ||= @collection.klass if @collection.respond_to? :klass
@columns = []
@row_class = options.delete(:row_class)
build_table
super(options)
columns(*attrs)
end
|
#build_table ⇒ Object
55
56
57
58
|
# File 'lib/active_admin/views/components/table_for.rb', line 55
def build_table
build_table_head
build_table_body
end
|
#build_table_body ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/active_admin/views/components/table_for.rb', line 84
def build_table_body
@tbody = tbody do
@collection.each do |elem|
classes = [helpers.cycle('odd', 'even')]
if @row_class
classes << @row_class.call(elem)
end
tr(class: classes.flatten.join(' '), id: dom_id_for(elem))
end
end
end
|
#build_table_cell(col, resource) ⇒ Object
99
100
101
102
103
104
105
|
# File 'lib/active_admin/views/components/table_for.rb', line 99
def build_table_cell(col, resource)
td class: col.html_class do
html = helpers.format_attribute(resource, col.data)
current_arbo_element << html unless current_arbo_element.children.include? html
end
end
|
#build_table_head ⇒ Object
60
61
62
63
64
|
# File 'lib/active_admin/views/components/table_for.rb', line 60
def build_table_head
@thead = thead do
= tr
end
end
|
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/active_admin/views/components/table_for.rb', line 66
def (col)
classes = Arbo::HTML::ClassList.new
sort_key = sortable? && col.sortable? && col.sort_key
params = request.query_parameters.except :page, :order, :commit, :format
classes << 'sortable' if sort_key
classes << "sorted-#{current_sort[1]}" if sort_key && current_sort[0] == sort_key
classes << col.html_class
if sort_key
th class: classes do
link_to col.pretty_title, params: params, order: "#{sort_key}_#{order_for_sort_key(sort_key)}"
end
else
th col.pretty_title, class: classes
end
end
|
#column(*args, &block) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/active_admin/views/components/table_for.rb', line 28
def column(*args, &block)
options = default_options.merge(args.)
title = args[0]
data = args[1] || args[0]
col = Column.new(title, data, @resource_class, options, &block)
@columns << col
within do
(col)
end
@collection.each_with_index do |resource, index|
within @tbody.children[index] do
build_table_cell col, resource
end
end
end
|
#columns(*attrs) ⇒ Object
24
25
26
|
# File 'lib/active_admin/views/components/table_for.rb', line 24
def columns(*attrs)
attrs.each {|attr| column(attr) }
end
|
#current_sort ⇒ Object
Returns an array for the current sort order
current_sort[0]
current_sort[1]
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/active_admin/views/components/table_for.rb', line 110
def current_sort
@current_sort ||= begin
order_clause = active_admin_config.order_clause.new(active_admin_config, params[:order])
if order_clause.valid?
[order_clause.field, order_clause.order]
else
[]
end
end
end
|
#default_options ⇒ Object
132
133
134
135
136
|
# File 'lib/active_admin/views/components/table_for.rb', line 132
def default_options
{
i18n: @resource_class
}
end
|
#order_for_sort_key(sort_key) ⇒ Object
Returns the order to use for a given sort key
Default is to use ‘desc’. If the current sort key is ‘desc’ it will return ‘asc’
126
127
128
129
130
|
# File 'lib/active_admin/views/components/table_for.rb', line 126
def order_for_sort_key(sort_key)
current_key, current_order = current_sort
return 'desc' unless current_key == sort_key
current_order == 'desc' ? 'asc' : 'desc'
end
|
#sortable? ⇒ Boolean
49
50
51
|
# File 'lib/active_admin/views/components/table_for.rb', line 49
def sortable?
!!@sortable
end
|
#tag_name ⇒ Object
6
7
8
|
# File 'lib/active_admin/views/components/table_for.rb', line 6
def tag_name
'table'
end
|