require 'glue/uri'
require 'glue/configuration'
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
#
#
#
#
# #{table :values => users, :headers => header}
#
#
#
# === Extended Example
#
# 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" }
# ?>
#
#
# #{table :values => users, :headers => header,
# :order => order_opts, :alternating_rows => true }
#
#
#--
# TODO: legend, verbose... ?
#++
module TableHelper
# The order by key.
setting :order_by_key, :default => '_order_by', :doc => 'The order key'
# The order by key.
setting :order_direction_key, :default => '_order_direction', :doc => 'The order direction key'
# [+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
def table(options)
str = ''
str << table_rows(options)
str << '
'
end
alias_method :build_table, :table
# [+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
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 << ''
for row in body
str << ''
for value in row
str << %|#{process(value)} | |
end
str << '
'
end
str << ''
end
else
for row in items
str << ''
for value in row
str << %|#{process(value)} | |
end
str << '
'
end
end
return str
end
private
# [+options+]
# A hash of options.
#
# :id = id 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
def table_header(options)
str = ''
str << '' if create_tbody?(options) || options[:footers]
str << ''
options[:headers].each_with_index do |header, index|
header = process(header)
if (options[:order] && options[:order][:values] &&
options[:order][:values][index])
order_by = options[:order][:values][index]
asc_val = if options[:order][:asc_pic]
%||
else
'^'
end
desc_val = if options[:order][:desc_pic]
%||
else
'v'
end
order_asc = "#{asc_val}"
order_desc = "#{desc_val}"
str << ''
if options[:order][:left] || !options[:order][:right]
str << "#{order_asc} #{order_desc} | "
end
str << %|#{header} | |
if options[:order][:right]
str << "#{order_asc} #{order_desc} | "
end
str << '
| '
else
str << %|#{header} | |
end
end
str << '
'
str << '' if create_tbody?(options) || options[:footers]
return str
end
# [+options+]
# A hash of options.
#
# :id = id 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
def table_footer(options)
str = ''
for footer in options[:footers]
str << %|#{footer} | |
end
str << '
'
return str
end
# Generate the target URI.
def target_uri(order_by, direction)
params = { TableHelper.order_by_key => order_by,
TableHelper.order_direction_key => direction }
return Glue::UriUtils.update_query_string(request.uri.to_s, params)
end
def create_tbody?(options)
options[:values][0].respond_to?(:to_ary) && options[:values][0][0].respond_to?(:to_ary)
end
# A simple hack to allow Localization-processing in the table-helper
# overwrite this method to make this happen
# Now you can just pass [[somestring]] to the tablehelper and it will
# be localized like the rest of the page.
# ATM it's only kicking in when the @lc is set.
#
# Example for the method:
# module Nitro
# module TableHelper
# def process(value)
# val = value.to_s.gsub(/\[\[(.*?)\]\]/){ @lc[$1] }
# return val == "" ? value : val
# end
# end
# end
def process(value)
if @lc
val = value.to_s.gsub(/\[\[(.*?)\]\]/){ @lc[$1] }
return val == "" ? value : val
end
return value
end
end
end
# * George Moschovitis
# * Kashia Buch