data-table 1.0.1 → 2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39eba54db8dd1c599476d714baf1245cd7517333
4
+ data.tar.gz: cbf63f58a76943fa40793161c0a6e5813bc3290a
5
+ SHA512:
6
+ metadata.gz: '0509df5140e6bcd65a0ca07c04db40e7722161ee898c6a41552ec550a982a991e694f970479a9a70e8cf40b24cb442d1483cf6c6ff2199391b0aabae32319eb5'
7
+ data.tar.gz: 1d19797af22349f05d6a811bb97dc06f235bba096bff0dc7637e180dc866aaf4e16fc3e9ef445c45b463f018023b4080ad2e530855cd004431d43f4a2597eb49
data/.gitignore CHANGED
@@ -2,3 +2,7 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+
6
+ .ruby-version
7
+ .byebug_history
8
+ .vscode/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.3
4
+ - 2.4.0
@@ -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/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # DataTable
2
+ [![Build Status](https://secure.travis-ci.org/veracross/data-table.png)](http://travis-ci.org/veracross/data-table)
3
+ [![Code Climate](https://codeclimate.com/github/veracross/data-table.png)](https://codeclimate.com/github/veracross/data-table)
2
4
 
3
- ## Credits
4
- Nearly all of the code for this was written by @smerickson.
5
+ DataTable renders collections (an array of hashes or ActiveRecord models) as HTML tables.
5
6
 
6
7
  ## Install
7
8
  ```ruby
@@ -14,15 +15,130 @@ or, in your Gemfile
14
15
  gem 'data-table'
15
16
  ```
16
17
 
17
- ## Synopsis
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
+
18
118
  ```ruby
19
- records = [{:name => 'Matz', :language => 'Ruby'}, {:name => 'Ashkenas', :language => 'CoffeeScript'}, {:name => 'Guido', :language => 'Python'}]
20
-
21
- DataTable.render(records) do |t|
22
- t.id = 'language-table'
23
- t.cssClass = 'table'
24
-
25
- t.column :name, 'Creator Name'
26
- t.column :language, 'Language'
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
27
129
  end
28
- ```
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.
136
+
137
+ ## License
138
+ Copyright (c) 2012-2013 Jeff Fraser (Veracross LLC) jfraser@breuer.com
139
+
140
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
141
+
142
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
143
+
144
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -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>
@@ -1,20 +1,27 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
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 = "data-table"
5
+ s.name = 'data-table'
7
6
  s.version = DataTable::VERSION
8
- s.authors = ["Steve Erickson", "Jeff Fraser"]
9
- s.email = ["sixfeetover@gmail.com"]
10
- s.homepage = "https://github.com/sixfeetover/data-table"
11
- s.summary = %q{Turn arrays of hashes or models in to an HTML table.}
12
- s.description = %q{data-table is a simple gem that provides a DSL for allowing you do turn an array of hashes or ActiveRecord objects into an HTML table.}
7
+ s.licenses = ['Nonstandard']
8
+ s.authors = ['Steve Erickson', 'Jeff Fraser']
9
+ s.email = ['sixfeetover@gmail.com']
10
+ s.homepage = 'https://github.com/sixfeetover/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.rubyforge_project = "data-table"
16
+ s.rubyforge_project = 'data-table'
17
+
18
+ s.add_development_dependency 'rake', '~> 12'
19
+ s.add_development_dependency 'rspec', '~> 3'
20
+ s.add_development_dependency 'guard', '~> 2'
21
+ s.add_development_dependency 'guard-rspec', '~> 4'
15
22
 
16
23
  s.files = `git ls-files`.split("\n")
17
24
  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 = ["lib"]
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
26
+ s.require_paths = ['lib']
20
27
  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 }