Class: Bootstrap::TableHelper::Table

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/bootstrap/table_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, default_row_options: {}, collection: [], caption: nil, caption_options: {}, type: 'hover', show_header: true, **options) ⇒ Table

Returns a new instance of Table.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/bootstrap/table_helper.rb', line 64

def initialize(template,
  default_row_options: {},
  collection: [],
  caption: nil,
  caption_options: {},
  type: 'hover',
  show_header: true,
  **options)
  @template = template
  @type = type
  @default_row_options = default_row_options
  @options = options
  @collection = collection
  @caption = caption
  @caption_options = caption_options
  @show_header = show_header
  @headers = []
  @rows = []
  @columns = []
  @cust_header = false
end

Instance Attribute Details

#captionObject (readonly)

Returns the value of attribute caption.



61
62
63
# File 'app/helpers/bootstrap/table_helper.rb', line 61

def caption
  @caption
end

#caption_optionsObject (readonly)

Returns the value of attribute caption_options.



61
62
63
# File 'app/helpers/bootstrap/table_helper.rb', line 61

def caption_options
  @caption_options
end

#collectionObject (readonly)

Returns the value of attribute collection.



61
62
63
# File 'app/helpers/bootstrap/table_helper.rb', line 61

def collection
  @collection
end

#columnsObject (readonly)

Returns the value of attribute columns.



61
62
63
# File 'app/helpers/bootstrap/table_helper.rb', line 61

def columns
  @columns
end

#default_row_optionsObject (readonly)

Returns the value of attribute default_row_options.



61
62
63
# File 'app/helpers/bootstrap/table_helper.rb', line 61

def default_row_options
  @default_row_options
end

#headersObject (readonly)

Returns the value of attribute headers.



61
62
63
# File 'app/helpers/bootstrap/table_helper.rb', line 61

def headers
  @headers
end

#optionsObject (readonly)

Returns the value of attribute options.



61
62
63
# File 'app/helpers/bootstrap/table_helper.rb', line 61

def options
  @options
end

#rowsObject (readonly)

Returns the value of attribute rows.



61
62
63
# File 'app/helpers/bootstrap/table_helper.rb', line 61

def rows
  @rows
end

#show_headerObject (readonly)

Returns the value of attribute show_header.



61
62
63
# File 'app/helpers/bootstrap/table_helper.rb', line 61

def show_header
  @show_header
end

#templateObject (readonly)

Returns the value of attribute template.



61
62
63
# File 'app/helpers/bootstrap/table_helper.rb', line 61

def template
  @template
end

#typeObject (readonly)

Returns the value of attribute type.



61
62
63
# File 'app/helpers/bootstrap/table_helper.rb', line 61

def type
  @type
end

Instance Method Details

#column(method: nil, header_options: {}, **options, &block) ⇒ Object

options

width - 1~12
method - object's method, use send(method) to get value
align - text alignment: left, center and right
header_options - options of header


91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/helpers/bootstrap/table_helper.rb', line 91

def column(method: nil, header_options: {}, **options, &block)
  if show_header && !@cust_header

    text = if method
      collection.try(:model).try(:human_attribute_name, method)
    end

    header(header_options.reverse_merge!(width: options[:width],
                                         align: options[:align],
                                         text: text))
  end

  columns << { method: method, options: options, content: block }
end

#header_row(&block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'app/helpers/bootstrap/table_helper.rb', line 112

def header_row(&block)
  if @cust_header == false
    @cust_header = true
    headers.clear
  end

  new_row = Row.new(template)
  template.capture(new_row, &block)
  headers << new_row
end

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



106
107
108
109
110
# File 'app/helpers/bootstrap/table_helper.rb', line 106

def row(options = {}, &block)
  new_row = Row.new(template, options.reverse_merge!(default_row_options))
  template.capture(new_row, &block)
  rows << new_row
end

#to_sObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/helpers/bootstrap/table_helper.rb', line 123

def to_s
  table_class = 'table'
  type.split(' ').each do |table_type|
    table_class << " table-#{table_type}"
  end

  template.merge_predef_class(table_class, options)
  template.(:table, nil, options) do
    content = ''
    content << template.('caption', caption, caption_options) if caption

    if show_header && headers.any?
      content << template.('thead') do
        header_content = ''
        headers.each do |h|
          header_content << h.to_s
        end
        header_content.html_safe
      end
    end

    content << template.(:tbody) do
      tbody_content = ''
      if collection && collection.any?
        collection.each_with_index do |o, i|
          row(default_row_options) do |r|
            columns.each do |c|
              column_options = c[:options].dup

              text = if c[:method] && o.respond_to?(c[:method])
                o.send(c[:method])
              end

              if c[:content]
                r.column(column_options.merge!(text: text)) do
                  template.capture(o, i, &c[:content])
                end
              else
                r.column(column_options.merge!(text: text))
              end
            end
          end
        end
      end

      rows.each do |r|
        tbody_content << r.to_s
      end

      tbody_content.html_safe
    end

    content.html_safe
  end

end