Class: TableForHelper::Builder

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper
Defined in:
lib/table_for_helper/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection, options = nil) {|_self| ... } ⇒ Builder

Initialize variables

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/table_for_helper/builder.rb', line 6

def initialize collection, options = nil
  @collection = collection
  @options = 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, options = nil, &block
  # Move a value from the title to options variable for 2 params usage
  if title.is_a? Hash
    options = 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] = ('th', title, options)

  # Save a builder for rows
  @body[name] = block if block_given?
end

Add a footer row column value



46
47
48
49
50
51
52
# File 'lib/table_for_helper/builder.rb', line 46

def footer name, options = nil, &block
  # Save a builder for a footer cell
  @footer[name] = block if block_given?

  # Save options for a footer cell
  @footer_options[name] = options if options
end

#to_htmlObject

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 = ('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 << ('td', cell.to_s)
    end
    body << ('tr', row.html_safe)
  end

  # Construct the footer row
  footer = ''
  if !@footer.empty?
    columns.each do |column|
      if @footer[column]
        footer << ('td', @footer[column].call(@collection), @footer_options[column])
      else
        footer << ('td')
      end
    end
    footer = ('tr', footer.html_safe)
  end

  # Join all table parts, put it to a table tag and return
  table = ''
  table << ('thead', head)
  table << ('tbody', body.html_safe + footer.html_safe)
  ('table', table.html_safe, @options)
end