-
Notifications
You must be signed in to change notification settings - Fork 41
/
presenter.rb
72 lines (60 loc) · 1.69 KB
/
presenter.rb
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
69
70
71
72
module TableCloth
class Presenter
attr_reader :view_context, :table_definition, :objects,
:table
def initialize(objects, table, view)
@objects = objects
@table_definition = table
@view_context = view
@table = table_definition.new(objects, view)
end
def v
view_context
end
def render_table
raise NoMethodError, "You must override the .render method"
end
def render_header
raise NoMethodError, "You must override the .header method"
end
def render_rows
raise NoMethodError, "You must override the .rows method"
end
def columns
@columns ||= table.class.columns.map do |name, options|
column = options[:class].new(name, options[:options])
if ColumnJury.new(column, table).available?
column
else
nil
end
end.compact
end
def column_names
@column_names ||= columns.each_with_object([]) do |column, names|
names << column.human_name
end
end
def row_values(object)
columns.each_with_object([]) do |column, values|
values << column.value(object, view_context, table)
end
end
def rows
objects.each_with_object([]) do |object, row|
row << row_values(object)
end
end
def wrapper_tag(type, value=nil, options={}, &block)
table_config = table.class.config.send(type).to_hash
tag_options = TableCloth.config_for(type)
tag_options.merge!(table_config)
tag_options.merge!(options)
content = if block_given?
v.content_tag(type, tag_options, &block)
else
v.content_tag(type, value, tag_options)
end
end
end
end