Module: Raw::TableHelper

Defined in:
lib/raw/helper/table.rb

Overview

The TableBuilder is a helper class that automates the creation of tables from collections of objects. The resulting html can be styled using css.

Example

<?r users = User.all.map { |u| [u.name, u.first_name, u.last_name, u.email] } headers = [‘Username’, ‘First name’, ‘Last name’, ‘Email’] ?>

<div class=“custom-table-class”>

#{table :values => users, :headers => header}

</div>

Extended Example

<?r
users = User.all.map { |u| [u.name, u.first_name, u.last_name, u.email] }
headers = ['Username', 'First name', 'Last name', 'Email']
order_opts = { :right => true, # right align the order controls
     :values => [nil, 'first_name', 'last_name'], # column names from DB
     :asc_pic => "/images/asc.png", 
     :desc_pic => "/images/desc.png" }
?>

<div class="custom-table-class">
  #{table :values => users, :headers => header, 
   :order => order_opts, :alternating_rows => true }
</div>

– TODO: legend, verbose… ? TODO, gmosx: Remove crappy, bloatware additions. ++

Instance Method Summary collapse

Instance Method Details

#table(options) ⇒ Object Also known as: build_table

options

A hash of options.

:id = id of the component. :class = class of the component :headers = an array of the header values :values = an array of arrays. :order = options hash (:left, :right, :asc_pic, :desc_pic, :values) :alternating_rows = alternating rows, use css to color row_even / row_odd :footers = an array of tfooter values



65
66
67
68
69
70
71
72
73
74
# File 'lib/raw/helper/table.rb', line 65

def table(options)
  str = '<table'
  str << %| id="#{options[:id]}"| if options[:id]
  str << %| class="#{options[:class]}"| if options[:class]
  str << '>'
  
  str << table_rows(options)
  
  str << '</table>'
end

#table_rows(options) ⇒ Object

options

A hash of options.

:headers = an array of the header values :values = an array of arrays. :order = options hash (:left, :right, :asc_pic, :desc_pic, :values) :alternating_rows = alternating rows, use css to color row_even / row_odd :footers = an array of tfooter values



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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/raw/helper/table.rb', line 86

def table_rows(options)
  # also accept :items, :rows
  options[:values] = options[:values] || options[:items] || options[:rows]

  str = ''
  str << table_header(options) if options[:headers]
  str << table_footer(options) if options[:footers]
  
  items = options[:values]
  
  row_state = 'odd' if options[:alternating_rows]
  
  # when items is an array of arrays of arrays (meaning, several
  # arrays deviding the table into several table parts (tbodys))
  
  if create_tbody?(options)
    for body in items
      str << '<tbody>'
      
      for row in body
        str << '<tr'
        
        if options[:alternating_rows]
          str << %| class="row_#{row_state}"|
          row_state = (row_state == "odd" ? "even" : "odd")
        end
        
        str << '>'
        
        for value in row
          str << %|<td>#{value}</td>|
        end
        
        str << '</tr>'
      end
      
      str << '</tbody>'
    end
  else
    for row in items
      str << '<tr'
      
      if options[:alternating_rows]
        str << %| class="row_#{row_state}"|
        row_state = (row_state == "odd" ? "even" : "odd")
      end
      
      str << '>'
      
      for value in row
        str << %|<td>#{value}</td>|
      end
      
      str << '</tr>'
    end
  end
  
  return str
end