Sha256: 49150a8e823adcac8a248619c33c5d807f0d993793667a91cd7233fd2650f11d

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 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] }
# header = ['Username', 'First name', 'Last name', 'Email']
# ?>
# 
# <div class="custom-table-class">
#   #{table(users, header)}
# </div>
#--
# TODO: sorting, thead/tbody/legend etc, verbose...
#++

module TableMixin

  # [+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 << '><tr>'

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

    str << "</tr>"

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

    str << "</table>"

    return str
  end
  alias_method :build_table, :table

end

end 

# * George Moschovitis <gm@navel.gr>

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nitro-0.21.0 lib/nitro/mixin/table.rb
nitro-0.21.2 lib/nitro/mixin/table.rb
nitro-0.22.0 lib/nitro/mixin/table.rb
nitro-0.23.0 lib/nitro/mixin/table.rb