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

Adds .cant_split and .cant_split? table methods. #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,23 @@ end

*Note: content of cells 21 and 24 will disappear*

Row can be marked as can't split rows that re-position themselves at the top of the next if they would have otherwise split across a page break.

```
docx.table [['11', '22'], ['14', '25'], ['64', '23']] do
cant_split rows[1]
end
```

or

```
docx.table [['11', '22'], ['14', '25'], ['64', '23']] do
cant_split rows[1...-1]
end
```


### Table Cells

If your table contains more complex data (multiple paragraphs, images, lists, etc.), you will probably want to instantiate your `TableCellModel` instances directly. With the exception of page breaks, table cells can contain anything the document can contain, including another table.
Expand Down
29 changes: 28 additions & 1 deletion lib/caracal/core/models/table_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +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_CANT_SPLIT_ROWS, {})

# accessors
attr_reader :table_align
Expand All @@ -44,6 +45,7 @@ def initialize(options={}, &block)
@table_border_line = DEFAULT_TABLE_BORDER_LINE
@table_border_size = DEFAULT_TABLE_BORDER_SIZE
@table_border_spacing = DEFAULT_TABLE_BORDER_SPACING
@table_rows_cant_split = {}

super options, &block
end
Expand Down Expand Up @@ -99,8 +101,33 @@ def cell_style(models, options={})
m.apply_styles(options)
end
end

# This method allows a user to specify one or more
# rows that should be rendered with the
# <w:cant_split /> property
#
# For example
#
# docx.table data do |t|
# t.cant_split r.rows[0...-1]
# end
#
# would cause all rows in the table to be rendered
# with the <w:cant_split /> property.
def cant_split(models, options={})
models = rows.include?(models) ? [models] : models
models.each do |r|
@table_rows_cant_split[rows.index(r)] = true
end
end


# This method returns true or false depending on
# if a row has been marked as a can't split row
# via the cant_split method.
def cant_split?(row_index)
!!@table_rows_cant_split[row_index]
end

#=============== GETTERS ==============================

# border attrs
Expand Down
9 changes: 8 additions & 1 deletion lib/caracal/renderers/document_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,9 @@ def render_table(xml, model)
end

rowspan_hash = {}
model.rows.each do |row|
model.rows.each.with_index do |row, row_index|
xml['w'].tr do
render_table_row_properties(xml, model, row_index)
tc_index = 0
row.each do |tc|
xml['w'].tc do
Expand Down Expand Up @@ -390,6 +391,12 @@ def render_table(xml, model)
end
end

def render_table_row_properties(xml, model, index)
xml['w'].trPr do
xml['w'].cantSplit if model.cant_split?(index)
end
end


#============= OPTIONS ===================================

Expand Down
20 changes: 19 additions & 1 deletion spec/lib/caracal/core/models/table_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@
describe '.cells' do
it { expect(subject.cells[0]).to eq 'top left' }
end

# .cant_split and cant_split?
describe '.cant_split and .cant_split?' do
it { expect(subject.cant_split?(0)).to eq(false) }

describe "when first row can't split" do
before { subject.cant_split subject.rows[0] }

it { expect(subject.cant_split?(0)).to eq(true) }
end

describe 'when no rows can be split' do
before { subject.cant_split subject.rows }

it { expect(subject.cant_split?(0)).to eq(true) }
it { expect(subject.cant_split?(1)).to eq(true) }
end
end
end


Expand Down Expand Up @@ -219,4 +237,4 @@

end

end
end
2 changes: 1 addition & 1 deletion spec/lib/caracal/core/tables_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

end

end
end