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 << %|#{value}| end str << '' end str << '' end else for row in items str << '' for value in row str << %|#{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| if (options[:order] && options[:order][:values] && options[:order][:values][index]) order_by = options[:order][:values][index] asc_val = if options[:order][:asc_pic] %|asc| else '^' end desc_val = if options[:order][:desc_pic] %|desc| else 'v' end order_asc = "#{asc_val}" order_desc = "#{desc_val}" str << '' if options[:order][:left] || !options[:order][:right] str << "" end str << %|| if options[:order][:right] str << "" end str << '
#{order_asc}
#{order_desc}
#{header}#{order_asc}
#{order_desc}
' 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][0].respond_to?(:to_ary) end end end # * George Moschovitis # * Kashia Buch