greentable 0.0.4 → 0.0.5
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.
- data/README.md +155 -25
- data/greentable.gemspec +2 -1
- data/lib/greentable/configuration.rb +18 -0
- data/lib/greentable/greentable_table.rb +13 -11
- data/lib/greentable.rb +2 -5
- data/todo.txt +8 -0
- metadata +6 -3
data/README.md
CHANGED
@@ -1,39 +1,169 @@
|
|
1
|
-
|
2
|
-
assets/greentable.css
|
3
|
-
|
4
|
-
greentable
|
1
|
+
Greentable
|
5
2
|
==========
|
6
3
|
|
7
|
-
Rails declarative html tables with export (xls,
|
4
|
+
Rails declarative html tables with export (csv,xls,pdf,xml) features.
|
5
|
+
|
6
|
+
Please note that the export module is still under development.
|
8
7
|
|
9
|
-
|
8
|
+
Tested on rails 3.2, using ruby 1.9.3
|
10
9
|
|
11
|
-
|
10
|
+
## Installation
|
11
|
+
```ruby
|
12
12
|
gem 'greentable'
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
The Greentable gem provides two methods only. To produce a table from an array of elements, do the following in a view:
|
17
|
+
|
18
|
+
```haml
|
19
|
+
=greentable(users) do |gt,user|
|
20
|
+
- gt.col('NAME') do
|
21
|
+
=user.name
|
22
|
+
- gt.col('EMAIL') do
|
23
|
+
=user.email
|
24
|
+
```
|
25
|
+
|
26
|
+
which will produce the following html:
|
27
|
+
|
28
|
+
```html
|
29
|
+
<table>
|
30
|
+
<thead>
|
31
|
+
<tr>
|
32
|
+
<th>NAME</th>
|
33
|
+
<th>EMAIL</th>
|
34
|
+
</tr>
|
35
|
+
</thead>
|
36
|
+
<tbody>
|
37
|
+
<tr>
|
38
|
+
<td>Joe Ruby</td>
|
39
|
+
<td>jruby@example.com</td>
|
40
|
+
</tr>
|
41
|
+
<tr>
|
42
|
+
<td>Donald Rails</td>
|
43
|
+
<td>drails@example.com</td>
|
44
|
+
</tr>
|
45
|
+
</tbody>
|
46
|
+
</table>
|
47
|
+
```
|
48
|
+
|
49
|
+
## Optional Options
|
50
|
+
|
51
|
+
Both greentable and greentable.col may be called with an optional hash
|
52
|
+
|
53
|
+
```haml
|
54
|
+
=greentable(array, class: 'a', style: 'b:c', tr: {class: 'd'}, th: {onclick: 'e();'}, td: {'data-target' => 'f'} ) do |gt, el|
|
55
|
+
-gt.col('Col 1', class: 'g', th: {class: 'h'}) do
|
56
|
+
= el.col1
|
57
|
+
-gt.col('Col 2') do
|
58
|
+
= el.col2
|
59
|
+
```
|
60
|
+
|
61
|
+
will produce
|
62
|
+
|
63
|
+
```html
|
64
|
+
<table class='a' style='b:c'>
|
65
|
+
<thead>
|
66
|
+
<tr class='d'>
|
67
|
+
<th class='h' onclick='e();'>Col 1</th>
|
68
|
+
<th onclick='e();'>Col 2</th>
|
69
|
+
</tr>
|
70
|
+
</thead>
|
71
|
+
<tbody>
|
72
|
+
<tr class='d'>
|
73
|
+
<td class='g' data-target='f'>...</td>
|
74
|
+
<td data-target='f'>...</td>
|
75
|
+
</tr>
|
76
|
+
</tbody>
|
77
|
+
</table>
|
78
|
+
```
|
79
|
+
|
80
|
+
If you need column names or options to be a little more dynamic, you can use procs:
|
81
|
+
|
82
|
+
```haml
|
83
|
+
=greentable(years) do |gt,year|
|
84
|
+
-gt.col('Year') do
|
85
|
+
=year.to_s
|
86
|
+
-4.times do |week|
|
87
|
+
-gt.col(Proc.new{ week % 2 == 0 ? 'An even week' : 'An odd week' }, td: {style: Proc.new{ year % 2 == 1 ? 'background-color:red' : nil }} ) do
|
88
|
+
=week
|
89
|
+
```
|
90
|
+
|
91
|
+
will produce
|
92
|
+
|
93
|
+
```html
|
94
|
+
<table>
|
95
|
+
<thead>
|
96
|
+
<tr>
|
97
|
+
<th>Year</th>
|
98
|
+
<th>An even week</th>
|
99
|
+
<th>An odd week</th>
|
100
|
+
<th>An even week</th>
|
101
|
+
</tr>
|
102
|
+
</thead>
|
103
|
+
<tbody>
|
104
|
+
<tr>
|
105
|
+
<td>2013</td>
|
106
|
+
<td>0</td>
|
107
|
+
<td style='background-color:red'>1</td>
|
108
|
+
<td>2</td>
|
109
|
+
<td style='background-color:red'>3</td>
|
110
|
+
</tr>
|
111
|
+
<tr>
|
112
|
+
<td>2014</td>
|
113
|
+
<td>0</td>
|
114
|
+
<td style='background-color:red'>1</td>
|
115
|
+
<td>2</td>
|
116
|
+
<td style='background-color:red'>3</td>
|
117
|
+
</tr>
|
118
|
+
...
|
119
|
+
</tbody>
|
120
|
+
</table>
|
121
|
+
```
|
122
|
+
|
123
|
+
|
124
|
+
## Global Defaults
|
125
|
+
You can configure global defaults for all your greentables.
|
126
|
+
|
127
|
+
In config/initializers/greentable.rb
|
13
128
|
|
14
|
-
### Usage
|
15
|
-
blbabl
|
16
129
|
```ruby
|
17
|
-
|
130
|
+
Greentable.configure do |config|
|
131
|
+
config.defaults = {class: 'myTableClass', tr: {class: 'myTrClass'}, th: {style: 'cursor:pointer'}, td: {class: 'pi', onclick: 'alert(3.14159265359)'}}
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
and in some view:
|
136
|
+
|
137
|
+
```erb
|
138
|
+
<%= greentable([3.14]) do |gt,element| %>
|
139
|
+
<% gt.col('First Column') do %>
|
140
|
+
<%= element %>
|
141
|
+
<% end %>
|
142
|
+
<% end %>
|
143
|
+
```
|
144
|
+
|
145
|
+
will produce
|
146
|
+
|
147
|
+
```html
|
148
|
+
<table class='myTableClass'>
|
149
|
+
<thead>
|
150
|
+
<tr class='myTrClass'>
|
151
|
+
<th style='cursor:pointer'>First Column</th>
|
152
|
+
</tr>
|
153
|
+
</thead>
|
154
|
+
<tbody>
|
155
|
+
<tr class='myTrClass'>
|
156
|
+
<td class='pi' onclick='alert(3.14159265359)'>3.14</td>
|
157
|
+
</tr>
|
158
|
+
</tbody>
|
159
|
+
</table>
|
18
160
|
```
|
19
|
-
bla2
|
20
161
|
|
21
|
-
|
22
|
-
display_on = first | last | all
|
162
|
+
## Green Export
|
23
163
|
|
24
|
-
|
25
|
-
extend with a plugin
|
26
|
-
### Options
|
164
|
+
Greentable enables you to export your greentable data for download in various formats seamlessly by a rack middleware.
|
27
165
|
|
28
|
-
greentable records, class: 'listtable', tr: {...}, th: {...}, td: {...} do |gt, r|
|
29
166
|
|
30
|
-
|
31
|
-
show_empty
|
32
|
-
export
|
33
|
-
media skip (?)
|
167
|
+
[export still under development. more soon]
|
34
168
|
|
35
|
-
### Special Query Parameters
|
36
|
-
greentable_export
|
37
|
-
greentable_id
|
38
|
-
greentable_export_filename
|
39
169
|
|
data/greentable.gemspec
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "greentable"
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.5"
|
4
4
|
s.author = "Wael Chatila"
|
5
5
|
s.homepage = "https://github.com/waelchatila/greentable"
|
6
6
|
s.summary = "Rails declarative html tables with export features"
|
7
7
|
s.description = "Rails declarative html tables with export features"
|
8
|
+
s.license = 'LGPLv3+'
|
8
9
|
|
9
10
|
s.files = `git ls-files`.split("\n")
|
10
11
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Greentable
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :defaults
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@defaults = {}
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
attr_accessor :configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configure
|
15
|
+
self.configuration ||= Configuration.new
|
16
|
+
yield configuration
|
17
|
+
end
|
18
|
+
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'greentable/configuration'
|
1
2
|
require 'greentable/greentable_counter'
|
2
3
|
|
3
4
|
module Greentable
|
@@ -6,16 +7,15 @@ module Greentable
|
|
6
7
|
def initialize(parent, records, opts)
|
7
8
|
@parent = parent
|
8
9
|
@records = records
|
9
|
-
|
10
|
-
@
|
11
|
-
@
|
12
|
-
@
|
10
|
+
defaults = Greentable.configuration.defaults.clone rescue {}
|
11
|
+
@defaults_tr = (defaults.delete(:tr) || {}).deep_merge(opts.delete(:tr) || {})
|
12
|
+
@defaults_th = (defaults.delete(:th) || {}).deep_merge(opts.delete(:th) || {})
|
13
|
+
@defaults_td = (defaults.delete(:td) || {}).deep_merge(opts.delete(:td) || {})
|
14
|
+
@opts = defaults.deep_merge(opts)
|
13
15
|
|
14
16
|
@tr_attributes = []
|
15
|
-
|
16
17
|
@th_attributes = []
|
17
18
|
@th_html = []
|
18
|
-
|
19
19
|
@td_attributes = []
|
20
20
|
@td_html = []
|
21
21
|
|
@@ -29,10 +29,11 @@ module Greentable
|
|
29
29
|
|
30
30
|
def col(th = nil, opts = {}, &block)
|
31
31
|
@th_html[@current_col] = th
|
32
|
+
@th_attributes[@current_col] = opts.delete(:th) || {}
|
33
|
+
@td_attributes[@current_col] = opts
|
32
34
|
|
33
35
|
@td_html[@row_counter.i] ||= []
|
34
36
|
@td_html[@row_counter.i][@current_col] = @parent.capture(&block)
|
35
|
-
@td_attributes[@row_counter.i] = opts
|
36
37
|
|
37
38
|
@current_col += 1
|
38
39
|
return nil
|
@@ -54,8 +55,8 @@ module Greentable
|
|
54
55
|
unless @th_html.compact.empty?
|
55
56
|
ret << "<thead>"
|
56
57
|
ret << "<tr>"
|
57
|
-
@th_html.
|
58
|
-
ret << "<th#{do_attributes(@
|
58
|
+
@th_html.each_with_index do |th,i|
|
59
|
+
ret << "<th#{do_attributes(@th_attributes[i])}>#{th.is_a?(Proc) ? th.call.to_s : th}</th>"
|
59
60
|
end
|
60
61
|
ret << "</tr>"
|
61
62
|
ret << "</thead>"
|
@@ -63,9 +64,9 @@ module Greentable
|
|
63
64
|
ret << "<tbody>"
|
64
65
|
|
65
66
|
@row_counter.i.times do |i|
|
66
|
-
ret << "<tr#{do_attributes(@
|
67
|
+
ret << "<tr#{do_attributes(@defaults_tr.deep_merge(@tr_attributes[i]||{}))}>"
|
67
68
|
@td_html[i].each do |td|
|
68
|
-
ret << "<td#{do_attributes(@
|
69
|
+
ret << "<td#{do_attributes(@defaults_td.deep_merge(@td_attributes[i]||{}))}>#{td}</td>"
|
69
70
|
end
|
70
71
|
ret << "</tr>"
|
71
72
|
end
|
@@ -80,5 +81,6 @@ module Greentable
|
|
80
81
|
ret = " " + ret unless ret.empty?
|
81
82
|
return ret
|
82
83
|
end
|
84
|
+
|
83
85
|
end
|
84
86
|
end
|
data/lib/greentable.rb
CHANGED
data/todo.txt
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: greentable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-09-
|
12
|
+
date: 2013-09-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -54,13 +54,16 @@ files:
|
|
54
54
|
- Rakefile
|
55
55
|
- greentable.gemspec
|
56
56
|
- lib/greentable.rb
|
57
|
+
- lib/greentable/configuration.rb
|
57
58
|
- lib/greentable/export.rb
|
58
59
|
- lib/greentable/greentable_counter.rb
|
59
60
|
- lib/greentable/greentable_table.rb
|
60
61
|
- lib/greentable/railtie.rb
|
61
62
|
- lib/greentable/view_helpers.rb
|
63
|
+
- todo.txt
|
62
64
|
homepage: https://github.com/waelchatila/greentable
|
63
|
-
licenses:
|
65
|
+
licenses:
|
66
|
+
- LGPLv3+
|
64
67
|
post_install_message:
|
65
68
|
rdoc_options: []
|
66
69
|
require_paths:
|