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

def initialize(template, rows, options, data)
  @template = template
  @rows     = rows

  @as         = options.delete(:as) || rows.table_name
  @theme      = options.delete(:theme) || ''
  @show_sorts = options.delete(:show_sorts) || false
  @attrs      = options

  @columns = []
  @filters = []

  @current_sort = data[:current_sort]
  @field_map    = data[:field_map] || {}
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

#attrsObject (readonly)

Returns the value of attribute attrs.



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

def attrs
  @attrs
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

#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

#themeObject (readonly)

Returns the value of attribute theme.



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

def theme
  @theme
end

Instance Method Details

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



41
42
43
# File 'lib/tableficate/table.rb', line 41

def actions(options = {}, &block)
  @columns.push(ActionColumn.new(self, options, &block))
end

#caption(*args, &block) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/tableficate/table.rb', line 29

def caption(*args, &block)
  if args.empty? and not block_given?
    @caption
  else
    @caption = Caption.new(*args, &block)
  end
end

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



37
38
39
# File 'lib/tableficate/table.rb', line 37

def column(name, options = {}, &block)
  @columns.push(Column.new(self, name, options.reverse_merge(show_sort: @show_sorts), &block))
end

#empty(*args, &block) ⇒ Object



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

def empty(*args, &block)
  if args.empty? and not block_given?
    @empty
  else
    @empty = Empty.new(self, *args, &block)
  end
end

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tableficate/table.rb', line 49

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) || find_as(name, options.has_key?(:collection))

  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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/tableficate/table.rb', line 79

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) || find_as(name, options.has_key?(:collection))

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

  options[:type] = as.to_s

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

#show_sort?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/tableficate/table.rb', line 45

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