Skip to content

Commit

Permalink
Keep more specific cell styling instead of it being overwritten
Browse files Browse the repository at this point in the history
  by the table cell_style
  #56
  • Loading branch information
russellsanders committed Nov 25, 2015
1 parent 810ccda commit 60084d8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
24 changes: 14 additions & 10 deletions lib/prawn/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ module Errors
# end
#
class Table
module Interface
module Interface
# @group Experimental API

# Set up and draw a table on this document. A block can be given, which will
Expand Down Expand Up @@ -136,7 +136,7 @@ def make_table(data, options={}, &block)
#
def initialize(data, document, options={}, &block)
@pdf = document
@cells = make_cells(data)
@cells = make_cells(data, options.delete(:cell_style) || {})
@header = false
options.each { |k, v| send("#{k}=", v) }

Expand Down Expand Up @@ -291,7 +291,7 @@ def draw
cells_this_page = []

@cells.each do |cell|
if start_new_page?(cell, offset, ref_bounds)
if start_new_page?(cell, offset, ref_bounds)
# draw cells on the current page and then start a new one
# this will also add a header to the new page if a header is set
# reset array of cells for the new page
Expand Down Expand Up @@ -381,7 +381,7 @@ def row_heights
end

protected

# sets the background color (if necessary) for the given cell
def set_background_color(cell, started_new_page_at_row)
if defined?(@row_colors) && @row_colors && (!@header || cell.row > 0)
Expand Down Expand Up @@ -429,9 +429,9 @@ def ink_and_draw_cells(cells_this_page, draw_cells = true)
def ink_and_draw_cells_and_start_new_page(cells_this_page, cell)
# don't draw only a header
draw_cells = (@header_row.nil? || cells_this_page.size > @header_row.size)

ink_and_draw_cells(cells_this_page, draw_cells)

# start a new page or column
@pdf.bounds.move_past_bottom

Expand Down Expand Up @@ -509,7 +509,7 @@ def header_rows
# Prawn::Table::Cell, and sets up their in-table properties so that they
# know their own position in the table.
#
def make_cells(data)
def make_cells(data, cell_style = {})
assert_proper_table_data(data)

cells = Cells.new
Expand All @@ -523,7 +523,11 @@ def make_cells(data)
column_number += 1 until cells[row_number, column_number].nil?

# Build the cell and store it in the Cells collection.
cell = Cell.make(@pdf, cell_data)
cell = if cell_data.is_a?(Hash)
Cell.make(@pdf, cell_style.merge(cell_data))
else
Cell.make(@pdf, cell_data, cell_style)
end
cells[row_number, column_number] = cell

# Add dummy cells for the rest of the cells in the span group. This
Expand Down Expand Up @@ -576,7 +580,7 @@ def add_header(row_number, cells_this_page)
number_of_header_rows.times do |h|
additional_header_height = add_one_header_row(cells_this_page, x_offset, y_coord-header_height, row_number-1, h)
header_height += additional_header_height
end
end
end
header_height
end
Expand All @@ -593,7 +597,7 @@ def add_one_header_row(page_of_cells, x_offset, y, row, row_of_header=nil)
rows_to_operate_on = @header_row.rows(row_of_header) if row_of_header
rows_to_operate_on.each do |cell|
cell.row = row
cell.dummy_cells.each {|c|
cell.dummy_cells.each {|c|
if cell.rowspan > 1
# be sure to account for cells that span multiple rows
# in this case you need multiple row numbers
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/table/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def initialize(pdf, point, options={})
@rowspan = 1
@dummy_cells = []

options.each { |k, v| send("#{k}=", v) }
style(options)

@initializer_run = true
end
Expand Down
10 changes: 10 additions & 0 deletions spec/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1604,4 +1604,14 @@
pdf.render
pdf.page_count.should == 1
end

it 'illustrates issue #56 cell style should not be overwritten by table style', issue: 56 do
t = @pdf.table([['col1', 'col2'],
['val1', { content: 'val2', align: :left }]],
cell_style: { align: :center })
t.cells[0, 0].align.should == :center
t.cells[0, 1].align.should == :center
t.cells[1, 0].align.should == :center
t.cells[1, 1].align.should == :left
end
end

0 comments on commit 60084d8

Please sign in to comment.