Class: TableForHelper::Builder
- Inherits:
-
Object
- Object
- TableForHelper::Builder
- Includes:
- ActionView::Helpers::TagHelper
- Defined in:
- lib/table_for_helper/builder.rb
Instance Method Summary collapse
-
#column(name, title = nil, options = nil, &block) ⇒ Object
Add a new column to the table.
-
#footer(name, options = nil, &block) ⇒ Object
Add a footer row column value.
-
#initialize(collection, options = nil) {|_self| ... } ⇒ Builder
constructor
Initialize variables.
-
#to_html ⇒ Object
Generate a html.
Constructor Details
#initialize(collection, options = nil) {|_self| ... } ⇒ Builder
Initialize variables
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/table_for_helper/builder.rb', line 6 def initialize collection, = nil @collection = collection @options = .symbolize_keys @model = collection.first.class @head = {} @body = {} @footer = {} @footer_options = {} yield self if block_given? end |
Instance Method Details
#column(name, title = nil, options = nil, &block) ⇒ Object
Add a new column to the table
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/table_for_helper/builder.rb', line 20 def column name, title = nil, = nil, &block # Move a value from the title to options variable for 2 params usage if title.is_a? Hash = title title = nil end # Take a title for this column from the dictionary if !title # A title for a reference if name.is_a? Class title = name.model_name.human # A title for an model attribute else title = @model.human_attribute_name(name) end end # Create a html head cell @head[name] = content_tag('th', title, ) # Save a builder for rows @body[name] = block if block_given? end |
#footer(name, options = nil, &block) ⇒ Object
Add a footer row column value
46 47 48 49 50 51 52 |
# File 'lib/table_for_helper/builder.rb', line 46 def name, = nil, &block # Save a builder for a footer cell @footer[name] = block if block_given? # Save options for a footer cell @footer_options[name] = if end |
#to_html ⇒ Object
Generate a html
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/table_for_helper/builder.rb', line 55 def to_html # Add all model attributes if no block given @model.attribute_names.each{|name| column(name)} if @head.empty? # Save list of view attributes columns = @head.keys # Create a html row tag for head cells head = content_tag('tr', @head.values.join.html_safe) # Construct body html rows body = '' @collection.each do |resource| row = '' columns.each do |column| if @body[column] cell = @body[column].call(resource) else cell = resource[column] end row << content_tag('td', cell.to_s) end body << content_tag('tr', row.html_safe) end # Construct the footer row = '' if !@footer.empty? columns.each do |column| if @footer[column] << content_tag('td', @footer[column].call(@collection), @footer_options[column]) else << content_tag('td') end end = content_tag('tr', .html_safe) end # Join all table parts, put it to a table tag and return table = '' table << content_tag('thead', head) table << content_tag('tbody', body.html_safe + .html_safe) content_tag('table', table.html_safe, @options) end |