Class: Bootstrap::TableHelper::Row

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, options = {}) ⇒ Row

Returns a new instance of Row.



7
8
9
10
11
# File 'app/helpers/bootstrap/table_helper.rb', line 7

def initialize(template, options = {})
  @template = template
  @options = options
  @columns = []
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



5
6
7
# File 'app/helpers/bootstrap/table_helper.rb', line 5

def columns
  @columns
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'app/helpers/bootstrap/table_helper.rb', line 5

def options
  @options
end

#templateObject (readonly)

Returns the value of attribute template.



5
6
7
# File 'app/helpers/bootstrap/table_helper.rb', line 5

def template
  @template
end

Instance Method Details

#column(width: nil, align: 'left', text: nil, tag: :td, **options, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/helpers/bootstrap/table_helper.rb', line 13

def column(width: nil, align: 'left', text: nil, tag: :td, **options, &block)
  if width && width.to_i <= 12 && width.to_i > 0
    template.merge_predef_class("span#{width.to_i}", options)
  end

  columns << {
    options: options,
    align: align,
    text: text,
    tag: tag,
    content: block
  }
end

#header(tag: :th, **options, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'app/helpers/bootstrap/table_helper.rb', line 27

def header(tag: :th, **options, &block)
  if block_given?
    column(options.merge!(tag: tag)) do
      template.capture(&block)
    end
  else
    column(options.merge!(tag: tag))
  end
end

#to_sObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/bootstrap/table_helper.rb', line 37

def to_s
  template.(:tr, nil, options) do
    tr_content = ''
    columns.each do |c|
      column_options = c[:options]
      column_options[:style] ||= ''
      column_options[:style] << "text-align: #{c[:align]};"

      tr_content << template.(c[:tag], nil, column_options) do
        if c[:content]
          template.capture(&c[:content])
        else
          c[:text]
        end
      end
    end

    tr_content.html_safe
  end
end