Module TableHelper
In: app/helpers/table_helper.rb

Build HTML table with standard structure. Wrap caption div + table in div container for consistent captions across browsers. Show "None" if empty content. Add sortable headers.

Methods

sort_rows   table   th  

Public Instance methods

Sort rows in memory based on sort_by. Paginated table contents need to be sorted in DB.

[Source]

    # File app/helpers/table_helper.rb, line 54
54:   def sort_rows(collection, sort_by, sort_direction)
55:     return collection if sort_by.blank?
56:     
57:      sort_by.split(",").each do |sort_attribute|
58:       sort_attribute_symbol = sort_attribute.to_sym
59:       collection.sort! { |x, y| (x.send(sort_attribute_symbol) || "") <=> (y.send(sort_attribute_symbol) || "") }
60:     end
61:       
62:     collection.reverse! if sort_direction == "desc"
63:     collection
64:   end

Arguments

  • caption
  • caption_visible. Default true
  • id. CSS ID
  • style_class or class. CSS style class
  • collection. Table contents. Only used to show "None"
  • columns. Default 1. If insert_header is true, insert columns <th />
  • insert_header. Insert <th/> for "bar" on top of tables

[Source]

    # File app/helpers/table_helper.rb, line 13
13:   def table(options = {}, &block)
14:     # TODO Use merge or something
15:     options[:caption] = nil unless options[:caption]
16:     options[:caption_visible] = true unless options[:caption_visible]
17:     options[:new_action] = nil unless options[:new_action]
18:     options[:id] = nil unless options[:id]
19:     options[:style_class] = options[:class]
20:     options.delete(:class)
21:     options[:collection] = options[:collection]
22:     options[:columns] = options[:columns] || 1
23:     options[:insert_header] = nil unless (options[:insert_header] && RacingAssociation.current.always_insert_table_headers?)
24:     block_to_partial "table/base", options, &block
25:   end

Arguments

  • sort_by: Sort by this attribute
  • style_class or class. CSS
  • title. <th>title</th>
  • sort_params. Append to sort link
  • sort_direction. "asc" or "desc"

[Source]

    # File app/helpers/table_helper.rb, line 33
33:   def th(attribute = nil, *options)
34:     _attribute = nil
35:     _attribute = attribute.to_s if attribute
36: 
37:     locals = { :attribute => _attribute }
38:     options = options.extract_options!
39:     locals[:sort_by] = [options[:sort_by] || _attribute].flatten
40:     locals[:style_class] = options[:class] || _attribute
41:     locals[:title] = options[:title] || (_attribute.titlecase  if _attribute)
42:     locals[:sort_params] = options[:sort_params] || {}
43: 
44:     if params[:sort_by] == _attribute && params[:sort_direction] == "asc"
45:       locals[:sort_direction] = "desc"
46:     else
47:       locals[:sort_direction] = "asc"
48:     end
49: 
50:     render :partial => "table/th", :locals => locals
51:   end

[Validate]