Sha256: 4800d014e58f81fd36ccd785050ae59398db1fd95d4f42e8926b895e7d21a50e

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module Nitro

# 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>
#--
# TODO: sorting, thead/tbody/legend etc, verbose...
#++

module TableHelper

  # [+options+]
  #    A hash of options.
  # 
  # :id = id of the component.
  # :headers = an array of the header values
  # :values = an array of arrays.

  def table(options)
    c = options

    str = '<table'
    str << %| id="#{c[:id]}"| if c[:id]
    str << '>'

    str << table_rows(options)
    
    str << '</table>'
  end
  alias_method :build_table, :table

  # [+options+]
  #    A hash of options.
  # 
  # :id = id of the component.
  # :headers = an array of the header values
  # :values = an array of arrays.

  def table_rows(options)
    c = options

    str = '<tr>'

    for h in c[:headers]
      str << %|<th>#{h}</th>|
    end

    str << "</tr>"

    items = c[:values] || c[:items] || c[:rows]

    for row in c[:values]
      str << "<tr>"
      
      for v in row
        str << %|<td>#{v}</td>|
      end
      
      str << "</tr>"
    end

    return str
  end

end

end 

# * George Moschovitis <gm@navel.gr>

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.25.0 lib/nitro/helper/table.rb
nitro-0.26.0 lib/nitro/helper/table.rb