Class: SuperTable::Builder

Inherits:
Object
  • Object
show all
Includes:
ViewHelpers
Defined in:
lib/super_table/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ViewHelpers

#del, #div, included, #small, #span, #strong, #td, #th, #tr

Constructor Details

#initialize(table, view_context) ⇒ Builder

Returns a new instance of Builder.



12
13
14
15
# File 'lib/super_table/builder.rb', line 12

def initialize(table, view_context)
  self.view_context = view_context
  self.table = table
end

Instance Attribute Details

#tableObject

Returns the value of attribute table.



7
8
9
# File 'lib/super_table/builder.rb', line 7

def table
  @table
end

#view_contextObject

Returns the value of attribute view_context.



7
8
9
# File 'lib/super_table/builder.rb', line 7

def view_context
  @view_context
end

Instance Method Details

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/super_table/builder.rb', line 85

def column(key=nil, options={}, &block)
  if block_given?
    options = key
    return td(options, &block)
  end

  content = case key
  when Symbol
    @rows.last.send(key)
  else
    key
  end
  td(content, options)
end

#columns(*args, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/super_table/builder.rb', line 62

def columns(*args, &block)
  @rows = []
  options = args.extract_options!
  wrap_options = options.delete(:wrap) || {}
  if collection.empty?
    return placeholder_row
  end
  collection.each do |record|
    @rows << record
    html = wrap_tr(options, wrap_options) do
      if block_given?
        yield record
      else
        args.each do |key|
          concat td(record.send(key))
        end
      end
    end
    concat html
  end
  nil
end

#head(key = nil, options = {}, &block) ⇒ Object



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
# File 'lib/super_table/builder.rb', line 35

def head(key=nil, options={}, &block)
  if block_given?
    options = key
    return th(options, &block)
  end

  case key
  when Symbol
    column_options = table.columns[key].clone
    title, desc = column_options.delete(:title), column_options.delete(:desc)
  else
    column_options = {}
    title = key
  end

  classes = []
  classes << options.delete(:class) if options[:class]
  classes << column_options.delete(:class) if column_options[:class]
  column_options.merge!(options)
  column_options.merge!(class: classes.join(" ")) if classes.present?

  th(column_options) do
    concat(title)
    concat (:div, desc, class: "small text-primary") if desc
  end
end

#heads(*args, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/super_table/builder.rb', line 17

def heads(*args, &block)
  options = args.extract_options!

  classes = ["table-secondary"]
  classes << options[:class] if options[:class]
  options[:class] = classes.join(" ")

  if block_given?
    tr(options, &block)
  else
    tr(options) do
      args.each do |key|
        concat head(key)
      end
    end
  end
end

#placeholder_rowObject



100
101
102
103
104
105
# File 'lib/super_table/builder.rb', line 100

def placeholder_row
  tr do
    content = table.placeholder || "尚無資料"
    td(content, colspan: table.columns.length, class: "text-center")
  end
end