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
18
19
20
21
22
# File 'lib/table_for_helper/builder.rb', line 6

def initialize collection, options = nil
  @collection = collection
  @options = options.symbolize_keys
  if options.has_key?(:model)
    @model = options[:model]
    options.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, 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) && 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] = ('th', title, options)

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

Add a footer row column value



56
57
58
59
60
61
62
# File 'lib/table_for_helper/builder.rb', line 56

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

#model_name(options = {}) ⇒ Object

Get a localized model name



65
66
67
# File 'lib/table_for_helper/builder.rb', line 65

def model_name options = {}
  @model.methods.include?(:human_attribute_name) ? @model.model_name.human(options) : @model.to_s
end

#to_htmlObject

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 = ('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