From 237e4d3c11ed2f9ef5885db20a1b1d2ee5aaa1e1 Mon Sep 17 00:00:00 2001 From: "El Skwid (Don Morrison)" Date: Fri, 22 May 2009 16:00:13 -0700 Subject: [PATCH] Add column formatters to html report and fixup when col_data doesn't respond to @data --- lib/munger/render/html.rb | 31 +++++++++++++++++++++++++----- lib/munger/render/sortable_html.rb | 7 ++++--- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/munger/render/html.rb b/lib/munger/render/html.rb index 65c9f28..edb3f6f 100644 --- a/lib/munger/render/html.rb +++ b/lib/munger/render/html.rb @@ -5,8 +5,8 @@ require 'builder' end -module Munger #:nodoc: - module Render #:nodoc: +module Munger + module Render class Html attr_reader :report, :classes @@ -50,7 +50,7 @@ def render x.tr(row_attrib) do if row[:meta][:group_header] - header = row[:meta][:group_value].to_s + header = @report.column_title(row[:meta][:group_name]) + ' : ' + row[:meta][:group_value].to_s x.th(:colspan => @report.columns.size) { x << header } else @report.columns.each do |column| @@ -63,7 +63,28 @@ def render end end - x.td(cell_attrib) { x << row[:data][column].to_s } + # x.td(cell_attrib) { x << row[:data][column].to_s } + # TODO: Clean this up, I don't like it but it's working + # output the cell + # x.td(cell_attrib) { x << row[:data][column].to_s } + x.td(cell_attrib) do + formatter,*args = *@report.column_formatter(column) + col_data = row[:data] #[column] + if formatter && col_data[column] + formatted = if formatter.class == Proc + data = col_data.respond_to?(:data) ? col_data.data : col_data + formatter.call(data) + elsif col_data[column].respond_to? formatter + col_data[column].send(formatter, *args) + elsif + col_data[column].to_s + end + else + formatted = col_data[column].to_s + end + x << formatted.to_s + end + end end end @@ -86,4 +107,4 @@ def valid? end end -end +end \ No newline at end of file diff --git a/lib/munger/render/sortable_html.rb b/lib/munger/render/sortable_html.rb index 36babbd..085c042 100644 --- a/lib/munger/render/sortable_html.rb +++ b/lib/munger/render/sortable_html.rb @@ -1,6 +1,6 @@ require 'builder' -module Munger #:nodoc: - module Render #:nodoc: +module Munger + module Render # Render a table that lets the user sort the columns class SortableHtml @@ -87,7 +87,8 @@ def render col_data = row[:data] #[column] if formatter && col_data[column] formatted = if formatter.class == Proc - formatter.call(col_data.data) + data = col_data.respond_to?(:data) ? col_data.data : col_data + formatter.call(data) elsif col_data[column].respond_to? formatter col_data[column].send(formatter, *args) elsif