Class: Bootstrap::TableHelper::Table
- Inherits:
-
Object
- Object
- Bootstrap::TableHelper::Table
- Defined in:
- app/helpers/bootstrap/table_helper.rb
Instance Attribute Summary collapse
-
#caption ⇒ Object
readonly
Returns the value of attribute caption.
-
#caption_options ⇒ Object
readonly
Returns the value of attribute caption_options.
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#default_row_options ⇒ Object
readonly
Returns the value of attribute default_row_options.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
-
#show_header ⇒ Object
readonly
Returns the value of attribute show_header.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#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.
- #header_row(&block) ⇒ Object
-
#initialize(template, default_row_options: {}, collection: [], caption: nil, caption_options: {}, type: 'hover', show_header: true, **options) ⇒ Table
constructor
A new instance of Table.
- #row(options = {}, &block) ⇒ Object
- #to_s ⇒ Object
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, **) @template = template @type = type = = @collection = collection @caption = caption = @show_header = show_header @headers = [] @rows = [] @columns = [] @cust_header = false end |
Instance Attribute Details
#caption ⇒ Object (readonly)
Returns the value of attribute caption.
61 62 63 |
# File 'app/helpers/bootstrap/table_helper.rb', line 61 def caption @caption end |
#caption_options ⇒ Object (readonly)
Returns the value of attribute caption_options.
61 62 63 |
# File 'app/helpers/bootstrap/table_helper.rb', line 61 def end |
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
61 62 63 |
# File 'app/helpers/bootstrap/table_helper.rb', line 61 def collection @collection end |
#columns ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute default_row_options.
61 62 63 |
# File 'app/helpers/bootstrap/table_helper.rb', line 61 def end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
61 62 63 |
# File 'app/helpers/bootstrap/table_helper.rb', line 61 def headers @headers end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
61 62 63 |
# File 'app/helpers/bootstrap/table_helper.rb', line 61 def end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
61 62 63 |
# File 'app/helpers/bootstrap/table_helper.rb', line 61 def rows @rows end |
#show_header ⇒ Object (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 |
#template ⇒ Object (readonly)
Returns the value of attribute template.
61 62 63 |
# File 'app/helpers/bootstrap/table_helper.rb', line 61 def template @template end |
#type ⇒ Object (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: {}, **, &block) if show_header && !@cust_header text = if method collection.try(:model).try(:human_attribute_name, method) end header(.reverse_merge!(width: [:width], align: [:align], text: text)) end columns << { method: method, 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( = {}, &block) new_row = Row.new(template, .reverse_merge!()) template.capture(new_row, &block) rows << new_row end |
#to_s ⇒ Object
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, ) template.content_tag(:table, nil, ) do content = '' content << template.content_tag('caption', caption, ) if caption if show_header && headers.any? content << template.content_tag('thead') do header_content = '' headers.each do |h| header_content << h.to_s end header_content.html_safe end end content << template.content_tag(:tbody) do tbody_content = '' if collection && collection.any? collection.each_with_index do |o, i| row() do |r| columns.each do |c| = c[:options].dup text = if c[:method] && o.respond_to?(c[:method]) o.send(c[:method]) end if c[:content] r.column(.merge!(text: text)) do template.capture(o, i, &c[:content]) end else r.column(.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 |