Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes issue #407 #526

Merged
merged 4 commits into from
Aug 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions lib/prawn/table/cells.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def min_width
# Returns maximum width that can contain cells in the set.
#
def max_width
aggregate_cell_values(:column, :max_width_ignoring_span, :min)
aggregate_cell_values(:column, :max_width_ignoring_span, :max)
end

# Returns the total height of all rows in the selected set.
Expand Down Expand Up @@ -229,9 +229,41 @@ def index_cells
#
def aggregate_cell_values(row_or_column, meth, aggregate)
values = {}

#calculate values for all cells that do not span accross multiple cells
#this ensures that we don't have a problem if the first line includes
#a cell that spans across multiple cells
each do |cell|
index = cell.send(row_or_column)
values[index] = [values[index], cell.send(meth)].compact.send(aggregate)
if cell.colspan == 1
index = cell.send(row_or_column)
values[index] = [values[index], cell.send(meth)].compact.send(aggregate)
end
end

each do |cell|
index = cell.send(row_or_column)
if cell.colspan > 1
#calculate current (old) return value before we do anything
old_sum = 0
cell.colspan.times { |i|
old_sum += values[index+i] unless values[index+i].nil?
}

#calculate future return value
new_sum = cell.send(meth) * cell.colspan

if new_sum > old_sum
#not entirely sure why we need this line, but with it the tests pass
values[index] = [values[index], cell.send(meth)].compact.send(aggregate)
#overwrite the old values with the new ones, but only if an entry existed
cell.colspan.times { |i|
values[index+i] = cell.send(meth) if values[index+i]
}
end

else
values[index] = [values[index], cell.send(meth)].compact.send(aggregate)
end
end
values.values.inject(0, &:+)
end
Expand Down
39 changes: 38 additions & 1 deletion spec/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,43 @@
end
end

describe "You can explicitly set the column widths and use a colspan > 1" do
it "should work with a colspan > 1 with given column_widths (issue #407)" do
#normal entries in line 1
data = [
[ '','',''],
[ { :content => "", :colspan => 3 } ],
[ "", "", "" ],
]
pdf = Prawn::Document.new
table = Prawn::Table.new data, pdf, :column_widths => [100 , 200, 240]

#colspan entry in line 1
data = [
[ { :content => "", :colspan => 3 } ],
[ "", "", "" ],
]
pdf = Prawn::Document.new
table = Prawn::Table.new data, pdf, :column_widths => [100 , 200, 240]

#mixed entries in line 1
data = [
[ { :content => "", :colspan =>2 }, "" ],
[ "", "", "" ],
]
pdf = Prawn::Document.new
table = Prawn::Table.new data, pdf, :column_widths => [100 , 200, 240]

data = [['', '', {:content => '', :colspan => 2}, '',''],
['',{:content => '', :colspan => 5}]
]
pdf = Prawn::Document.new
table = Prawn::Table.new data, pdf, :column_widths => [50 , 100, 50, 50, 50, 50]

end

end

describe "#initialize" do
before(:each) do
@pdf = Prawn::Document.new
Expand Down Expand Up @@ -1176,7 +1213,7 @@
:colspan => 3}],
["A", "B", "C"]],
:width => 200)

puts "result: #{t.column_widths.inject(0) { |sum, w| sum + w }}"
t.column_widths.inject(0) { |sum, w| sum + w }.
should be_within(0.01).of(200)
end
Expand Down