data-table 2.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/data-table.rb +2 -2
- data/lib/data-table/table.rb +14 -3
- data/lib/data-table/version.rb +1 -1
- data/spec/table_spec.rb +42 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a93c19ba22c5c189c7eb7c2523776ce2b97f3c62
|
4
|
+
data.tar.gz: d31df8fbf78d197cd174a48ff1aff84ed1f3d465
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e58f9dd66a62c8b84d5ca9155775d887499b60526846652def4dbd277da321b5fecb19602c4a2382f296192823b7049623a32712703d8e08a0ab3d0fdaa7083a
|
7
|
+
data.tar.gz: ce764c8ec13fcd513a7e7c21148da825128b7f4802f8873749acb5216c6c0362d8d73d8fd3f9bf9cef84519b6018722d6cf767ce2a01e99d71f58e07e0428be3
|
data/lib/data-table.rb
CHANGED
@@ -32,8 +32,8 @@ module DataTable
|
|
32
32
|
|
33
33
|
.data_table .group_header th {text-align: left;}
|
34
34
|
|
35
|
-
.data_table .subtotal.
|
36
|
-
.data_table .parent_subtotal.
|
35
|
+
.data_table .subtotal.first td,
|
36
|
+
.data_table .parent_subtotal.first td
|
37
37
|
{
|
38
38
|
border-top: 1px solid #000;
|
39
39
|
}
|
data/lib/data-table/table.rb
CHANGED
@@ -284,13 +284,20 @@ module DataTable
|
|
284
284
|
html = ''
|
285
285
|
path = ancestors.nil? ? [] : ancestors.dup
|
286
286
|
path << group_header
|
287
|
+
|
288
|
+
is_first_subtotal = true
|
289
|
+
|
287
290
|
@subtotal_calculations[path].each_with_index do |group, index|
|
288
|
-
|
291
|
+
next if group.empty?
|
292
|
+
|
293
|
+
html << "<tr class='subtotal index_#{index} #{'first' if is_first_subtotal}'>"
|
289
294
|
@columns.each do |col|
|
290
295
|
value = group[col.name] ? group[col.name].values[0] : nil
|
291
296
|
html << col.render_cell(value)
|
292
297
|
end
|
293
298
|
html << '</tr>'
|
299
|
+
|
300
|
+
is_first_subtotal = false
|
294
301
|
end
|
295
302
|
html
|
296
303
|
end
|
@@ -339,7 +346,9 @@ module DataTable
|
|
339
346
|
@collection.each_pair_with_parents(@groupings.count) do |group_name, group_data, parents|
|
340
347
|
path = parents + [group_name]
|
341
348
|
result = calculate(group_data, subtotal[0], subtotal[1], path)
|
342
|
-
|
349
|
+
(0..index).each do |index|
|
350
|
+
@subtotal_calculations[path][index] ||= {}
|
351
|
+
end
|
343
352
|
@subtotal_calculations[path][index][subtotal[0]] = {subtotal[1] => result}
|
344
353
|
end
|
345
354
|
end
|
@@ -436,7 +445,9 @@ module DataTable
|
|
436
445
|
def total_row(collection, column_name, function = nil, index = nil, &block)
|
437
446
|
function_or_block = function || block
|
438
447
|
f = function && block_given? ? [function, block] : function_or_block
|
439
|
-
|
448
|
+
(0..index).each do |index|
|
449
|
+
collection[index] = {} if collection[index].nil?
|
450
|
+
end
|
440
451
|
collection[index][column_name] = f
|
441
452
|
end
|
442
453
|
end
|
data/lib/data-table/version.rb
CHANGED
data/spec/table_spec.rb
CHANGED
@@ -72,6 +72,48 @@ describe DataTable::Table do
|
|
72
72
|
expect(data_table.subtotal_calculations).to eq({["Star Wars"]=>[{:power_level=>{:sum=>145.0}}], ["Middle Earth"]=>[{:power_level=>{:sum=>9081.0}}]})
|
73
73
|
end
|
74
74
|
|
75
|
+
it "should do sub-totaling starting with indexes > 0" do
|
76
|
+
data_table.group_by :world, level: 0
|
77
|
+
data_table.column :power_level
|
78
|
+
data_table.subtotal :power_level, :sum, 1
|
79
|
+
|
80
|
+
data_table.prepare_data
|
81
|
+
|
82
|
+
expect(data_table.subtotal_calculations).to eq({
|
83
|
+
["Star Wars"] => [{}, {:power_level => {:sum => 145.0}}],
|
84
|
+
["Middle Earth"] => [{}, {:power_level => {:sum => 9081.0}}]
|
85
|
+
})
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should not render empty sub-total aggregate rows" do
|
89
|
+
data_table.group_by :world, level: 0
|
90
|
+
data_table.column :power_level
|
91
|
+
data_table.subtotal :power_level, nil, 1 do |_records, _column, path|
|
92
|
+
path
|
93
|
+
end
|
94
|
+
|
95
|
+
data_table.prepare_data
|
96
|
+
subtotal_calculations = data_table.subtotal_calculations
|
97
|
+
|
98
|
+
# this is convoluted because it's hard to assert a nested structure that includes procs
|
99
|
+
# [
|
100
|
+
# ["Middle Earth"] => [{}, {:power_level=>{#<Proc:0x03dbead8@table_spec.rb:78>=>"Middle Earth"}}],
|
101
|
+
# ["Star Wars"] => [{}, {:power_level=>{#<Proc:0x03dbead8@table_spec.rb:78>=>"Star Wars"}}]
|
102
|
+
# ]
|
103
|
+
expect(subtotal_calculations.keys).to eq([["Star Wars"], ["Middle Earth"]])
|
104
|
+
expect(subtotal_calculations.values.flatten.map(&:keys)).to eq([[], [:power_level], [], [:power_level]])
|
105
|
+
subtotal_calculations.values.flatten.map(&:values).flatten.map(&:keys).each do |k|
|
106
|
+
expect(k).to be_a(Array)
|
107
|
+
expect(k.length).to eq(1)
|
108
|
+
expect(k[0]).to be_a(Proc)
|
109
|
+
end
|
110
|
+
expect(subtotal_calculations.values.flatten.map(&:values).flatten.map(&:values)).to eq([["Star Wars"], ["Middle Earth"]])
|
111
|
+
|
112
|
+
# note how the rows are index_1, and there is no index_0 row
|
113
|
+
expect(data_table.render).to \
|
114
|
+
eq(%{<table id='' class='data_table ' cellspacing='0' cellpadding='0'><caption></caption><thead><tr><th class='power_level ' ></th></tr></thead><tbody class='star_wars'><tr class='group_header level_0'><th colspan='1'>Star Wars</th></tr><tr class='row_0 ' ><td class='power_level numeric' >50</td></tr><tr class='row_1 alt ' ><td class='power_level numeric' >95</td></tr><tr class='subtotal index_1 first'><td class='power_level numeric' >Star Wars</td></tr></tbody><tbody class='middle_earth'><tr class='group_header level_0'><th colspan='1'>Middle Earth</th></tr><tr class='row_0 ' ><td class='power_level numeric' >9001</td></tr><tr class='row_1 alt ' ><td class='power_level numeric' >80</td></tr><tr class='subtotal index_1 first'><td class='power_level numeric' >Middle Earth</td></tr></tbody></table>})
|
115
|
+
end
|
116
|
+
|
75
117
|
it "should render a custom header" do
|
76
118
|
data_table.custom_header do
|
77
119
|
th 'Two Columns', :colspan => 2
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data-table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Erickson
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2018-11-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
118
|
version: '0'
|
119
119
|
requirements: []
|
120
120
|
rubyforge_project: data-table
|
121
|
-
rubygems_version: 2.6.
|
121
|
+
rubygems_version: 2.6.14
|
122
122
|
signing_key:
|
123
123
|
specification_version: 4
|
124
124
|
summary: Turn arrays of hashes or models in to an HTML table.
|