Skip to content

Commit

Permalink
Merge pull request urvin-compliance#115 from martinsp/column-widths
Browse files Browse the repository at this point in the history
  • Loading branch information
goulvench committed Mar 18, 2020
2 parents 2401f49 + 6a6260c commit 438adc8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
7 changes: 7 additions & 0 deletions lib/caracal/core/models/table_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class TableModel < BaseModel
attr_reader :table_border_right # returns border model
attr_reader :table_border_horizontal # returns border model
attr_reader :table_border_vertical # returns border model
attr_reader :table_column_widths

# initialization
def initialize(options={}, &block)
Expand Down Expand Up @@ -148,6 +149,11 @@ def cell_style(models, options={})
instance_variable_set("@table_#{ m }", value.to_s.to_sym)
end
end

# column widths
def column_widths(value)
@table_column_widths = value.map(&:to_i) if value.is_a?(Array)
end

# .data
def data(value)
Expand Down Expand Up @@ -196,6 +202,7 @@ def option_keys
k << [:data, :align, :width]
k << [:border_color, :border_line, :border_size, :border_spacing]
k << [:border_bottom, :border_left, :border_right, :border_top, :border_horizontal, :border_vertical]
k << [:column_widths]
k.flatten
end

Expand Down
18 changes: 10 additions & 8 deletions lib/caracal/renderers/document_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,19 @@ def render_table(xml, model)
xml['w'].tblLook({ 'w:val' => '0600' })
end
xml['w'].tblGrid do
model.rows.first.each do |tc|
(tc.cell_colspan || 1).times do
xml['w'].gridCol({ 'w:w' => tc.cell_width })
end
column_widths = model.table_column_widths
column_widths ||= model.rows.first.map do |tc|
[tc.cell_width] * (tc.cell_colspan || 1)
end.flatten

column_widths.each do |width|
xml['w'].gridCol({ 'w:w' => width })
end

xml['w'].tblGridChange({ 'w:id' => '0' }) do
xml['w'].tblGrid do
model.rows.first.each do |tc|
(tc.cell_colspan || 1).times do
xml['w'].gridCol({ 'w:w' => tc.cell_width })
end
column_widths.each do |width|
xml['w'].gridCol({ 'w:w' => width })
end
end
end
Expand Down

0 comments on commit 438adc8

Please sign in to comment.