Class: Tableficate::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/tableficate/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, rows, options, data) ⇒ Table

Returns a new instance of Table.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/tableficate/table.rb', line 5

def initialize(template, rows, options, data)
  @template = template
  @rows     = rows
  @columns  = []
  @filters  = []
  @as       = options[:as] || rows.table_name

  @options = {
    show_sorts: false,
    theme:      ''
  }.merge(options)

  @current_sort = data[:current_sort]
end

Instance Attribute Details

#asObject (readonly)

Returns the value of attribute as.



3
4
5
# File 'lib/tableficate/table.rb', line 3

def as
  @as
end

#columnsObject (readonly)

Returns the value of attribute columns.



3
4
5
# File 'lib/tableficate/table.rb', line 3

def columns
  @columns
end

#current_sortObject (readonly)

Returns the value of attribute current_sort.



3
4
5
# File 'lib/tableficate/table.rb', line 3

def current_sort
  @current_sort
end

#filtersObject (readonly)

Returns the value of attribute filters.



3
4
5
# File 'lib/tableficate/table.rb', line 3

def filters
  @filters
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/tableficate/table.rb', line 3

def options
  @options
end

#rowsObject (readonly)

Returns the value of attribute rows.



3
4
5
# File 'lib/tableficate/table.rb', line 3

def rows
  @rows
end

#templateObject (readonly)

Returns the value of attribute template.



3
4
5
# File 'lib/tableficate/table.rb', line 3

def template
  @template
end

Instance Method Details

#actions(&block) ⇒ Object



28
29
30
# File 'lib/tableficate/table.rb', line 28

def actions(&block)
  @columns.push(ActionColumn.new(self, &block))
end

#column(name, options = {}, &block) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/tableficate/table.rb', line 20

def column(name, options = {}, &block)
  options.reverse_merge!(
    show_sort: @options[:show_sorts]
  )

  @columns.push(Column.new(self, name, options, &block))
end

#filter(name, options = {}) ⇒ Object



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
# File 'lib/tableficate/table.rb', line 36

def filter(name, options = {})
  as_map = {
    :'datetime-local' => Filter::Input,
    text:     Filter::Input,
    email:    Filter::Input,
    url:      Filter::Input,
    tel:      Filter::Input,
    number:   Filter::Input,
    range:    Filter::Input,
    date:     Filter::Input,
    month:    Filter::Input,
    week:     Filter::Input,
    time:     Filter::Input,
    datetime: Filter::Input,
    search:   Filter::Input,
    color:    Filter::Input,
    select:   Filter::Select,
    radio:    Filter::Radio,
    checkbox: Filter::CheckBox
  }

  as = options.delete(:as) || (options[:collection] ? :select : :text)

  raise Filter::UnknownInputType if as_map[as].nil?

  options[:type] = as.to_s

  @filters.push(as_map[as].new(self, name, options))
end

#filter_range(name, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tableficate/table.rb', line 66

def filter_range(name, options = {})
  as_map = {
    :'datetime-local' => Filter::InputRange,
    text:     Filter::InputRange,
    email:    Filter::InputRange,
    url:      Filter::InputRange,
    tel:      Filter::InputRange,
    number:   Filter::InputRange,
    range:    Filter::InputRange,
    date:     Filter::InputRange,
    month:    Filter::InputRange,
    week:     Filter::InputRange,
    time:     Filter::InputRange,
    datetime: Filter::InputRange,
    search:   Filter::InputRange,
    color:    Filter::InputRange,
    select:   Filter::SelectRange
  }

  as = options.delete(:as) || (options[:collection] ? :select : :text)

  raise Filter::UnknownInputType if as_map[as].nil?

  options[:type] = as.to_s

  @filters.push(as_map[as].new(self, name, options))
end

#render(options = {}) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/tableficate/table.rb', line 94

def render(options = {})
  options.reverse_merge!(
    partial: Tableficate::Utils::template_path('table', @options[:theme]),
    locals:  {table: self}
  )

  @template.render options
end

#show_sort?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/tableficate/table.rb', line 32

def show_sort?
  self.columns.any?{|column| column.show_sort?}
end