Skip to content

Commit

Permalink
Fix unhandled exception when starting subtotals with an index > 0
Browse files Browse the repository at this point in the history
- the `collection` is empty and you set `collection[1] = {}`, then `collection[0]` will be `nil` and that'll cause things to blow up
aharpervc authored and BrianBorge committed Nov 12, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 6510837 commit 85ff6fe
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/data-table/table.rb
Original file line number Diff line number Diff line change
@@ -339,7 +339,9 @@ def calculate_subtotals!
@collection.each_pair_with_parents(@groupings.count) do |group_name, group_data, parents|
path = parents + [group_name]
result = calculate(group_data, subtotal[0], subtotal[1], path)
@subtotal_calculations[path][index] ||= {}
(0..index).each do |index|
@subtotal_calculations[path][index] ||= {}
end
@subtotal_calculations[path][index][subtotal[0]] = {subtotal[1] => result}
end
end
@@ -436,7 +438,9 @@ def calculate_min(collection, column_name)
def total_row(collection, column_name, function = nil, index = nil, &block)
function_or_block = function || block
f = function && block_given? ? [function, block] : function_or_block
collection[index] = {} if collection[index].nil?
(0..index).each do |index|
collection[index] = {} if collection[index].nil?
end
collection[index][column_name] = f
end
end
13 changes: 13 additions & 0 deletions spec/table_spec.rb
Original file line number Diff line number Diff line change
@@ -72,6 +72,19 @@
expect(data_table.subtotal_calculations).to eq({["Star Wars"]=>[{:power_level=>{:sum=>145.0}}], ["Middle Earth"]=>[{:power_level=>{:sum=>9081.0}}]})
end

it "should do sub-totaling starting with indexes > 0" do
data_table.group_by :world, level: 0
data_table.column :power_level
data_table.subtotal :power_level, :sum, 1

data_table.prepare_data

expect(data_table.subtotal_calculations).to eq({
["Star Wars"] => [{}, {:power_level => {:sum => 145.0}}],
["Middle Earth"] => [{}, {:power_level => {:sum => 9081.0}}]
})
end

it "should render a custom header" do
data_table.custom_header do
th 'Two Columns', :colspan => 2

0 comments on commit 85ff6fe

Please sign in to comment.