public
Description: Fast, Nimble PDF Writer for Ruby
Homepage: http://prawn.majesticseacreature.com
Clone URL: git://github.com/sandal/prawn.git
[#105 state:resolved] Added specs for row background color and fixed the code 
such that the header color is properly set (it was being included in the row 
background coloring iteration.)
Gavin Stark (author)
Thu Nov 06 22:33:18 -0800 2008
commit  22697cabb6c729dcf2e3f3b0fb149eb1b7d6817a
tree    5b10bdea0339d6cf5adaed5fa1a9d2316c0c0960
parent  89989e0dfd9f48783ddd16f6a0f777064aaf9112
...
275
276
277
278
 
279
280
 
 
281
282
 
283
284
285
...
275
276
277
 
278
279
 
280
281
282
 
283
284
285
286
0
@@ -275,11 +275,12 @@ module Prawn
0
           contents.last.border_style  = :no_top
0
         end
0
         
0
-        contents.first.background_color = C(:header_color) if C(:header_color)
0
+        contents.first.background_color = C(:header_color) if C(:header_color) && C(:headers)
0
 
0
-        contents.each do |x|
0
+        rows_to_skip_coloring = C(:headers) ? 1 : 0
0
+        contents.each_with_index do |x,index|
0
           unless x.background_color 
0
-            x.background_color = next_row_color if C(:row_colors) 
0
+            x.background_color = next_row_color if C(:row_colors)  && index >= rows_to_skip_coloring
0
           end
0
           x.draw 
0
         end
...
73
74
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
77
78
...
109
110
111
112
 
113
114
115
...
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
...
202
203
204
 
205
206
207
208
0
@@ -73,6 +73,99 @@ describe "A table's height" do
0
   
0
 end
0
 
0
+describe "Table background colors" do
0
+  setup do
0
+    @default_row_count = 6
0
+    @data              = [["foo","bar"]] * @default_row_count
0
+    @headers           = ["baz","foobar"]
0
+  end
0
+
0
+  def expect_background_color( color )
0
+    Prawn::Graphics::CellBlock.any_instance.expects(:background_color=).with(color)
0
+  end
0
+
0
+  it "should correctly cycle row colors for PDF::Writer rows and an uncolored header" do
0
+    row_colors     = :pdf_writer
0
+    odd_row_color  = "cccccc"   # These colors are copied from table.rb  Perhaps they
0
+    even_row_color = "ffffff"   # should be constants in Prawn::Document or elsewhere?
0
+
0
+    (@default_row_count/2).times do
0
+      expect_background_color( odd_row_color )
0
+      expect_background_color( even_row_color )
0
+    end
0
+
0
+    Prawn::Document.new.table(@data, :headers => @headers, :row_colors => row_colors )
0
+  end
0
+
0
+  it "should correctly cycle row colors for a custom row color set and an uncolored header" do
0
+    odd_row_color  = "CC0000"
0
+    even_row_color = "0000BB"
0
+    row_colors     = [ odd_row_color, even_row_color ]
0
+
0
+    (@default_row_count/2).times do
0
+      expect_background_color( odd_row_color )
0
+      expect_background_color( even_row_color )
0
+    end
0
+
0
+    Prawn::Document.new.table(@data, :headers => @headers, :row_colors => row_colors )
0
+  end
0
+
0
+  it "should correctly cycle row colors and apply a custom header color when specified" do
0
+    header_row_color = "00DD00"
0
+    odd_row_color    = "CC0000"
0
+    even_row_color   = "0000BB"
0
+    row_colors       = [ odd_row_color, even_row_color ]
0
+
0
+    expect_background_color( header_row_color )
0
+    (@default_row_count/2).times do
0
+      expect_background_color( odd_row_color )
0
+      expect_background_color( even_row_color )
0
+    end
0
+
0
+    Prawn::Document.new.table(@data, :headers => @headers, :header_color => header_row_color, :row_colors => row_colors )
0
+  end
0
+
0
+  it "should correctly cycle row colors even if a header color is specified for a headerless table" do
0
+    header_row_color = "00DD00"
0
+    odd_row_color    = "CC0000"
0
+    even_row_color   = "0000BB"
0
+    row_colors       = [ odd_row_color, even_row_color ]
0
+
0
+    (@default_row_count/2).times do
0
+      expect_background_color( odd_row_color )
0
+      expect_background_color( even_row_color )
0
+    end
0
+
0
+    Prawn::Document.new.table(@data, :header_color => @header_row_color, :row_colors => row_colors )
0
+  end
0
+
0
+  it "should correctly cycle row colors even when only one row color is specified" do
0
+    row_color   = "0000BB"
0
+    row_colors  = [ row_color ]
0
+
0
+    @default_row_count.times do
0
+      expect_background_color( row_color )
0
+    end
0
+
0
+    Prawn::Document.new.table(@data, :row_colors => row_colors )
0
+  end
0
+
0
+  it "should correctly cycle row colors even when more than two row colors are specified" do
0
+    row_color_1   = "0000BB"
0
+    row_color_2   = "00CC00"
0
+    row_color_3   = "DD0000"
0
+    row_colors  = [ row_color_1, row_color_2, row_color_3 ]
0
+
0
+    (@default_row_count/3).times do
0
+      expect_background_color( row_color_1 )
0
+      expect_background_color( row_color_2 )
0
+      expect_background_color( row_color_3 )
0
+    end
0
+
0
+    Prawn::Document.new.table(@data, :row_colors => row_colors )
0
+  end
0
+end
0
+
0
 describe "A table's content" do
0
 
0
   it "should output content cell by cell, row by row" do
0
@@ -109,7 +202,7 @@ describe "A table's content" do
0
       @pdf.table(data)
0
     }.should.not.raise
0
   end   
0
-  
0
+
0
   it "should paginate for large tables" do
0
     # 30 rows fit on the table with default setting, 31 exceed.
0
     data = [["foo"]] * 31

Comments