Skip to content

Commit

Permalink
Merge pull request urvin-compliance#105 from AliceVerner:repeat_headers
Browse files Browse the repository at this point in the history
Add repeat table headers

# Conflicts:
#	lib/caracal/core/models/table_model.rb
#	lib/caracal/renderers/document_renderer.rb
  • Loading branch information
goulvench committed Mar 18, 2020
2 parents c76b895 + 32a45ab commit f3029a5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ docx.table [['Header 1','Header 2'],['Cell 1', 'Cell 2']] do
border_size 4 # sets the border width. defaults to 0. units in twips.
border_spacing 4 # sets the spacing around the border. defaults to 0. units in twips.
column_widths [80, 120] # sets the column widths. defaults to auto. units in twips.
repeat_header 1 # sets the number of header rows that is repeated on each page. defaults to 0.
end
```

Expand Down
16 changes: 3 additions & 13 deletions lib/caracal/core/models/table_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class TableModel < BaseModel
const_set(:DEFAULT_TABLE_BORDER_LINE, :single)
const_set(:DEFAULT_TABLE_BORDER_SIZE, 0) # units in 1/8 points
const_set(:DEFAULT_TABLE_BORDER_SPACING, 0)
const_set(:DEFAULT_TABLE_REPEAT_HEADER, false)
const_set(:DEFAULT_TABLE_HEADER_ROWS, 1)
const_set(:DEFAULT_TABLE_REPEAT_HEADER, 0)

# accessors
attr_reader :table_align
Expand All @@ -40,7 +39,6 @@ class TableModel < BaseModel
attr_reader :table_border_vertical # returns border model
attr_reader :table_column_widths
attr_reader :table_repeat_header
attr_reader :table_header_rows

# initialization
def initialize(options={}, &block)
Expand All @@ -50,7 +48,6 @@ def initialize(options={}, &block)
@table_border_size = DEFAULT_TABLE_BORDER_SIZE
@table_border_spacing = DEFAULT_TABLE_BORDER_SPACING
@table_repeat_header = DEFAULT_TABLE_REPEAT_HEADER
@table_header_rows = DEFAULT_TABLE_HEADER_ROWS

super options, &block
end
Expand Down Expand Up @@ -126,16 +123,9 @@ def cell_style(models, options={})


#=============== SETTERS ==============================

# booleans
[:repeat_header].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@table_#{ m }", !!value)
end
end

# integers
[:border_size, :border_spacing, :width, :header_rows].each do |m|
[:border_size, :border_spacing, :width, :repeat_header].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@table_#{ m }", value.to_i)
end
Expand Down Expand Up @@ -216,7 +206,7 @@ def option_keys
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 << [:repeat_header, :header_rows]
k << [:repeat_header]
k.flatten
end

Expand Down
6 changes: 2 additions & 4 deletions lib/caracal/renderers/document_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,8 @@ def render_table(xml, model)
rowspan_hash = {}
model.rows.each_with_index do |row, index|
xml['w'].tr do
tc_index = 0
row.each do |tc|
if model.table_repeat_header
if index < model.table_header_rows
if model.table_repeat_header > 0
if index < model.table_repeat_header
xml['w'].trPr do
xml['w'].tblHeader
end
Expand Down
12 changes: 11 additions & 1 deletion spec/lib/caracal/core/models/table_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
it { expect(described_class::DEFAULT_TABLE_BORDER_LINE).to eq :single }
it { expect(described_class::DEFAULT_TABLE_BORDER_SIZE).to eq 0 }
it { expect(described_class::DEFAULT_TABLE_BORDER_SPACING).to eq 0 }
it { expect(described_class::DEFAULT_TABLE_REPEAT_HEADER).to eq 0 }
end

# accessors
Expand All @@ -36,6 +37,7 @@
it { expect(subject.table_border_line).to eq :double }
it { expect(subject.table_border_size).to eq 8 }
it { expect(subject.table_border_spacing).to eq 4 }
it { expect(subject.table_repeat_header).to eq 0 }
end

end
Expand Down Expand Up @@ -179,6 +181,13 @@
it { expect(subject.table_width).to eq 7500 }
end

# .repeat_header
describe '.repeat_header' do
before { subject.repeat_header(2) }

it { expect(subject.table_repeat_header).to eq 2 }
end




Expand Down Expand Up @@ -212,7 +221,8 @@
let(:expected1) { [:data, :align, :width] }
let(:expected2) { [:border_color, :border_line, :border_size, :border_spacing] }
let(:expected3) { [:border_top, :border_bottom, :border_left, :border_right, :border_horizontal, :border_vertical] }
let(:expected) { (expected1 + expected2 + expected3).sort }
let(:expected4) { [:repeat_header] }
let(:expected) { (expected1 + expected2 + expected3 + expected4).sort }

it { expect(actual).to eq expected }
end
Expand Down

0 comments on commit f3029a5

Please sign in to comment.