blob: f16fc66be2126021c02a9aa79a8de4920e88d78e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
module TableBuilderHelper
class Column
attr_reader :key, :name, :attribute, :sortable
def initialize(key: nil, name: '', attribute:, sortable: true, link_to: nil, **opts)
if key.nil? && name.empty?
raise ColumnMustHaveKeyOrNameError
end
opts ||= {}
@key = key
@name = name
@attribute = attribute
@sortable = sortable
@link_to = link_to
@condition = opts[:if]
@extra_class = opts[:class]
end
def value(obj)
return unless check_condition(obj)
if @attribute.is_a?(Proc)
@attribute.call(obj)
else
obj.try(@attribute)
end
end
def header_label(model = nil)
return @name if @name.present?
# Transform `Chouette::Line` into "line"
model_key = model.to_s.underscore
model_key.gsub! 'chouette/', ''
model_key.gsub! '/', '.'
I18n.t("activerecord.attributes.#{model_key}.#{@key}")
end
def linkable?
!@link_to.nil?
end
def link_to(obj)
return unless check_condition(obj)
@link_to.call(obj)
end
def check_condition(obj)
condition_val = true
if @condition.present?
condition_val = @condition
condition_val = condition_val.call(obj) if condition_val.is_a?(Proc)
end
!!condition_val
end
def td_class(obj)
out = []
out << @attribute if @attribute.is_a?(String) || @attribute.is_a?(Symbol)
out << @extra_class
out = out.compact.join ' '
out.present? ? out : nil
end
end
class ColumnMustHaveKeyOrNameError < StandardError; end
end
|