data-table 1.0.0 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Guardfile +48 -0
- data/LICENSE +21 -0
- data/README.md +135 -0
- data/Rakefile +5 -0
- data/assigments_table.html +56 -0
- data/data-table.gemspec +17 -12
- data/examples/all_features.rb +161 -0
- data/lib/data-table.rb +62 -3
- data/lib/data-table/column.rb +75 -0
- data/lib/data-table/enum.rb +57 -0
- data/lib/data-table/table.rb +458 -0
- data/lib/data-table/version.rb +2 -2
- data/rebuild_gem.rb +11 -0
- data/spec/column_spec.rb +41 -0
- data/spec/data_table_spec.rb +22 -0
- data/spec/enum_spec.rb +36 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/table_spec.rb +281 -0
- metadata +89 -18
- data/lib/data-table/data_table.rb +0 -385
- data/lib/data-table/data_table_column.rb +0 -60
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c386a8363980289b78c55b567a8605680275a8f51444fa86be2da2872581a7dd
|
4
|
+
data.tar.gz: 9778187132a67c142491b5da51027b631cd906375e2827c7f82b61474be21e82
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e65192931fd42ebfabfa2cdf7cfd55309080e5fa4af9ffbb2ed9d2fbc99a24f837513b5e09ed9ea2c4f8dc1f9f76176118ad45b7c47838dc87ca9722507575f1
|
7
|
+
data.tar.gz: 55952adf452a7df4b7829ccd68c9e152cf644057dd588d7979ff284dc8c78a6cc99ad77e4bc5d53a093a139d0fbcb6eccdf9bfcae20ccb23b428df0f39a5ca03
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features) \
|
6
|
+
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
7
|
+
|
8
|
+
## Note: if you are using the `directories` clause above and you are not
|
9
|
+
## watching the project directory ('.'), then you will want to move
|
10
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
11
|
+
#
|
12
|
+
# $ mkdir config
|
13
|
+
# $ mv Guardfile config/
|
14
|
+
# $ ln -s config/Guardfile .
|
15
|
+
#
|
16
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
17
|
+
|
18
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
19
|
+
# rspec may be run, below are examples of the most common uses.
|
20
|
+
# * bundler: 'bundle exec rspec'
|
21
|
+
# * bundler binstubs: 'bin/rspec'
|
22
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
23
|
+
# installed the spring binstubs per the docs)
|
24
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
25
|
+
# * 'just' rspec: 'rspec'
|
26
|
+
|
27
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
28
|
+
require "guard/rspec/dsl"
|
29
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
30
|
+
|
31
|
+
# Feel free to open issues for suggestions and improvements
|
32
|
+
|
33
|
+
# RSpec files
|
34
|
+
rspec = dsl.rspec
|
35
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
36
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
37
|
+
watch(rspec.spec_files)
|
38
|
+
|
39
|
+
# Ruby files
|
40
|
+
ruby = dsl.ruby
|
41
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
42
|
+
|
43
|
+
# Turnip features and steps
|
44
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
45
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
46
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
|
47
|
+
end
|
48
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2020 Veracross
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
# DataTable
|
2
|
+
[![Build Status](https://secure.travis-ci.org/veracross/data-table.svg)](http://travis-ci.org/veracross/data-table)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/veracross/data-table.svg)](https://codeclimate.com/github/veracross/data-table)
|
4
|
+
|
5
|
+
DataTable renders collections (an array of hashes or ActiveRecord models) as HTML tables.
|
6
|
+
|
7
|
+
## Install
|
8
|
+
```ruby
|
9
|
+
gem install data-table
|
10
|
+
```
|
11
|
+
|
12
|
+
or, in your Gemfile
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'data-table'
|
16
|
+
```
|
17
|
+
|
18
|
+
### Basic Usage
|
19
|
+
|
20
|
+
the normal usage is to call the `DataTable.render()` method and pass it a collection. The method also takes a block which can be used to configure the table. The column method takes a symbol for the first parameter. If the symbol matches a key in the @collection, then that value is printed in the cell.
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
DataTable.render(@collection) do |t|
|
24
|
+
t.column :column_1, "Title"
|
25
|
+
t.column :column_2, "Title 2"
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
### Custom Cell Renderer
|
30
|
+
|
31
|
+
Sometimes you want to use Ruby code to customize the value for a cell. This can be done by passing a block to the .column method
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
DataTable.render(@collection) do |t|
|
35
|
+
t.column :column_id, "Title" do |value, row, row_index, column, column_index|
|
36
|
+
"The value is: #{value}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
You don't need to pass in all of the block parameters; just the ones up to the one you need.
|
42
|
+
|
43
|
+
**Tip** The column_id doesn't need to be an actual key in the collection. You can just make up an arbitrary column id and use the block renderer to customize the value for a column.
|
44
|
+
|
45
|
+
|
46
|
+
### All Table Configuration Options
|
47
|
+
|
48
|
+
id: the html id
|
49
|
+
title: the title of the data table
|
50
|
+
subtitle: the subtitle of the data table
|
51
|
+
css_class: an extra css class to get applied to the table
|
52
|
+
empty_text: the text to display of the collection is empty
|
53
|
+
display_header => false: hide the column headers for the data table
|
54
|
+
alternate_rows => false: turn off alternating of row css classes
|
55
|
+
alternate_cols => true: turn on alternating of column classes, defaults to false
|
56
|
+
|
57
|
+
### Totals
|
58
|
+
|
59
|
+
It is possible to setup totals & subtotals. Total columns take the name of the column that should be totaled. It is also possible to specify multiple total and subtotal columns.
|
60
|
+
|
61
|
+
They also take a default aggregate function name and/or a block.
|
62
|
+
* If only a default function is given, then it is used to calculate the total
|
63
|
+
* If only a block is given then only it is used to calculated the total
|
64
|
+
* If both a block and a function are given then the default aggregate function is called first then its result is passed into the block for further processing.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
DataTable.render(@collection) do |t|
|
68
|
+
t.column :column_1, "Title"
|
69
|
+
t.column :column_2, "Title 2"
|
70
|
+
t.column :column_3, "Title 3"
|
71
|
+
|
72
|
+
# Use a default function and no block.
|
73
|
+
t.total :column_1, 0, :sum
|
74
|
+
|
75
|
+
# Pass only a block and an index
|
76
|
+
t.total :column_2, 1, do |values|
|
77
|
+
# custom methods here
|
78
|
+
end
|
79
|
+
|
80
|
+
# Pass a default function and a block
|
81
|
+
t.total(:column_3, 2, :sum) do |aggregate_total|
|
82
|
+
format_money(aggregate_total)
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
Possible default functions: `:sum`, `:avg`, `:min`, `:max`
|
89
|
+
|
90
|
+
|
91
|
+
### Sub Totals
|
92
|
+
|
93
|
+
SubTotals work in a similar way to Totals. The main difference is that you need to call group_by to specify the different subtotal groupings.
|
94
|
+
|
95
|
+
When configuring more than one subtotal column the index parameter is required
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
DataTable.render(@collection) do |t|
|
99
|
+
t.column :column_1, "Title"
|
100
|
+
t.column :column_2, "Title 2"
|
101
|
+
t.column :column_3, "Title 3"
|
102
|
+
t.column :column_4, "Title 4"
|
103
|
+
|
104
|
+
t.group_by :column_1
|
105
|
+
|
106
|
+
t.subtotal :column_2, 0, :sum
|
107
|
+
t.subtotal :column_3, 1, :max
|
108
|
+
end
|
109
|
+
```
|
110
|
+
It is possible to use `group_by` on its own without subtotaling.
|
111
|
+
|
112
|
+
### Multiple Groups and Sub Totals
|
113
|
+
|
114
|
+
It is also possible to combine multiple `group_by` with multiple `subtotal`. In this
|
115
|
+
scenario you must specify a `level` for each group by passing an integer value.
|
116
|
+
When only specifying a group you may omit `level`.
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
DataTable.render(@collection) do |t|
|
120
|
+
t.column :column_1, "Title"
|
121
|
+
t.column :column_2, "Title 2"
|
122
|
+
t.column :column_3, "Title 3"
|
123
|
+
|
124
|
+
t.group_by :column_1, 0
|
125
|
+
t.group_by :column_2, 1
|
126
|
+
|
127
|
+
t.subtotal :column_3, 0, :sum
|
128
|
+
t.subtotal :column_3, 0, :sum
|
129
|
+
end
|
130
|
+
```
|
131
|
+
|
132
|
+
You can also combine subtotals & totals in the same table.
|
133
|
+
|
134
|
+
## Credits
|
135
|
+
Nearly all of the code for this was written by @smerickson, and later gemified by @sixfeetover.
|
data/Rakefile
CHANGED
@@ -0,0 +1,56 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta charset="utf-8">
|
5
|
+
<title>Data Tables Example</title>
|
6
|
+
<style>
|
7
|
+
table {
|
8
|
+
border-collapse: collapse;
|
9
|
+
}
|
10
|
+
th, td {
|
11
|
+
border: 1px solid black;
|
12
|
+
padding: 10px 15px;
|
13
|
+
}
|
14
|
+
.data_table {width: 100%; empty-cells: show}
|
15
|
+
.data_table td, .data_table th {padding: 3px}
|
16
|
+
|
17
|
+
.data_table caption {font-size: 2em; font-weight: bold}
|
18
|
+
|
19
|
+
.data_table thead {}
|
20
|
+
.data_table thead th {background-color: #ddd; border-bottom: 1px solid #bbb;}
|
21
|
+
|
22
|
+
.data_table tbody {}
|
23
|
+
.data_table tbody tr.alt {background-color: #eee;}
|
24
|
+
|
25
|
+
.data_table .group_header th {text-align: left;}
|
26
|
+
|
27
|
+
.data_table .subtotal {}
|
28
|
+
.data_table .subtotal td {border-top: 1px solid #000;}
|
29
|
+
|
30
|
+
.data_table tfoot {}
|
31
|
+
.data_table tfoot td {border-top: 1px solid #000;}
|
32
|
+
|
33
|
+
.empty_data_table {text-align: center; background-color: #ffc;}
|
34
|
+
|
35
|
+
/* Data Types */
|
36
|
+
.data_table .number, .data_table .money {text-align: right}
|
37
|
+
.data_table .text {text-align: left}
|
38
|
+
|
39
|
+
[class^="level_"] {
|
40
|
+
text-align: left
|
41
|
+
}
|
42
|
+
.level_0 th {
|
43
|
+
padding-left: 0;
|
44
|
+
}
|
45
|
+
.level_1 th {
|
46
|
+
padding-left: 35px;
|
47
|
+
}
|
48
|
+
.level_2 th {
|
49
|
+
padding-left: 70px;
|
50
|
+
}
|
51
|
+
</style>
|
52
|
+
</head>
|
53
|
+
<body>
|
54
|
+
<table id='assignments' class='data_table ' cellspacing='0' cellpadding='0'><caption>Table Title</caption><thead><tr><th class='description ' >Description</th><th class='score ' >Score</th><th class='points ' >Points</th><th class='karma ' >Karma</th></tr></thead><tbody class='yale'><tr class='group_header level_0'><th colspan='4'>Yale</th></tr><tr class='group_header level_1'><th colspan='4'>Math</th></tr><tr class='group_header level_2'><th colspan='4'>Homework</th></tr><tr class='row_0 ' ><td class='description text' >hw1</td><td class='score numeric' >98</td><td class='points numeric' >2</td><td class='karma numeric' >10</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 98.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 10.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 98.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max10.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 98.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 10.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_2'><th colspan='4'>Test</th></tr><tr class='row_0 ' ><td class='description text' >test 1</td><td class='score numeric' >89</td><td class='points numeric' >2</td><td class='karma numeric' >15</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 89.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 15.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 89.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max15.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 89.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 15.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_1'><th colspan='4'>Biology</th></tr><tr class='group_header level_2'><th colspan='4'>Quiz</th></tr><tr class='row_0 ' ><td class='description text' >quiz 1</td><td class='score numeric' >89</td><td class='points numeric' >2</td><td class='karma numeric' >7</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 89.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 7.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 89.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max7.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 89.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 7.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_2'><th colspan='4'>Test</th></tr><tr class='row_0 ' ><td class='description text' >test 2</td><td class='score numeric' >89</td><td class='points numeric' >2</td><td class='karma numeric' >10</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 89.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 10.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 89.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max10.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 89.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 10.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_1'><th colspan='4'>History</th></tr><tr class='group_header level_2'><th colspan='4'>Homework</th></tr><tr class='row_0 ' ><td class='description text' >hw2</td><td class='score numeric' >99</td><td class='points numeric' >2</td><td class='karma numeric' >13</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 99.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 13.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 99.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max13.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 99.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 13.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_2'><th colspan='4'>Test</th></tr><tr class='row_0 ' ><td class='description text' >test 1</td><td class='score numeric' >71</td><td class='points numeric' >2</td><td class='karma numeric' >20</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 71.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 20.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 71.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max20.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 71.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 20.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_1'><th colspan='4'>Law</th></tr><tr class='group_header level_2'><th colspan='4'>Homework</th></tr><tr class='row_0 ' ><td class='description text' >hw3</td><td class='score numeric' >93</td><td class='points numeric' >2</td><td class='karma numeric' >25</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 93.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 25.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 93.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max25.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 93.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 25.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_2'><th colspan='4'>Quiz</th></tr><tr class='row_0 ' ><td class='description text' >quiz 1</td><td class='score numeric' >91</td><td class='points numeric' >2</td><td class='karma numeric' >18</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 91.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 18.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 91.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max18.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 91.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 18.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='parent_subtotal index_0 yale'><td class='description text' ></td><td class='score numeric' >Score Avg 89.875</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 14.75</td></tr><tr class='parent_subtotal index_1 yale'><td class='description text' ></td><td class='score numeric' >Score Max 99.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max25.0</td></tr><tr class='parent_subtotal index_2 yale'><td class='description text' ></td><td class='score numeric' >Score Sum 719.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 118.0</td></tr><tr class='parent_subtotal index_3 yale'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr></tbody><tbody class='harvard'><tr class='group_header level_0'><th colspan='4'>Harvard</th></tr><tr class='group_header level_1'><th colspan='4'>History</th></tr><tr class='group_header level_2'><th colspan='4'>Homework</th></tr><tr class='row_0 ' ><td class='description text' >hw2</td><td class='score numeric' >90</td><td class='points numeric' >2</td><td class='karma numeric' >13</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 90.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 13.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 90.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max13.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 90.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 13.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_2'><th colspan='4'>Test</th></tr><tr class='row_0 ' ><td class='description text' >test 1</td><td class='score numeric' >75</td><td class='points numeric' >2</td><td class='karma numeric' >20</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 75.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 20.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 75.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max20.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 75.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 20.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_1'><th colspan='4'>Law</th></tr><tr class='group_header level_2'><th colspan='4'>Homework</th></tr><tr class='row_0 ' ><td class='description text' >hw3</td><td class='score numeric' >90</td><td class='points numeric' >2</td><td class='karma numeric' >25</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 90.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 25.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 90.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max25.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 90.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 25.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_2'><th colspan='4'>Quiz</th></tr><tr class='row_0 ' ><td class='description text' >quiz 1</td><td class='score numeric' >90</td><td class='points numeric' >2</td><td class='karma numeric' >18</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 90.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 18.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 90.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max18.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 90.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 18.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_1'><th colspan='4'>Math</th></tr><tr class='group_header level_2'><th colspan='4'>Homework</th></tr><tr class='row_0 ' ><td class='description text' >hw1</td><td class='score numeric' >62</td><td class='points numeric' >2</td><td class='karma numeric' >10</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 62.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 10.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 62.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max10.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 62.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 10.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_2'><th colspan='4'>Test</th></tr><tr class='row_0 ' ><td class='description text' >test 1</td><td class='score numeric' >53</td><td class='points numeric' >2</td><td class='karma numeric' >15</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 53.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 15.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 53.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max15.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 53.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 15.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_1'><th colspan='4'>Biology</th></tr><tr class='group_header level_2'><th colspan='4'>Quiz</th></tr><tr class='row_0 ' ><td class='description text' >quiz 1</td><td class='score numeric' >75</td><td class='points numeric' >2</td><td class='karma numeric' >7</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 75.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 7.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 75.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max7.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 75.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 7.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='group_header level_2'><th colspan='4'>Test</th></tr><tr class='row_0 ' ><td class='description text' >test 2</td><td class='score numeric' >32</td><td class='points numeric' >2</td><td class='karma numeric' >10</td></tr><tr class='subtotal index_0'><td class='description text' ></td><td class='score numeric' >Score Avg 32.0</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 10.0</td></tr><tr class='subtotal index_1'><td class='description text' ></td><td class='score numeric' >Score Max 32.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max10.0</td></tr><tr class='subtotal index_2'><td class='description text' ></td><td class='score numeric' >Score Sum 32.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 10.0</td></tr><tr class='subtotal index_3'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr><tr class='parent_subtotal index_0 harvard'><td class='description text' ></td><td class='score numeric' >Score Avg 70.875</td><td class='points numeric' >Points Avg 2.0</td><td class='karma numeric' >Karma Avg 14.75</td></tr><tr class='parent_subtotal index_1 harvard'><td class='description text' ></td><td class='score numeric' >Score Max 90.0</td><td class='points numeric' >Points Max 2.0</td><td class='karma numeric' >Karma Max25.0</td></tr><tr class='parent_subtotal index_2 harvard'><td class='description text' ></td><td class='score numeric' >Score Sum 567.0</td><td class='points numeric' ></td><td class='karma numeric' >Karma Avg 118.0</td></tr><tr class='parent_subtotal index_3 harvard'><td class='description text' ></td><td class='score numeric' ></td><td class='points numeric' >Points custom</td><td class='karma numeric' ></td></tr></tbody><tfoot><tr class='total index_0'><td class='description text' ></td><td class='score numeric' >Total score max: 99.0</td><td class='points numeric' >Total score max: 2.0</td><td class='karma numeric' >Total score max: 25.0</td></tr><tr class='total index_1'><td class='description text' ></td><td class='score numeric' >Total score avg: 80.375</td><td class='points numeric' ></td><td class='karma numeric' ></td></tr></tfoot></table>
|
55
|
+
</body>
|
56
|
+
</html>
|
data/data-table.gemspec
CHANGED
@@ -1,20 +1,25 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require "data-table/version"
|
1
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
2
|
+
require 'data-table/version'
|
4
3
|
|
5
4
|
Gem::Specification.new do |s|
|
6
|
-
s.name =
|
5
|
+
s.name = 'data-table'
|
7
6
|
s.version = DataTable::VERSION
|
8
|
-
s.
|
9
|
-
s.
|
10
|
-
s.
|
11
|
-
s.
|
12
|
-
s.
|
7
|
+
s.licenses = ['MIT']
|
8
|
+
s.authors = ['Steve Erickson', 'Jeff Fraser']
|
9
|
+
s.email = ['sixfeetover@gmail.com']
|
10
|
+
s.homepage = 'https://github.com/veracross/data-table'
|
11
|
+
s.summary = %(Turn arrays of hashes or models in to an HTML table.)
|
12
|
+
s.description = %(data-table is a simple gem that provides a DSL for
|
13
|
+
turning an array of hashes or ActiveRecord objects into an
|
14
|
+
HTML table.)
|
13
15
|
|
14
|
-
s.
|
16
|
+
s.add_development_dependency 'rake', '~> 12'
|
17
|
+
s.add_development_dependency 'rspec', '~> 3'
|
18
|
+
s.add_development_dependency 'guard', '~> 2'
|
19
|
+
s.add_development_dependency 'guard-rspec', '~> 4'
|
15
20
|
|
16
21
|
s.files = `git ls-files`.split("\n")
|
17
22
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
-
s.require_paths = [
|
23
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
24
|
+
s.require_paths = ['lib']
|
20
25
|
end
|
@@ -0,0 +1,161 @@
|
|
1
|
+
require 'byebug'
|
2
|
+
require 'pry'
|
3
|
+
require 'pry-byebug'
|
4
|
+
require 'data-table'
|
5
|
+
|
6
|
+
assignments = [
|
7
|
+
{assignment_type: "Homework", karma: 10, description: "hw1", score: 98, points: 2, course: "Math", school: "Yale"},
|
8
|
+
{assignment_type: "Test", karma: 15, description: "test 1", score: 89, points: 2, course: "Math", school: "Yale"},
|
9
|
+
{assignment_type: "Quiz", karma: 7, description: "quiz 1", score: 89, points: 2, course: "Biology", school: "Yale"},
|
10
|
+
{assignment_type: "Test", karma: 10, description: "test 2", score: 89, points: 2, course: "Biology", school: "Yale"},
|
11
|
+
{assignment_type: "Homework", karma: 13, description: "hw2", score: 99, points: 2, course: "History", school: "Yale"},
|
12
|
+
{assignment_type: "Test", karma: 20, description: "test 1", score: 71, points: 2, course: "History", school: "Yale"},
|
13
|
+
{assignment_type: "Homework", karma: 25, description: "hw3", score: 93, points: 2, course: "Law", school: "Yale"},
|
14
|
+
{assignment_type: "Quiz", karma: 18, description: "quiz 1", score: 91, points: 2, course: "Law", school: "Yale"},
|
15
|
+
{assignment_type: "Homework", karma: 13, description: "hw2", score: 90, points: 2, course: "History", school: "Harvard"},
|
16
|
+
{assignment_type: "Test", karma: 20, description: "test 1", score: 75, points: 2, course: "History", school: "Harvard"},
|
17
|
+
{assignment_type: "Homework", karma: 25, description: "hw3", score: 90, points: 2, course: "Law", school: "Harvard"},
|
18
|
+
{assignment_type: "Quiz", karma: 18, description: "quiz 1", score: 90, points: 2, course: "Law", school: "Harvard"},
|
19
|
+
{assignment_type: "Homework", karma: 10, description: "hw1", score: 62, points: 2, course: "Math", school: "Harvard"},
|
20
|
+
{assignment_type: "Test", karma: 15, description: "test 1", score: 53, points: 2, course: "Math", school: "Harvard"},
|
21
|
+
{assignment_type: "Quiz", karma: 7, description: "quiz 1", score: 75, points: 2, course: "Biology", school: "Harvard"},
|
22
|
+
{assignment_type: "Test", karma: 10, description: "test 2", score: 32, points: 2, course: "Biology", school: "Harvard"}
|
23
|
+
]
|
24
|
+
|
25
|
+
@assignments_table = DataTable.render(assignments) do |t|
|
26
|
+
|
27
|
+
t.id = 'assignments'
|
28
|
+
t.title = "Table Title"
|
29
|
+
# t.repeat_headers_for_groups = true
|
30
|
+
t.column :assignment_type, "Assignment Type"
|
31
|
+
t.column :description, "Description"
|
32
|
+
t.column :score, "Score"
|
33
|
+
t.column :points, "Points"
|
34
|
+
t.column :course, "Course"
|
35
|
+
t.column :school, "School"
|
36
|
+
t.column :karma, "Karma"
|
37
|
+
|
38
|
+
# Multiple levels of grouping
|
39
|
+
t.group_by :school, level: 1
|
40
|
+
t.group_by :course, level: 2
|
41
|
+
t.group_by :assignment_type, level: 3
|
42
|
+
|
43
|
+
# Multiple totals
|
44
|
+
t.total :score, 0, :max do |result|
|
45
|
+
"Total score max: #{result}"
|
46
|
+
end
|
47
|
+
|
48
|
+
t.total :points, 0, :max do |result|
|
49
|
+
"Total score max: #{result}"
|
50
|
+
end
|
51
|
+
|
52
|
+
t.total :karma, 0, :max do |result|
|
53
|
+
"Total score max: #{result}"
|
54
|
+
end
|
55
|
+
|
56
|
+
t.total :score, 1, :avg do |result|
|
57
|
+
"Total score avg: #{result}"
|
58
|
+
end
|
59
|
+
|
60
|
+
t.total :karma, 1, :avg do |average|
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
# Multiple subtotals
|
65
|
+
t.subtotal :score, 0, :avg do |result|
|
66
|
+
"Score Avg #{result}"
|
67
|
+
end
|
68
|
+
|
69
|
+
t.subtotal :points, 0, :avg do |result|
|
70
|
+
"Points Avg #{result}"
|
71
|
+
end
|
72
|
+
|
73
|
+
t.subtotal :karma, 0, :avg do |result|
|
74
|
+
"Karma Avg #{result}"
|
75
|
+
end
|
76
|
+
|
77
|
+
t.subtotal :score, 1, :max do |result|
|
78
|
+
"Score Max #{result}"
|
79
|
+
end
|
80
|
+
|
81
|
+
t.subtotal :karma, 1, :max do |result|
|
82
|
+
"Karma Max#{result}"
|
83
|
+
end
|
84
|
+
|
85
|
+
t.subtotal :points, 1, :max do |result|
|
86
|
+
"Points Max #{result}"
|
87
|
+
end
|
88
|
+
|
89
|
+
t.subtotal :score, 2, :sum do |result|
|
90
|
+
"Score Sum #{result}"
|
91
|
+
end
|
92
|
+
|
93
|
+
t.subtotal :karma, 2, :sum do |result|
|
94
|
+
"Karma Avg #{result}"
|
95
|
+
end
|
96
|
+
|
97
|
+
t.subtotal :points, 3 do |result|
|
98
|
+
"Points custom"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
@assignments_markup = <<-HTML_DOC
|
103
|
+
<!DOCTYPE html>
|
104
|
+
<html>
|
105
|
+
<head>
|
106
|
+
<meta charset="utf-8">
|
107
|
+
<title>Data Tables Example</title>
|
108
|
+
<style>
|
109
|
+
table {
|
110
|
+
border-collapse: collapse;
|
111
|
+
}
|
112
|
+
th, td {
|
113
|
+
border: 1px solid black;
|
114
|
+
padding: 10px 15px;
|
115
|
+
}
|
116
|
+
.data_table {width: 100%; empty-cells: show}
|
117
|
+
.data_table td, .data_table th {padding: 3px}
|
118
|
+
|
119
|
+
.data_table caption {font-size: 2em; font-weight: bold}
|
120
|
+
|
121
|
+
.data_table thead {}
|
122
|
+
.data_table thead th {background-color: #ddd; border-bottom: 1px solid #bbb;}
|
123
|
+
|
124
|
+
.data_table tbody {}
|
125
|
+
.data_table tbody tr.alt {background-color: #eee;}
|
126
|
+
|
127
|
+
.data_table .group_header th {text-align: left;}
|
128
|
+
|
129
|
+
.data_table .subtotal {}
|
130
|
+
.data_table .subtotal td {border-top: 1px solid #000;}
|
131
|
+
|
132
|
+
.data_table tfoot {}
|
133
|
+
.data_table tfoot td {border-top: 1px solid #000;}
|
134
|
+
|
135
|
+
.empty_data_table {text-align: center; background-color: #ffc;}
|
136
|
+
|
137
|
+
/* Data Types */
|
138
|
+
.data_table .number, .data_table .money {text-align: right}
|
139
|
+
.data_table .text {text-align: left}
|
140
|
+
|
141
|
+
[class^="level_"] {
|
142
|
+
text-align: left
|
143
|
+
}
|
144
|
+
.level_0 th {
|
145
|
+
padding-left: 0;
|
146
|
+
}
|
147
|
+
.level_1 th {
|
148
|
+
padding-left: 35px;
|
149
|
+
}
|
150
|
+
.level_2 th {
|
151
|
+
padding-left: 70px;
|
152
|
+
}
|
153
|
+
</style>
|
154
|
+
</head>
|
155
|
+
<body>
|
156
|
+
#{@assignments_table}
|
157
|
+
</body>
|
158
|
+
</html>
|
159
|
+
HTML_DOC
|
160
|
+
|
161
|
+
File.open("assigments_table.html", "w") { |f| f.write @assignments_markup }
|