Class: TableForHelper::Builder
- Inherits:
-
Object
- Object
- TableForHelper::Builder
- Includes:
- ActionView::Helpers::TagHelper
- Defined in:
- lib/table_for_helper/builder.rb
Instance Method Summary collapse
-
#attribute_name(attribute) ⇒ Object
Get a localized attribute name.
-
#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.
-
#model_name(options = {}) ⇒ Object
Get a localized model name.
-
#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 18 19 20 21 22 |
# File 'lib/table_for_helper/builder.rb', line 6 def initialize collection, = nil @collection = collection @options = .symbolize_keys if .has_key?(:model) @model = [:model] .delete :model else @model = collection.first.class end @head = {} @body = {} @footer = {} @footer_options = {} yield self if block_given? end |
Instance Method Details
#attribute_name(attribute) ⇒ Object
Get a localized attribute name
25 26 27 |
# File 'lib/table_for_helper/builder.rb', line 25 def attribute_name attribute @model.methods.include?(:human_attribute_name) ? @model.human_attribute_name(attribute) : attribute.to_s end |
#column(name, title = nil, options = nil, &block) ⇒ Object
Add a new column to the table
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/table_for_helper/builder.rb', line 30 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) && name.methods.include?(:model_name) title = name.model_name.human # A title for a model attribute else title = 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
56 57 58 59 60 61 62 |
# File 'lib/table_for_helper/builder.rb', line 56 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 |
#model_name(options = {}) ⇒ Object
Get a localized model name
65 66 67 |
# File 'lib/table_for_helper/builder.rb', line 65 def model_name = {} @model.methods.include?(:human_attribute_name) ? @model.model_name.human() : @model.to_s end |
#to_html ⇒ Object
Generate a html
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/table_for_helper/builder.rb', line 70 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 |