Changeset 973

Show
Ignore:
Timestamp:
05/12/07 21:16:05 (2 years ago)
Author:
mikem836
Message:

html :justified style for grouping

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ruport/trunk/lib/ruport/formatter/html.rb

    r873 r973  
    2222  # <tt>:show_group_headers</tt>  True by default    
    2323  # 
     24  # <tt>:style</tt> Used for grouping (:inline, :justified) 
     25  # 
    2426  class Formatter::HTML < Formatter     
    2527     
     
    2729                             Renderer::Group, Renderer::Grouping ] 
    2830 
    29     opt_reader :show_table_headers, :show_group_headers 
     31    opt_reader :show_table_headers, :show_group_headers, :style 
    3032     
    3133    # Generates table headers based on the column names of your Data::Table.   
     
    8183    # 
    8284    def build_grouping_body 
    83       render_inline_grouping(options) 
     85      case style 
     86      when :inline 
     87        render_inline_grouping(options) 
     88      when :justified 
     89        render_justified_grouping 
     90      end 
    8491    end 
    8592 
     
    107114      raise RuntimeError, "You need RedCloth!\n gem install RedCloth -v 3.0.3" 
    108115    end 
     116     
     117    private 
     118     
     119    def render_justified_grouping 
     120      output << "\t<table>\n\t\t<tr>\n\t\t\t<th>" + 
     121        "#{data.grouped_by}</th>\n\t\t\t<th>" + 
     122        grouping_columns.join("</th>\n\t\t\t<th>") +  
     123        "</th>\n\t\t</tr>\n" 
     124      data.each do |name, group|                      
     125        group.each_with_index do |row, i| 
     126          output << "\t\t<tr>\n\t\t\t" 
     127          if i == 0 
     128            output << "<td class=\"groupName\">#{name}</td>\n\t\t\t<td>" 
     129          else 
     130            output << "<td>&nbsp;</td>\n\t\t\t<td>" 
     131          end 
     132          output << row.to_a.join("</td>\n\t\t\t<td>") + 
     133            "</td>\n\t\t</tr>\n" 
     134        end 
     135      end 
     136      output << "\t</table>" 
     137    end 
     138     
     139    def grouping_columns 
     140      data.data.to_a[0][1].column_names 
     141    end 
    109142 
    110143  end 
  • ruport/trunk/test/html_formatter_test.rb

    r894 r973  
    102102                 "\t\t\t<td>9</td>\n\t\t</tr>\n\t</table>\n", actual 
    103103  end 
     104 
     105  def test_render_justified_html_grouping 
     106    table = Table(%w[a b c]) << [1,2,3] << [1,1,3] << [2,7,9] 
     107    g = Grouping(table,:by => "a") 
     108    actual = Ruport::Renderer::Grouping.render(:html, :data => g, 
     109                                               :style => :justified) 
     110 
     111    assert_equal "\t<table>\n\t\t<tr>\n\t\t\t<th>a</th>\n\t\t\t<th>b</th>\n"+ 
     112                 "\t\t\t<th>c</th>\n\t\t</tr>\n\t\t<tr>\n\t\t\t"+ 
     113                 "<td class=\"groupName\">1</td>\n\t\t\t<td>"+ 
     114                 "2</td>\n\t\t\t<td>3</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t"+ 
     115                 "<td>&nbsp;</td>\n\t\t\t<td>1</td>\n\t\t\t<td>3</td>"+ 
     116                 "\n\t\t</tr>\n\t\t<tr>\n\t\t\t"+ 
     117                 "<td class=\"groupName\">2</td>\n\t\t\t<td>7</td>\n"+ 
     118                 "\t\t\t<td>9</td>\n\t\t</tr>\n\t</table>", actual 
     119  end 
    104120end   
    105121