greentable 0.0.9 → 0.9.0

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.
Files changed (67) hide show
  1. data/.coveralls.yml +1 -0
  2. data/.gitignore +13 -0
  3. data/.travis.yml +8 -0
  4. data/Gemfile +17 -0
  5. data/Gemfile.lock +135 -0
  6. data/Guardfile +8 -0
  7. data/README.md +77 -24
  8. data/Rakefile +38 -1
  9. data/greentable.gemspec +30 -15
  10. data/lib/greentable/export.rb +38 -28
  11. data/lib/greentable/greentable_table.rb +40 -11
  12. data/lib/greentable/railtie.rb +3 -3
  13. data/lib/greentable/version.rb +3 -0
  14. data/lib/greentable/view_helpers.rb +0 -1
  15. data/lib/greentable.rb +1 -1
  16. data/lib/tasks/greentable_tasks.rake +4 -0
  17. data/test/dummy/Rakefile +7 -0
  18. data/test/dummy/app/assets/javascripts/application.js +15 -0
  19. data/test/dummy/app/assets/stylesheets/application.css +16 -0
  20. data/test/dummy/app/controllers/application_controller.rb +7 -0
  21. data/test/dummy/app/helpers/application_helper.rb +2 -0
  22. data/test/dummy/app/mailers/.gitkeep +0 -0
  23. data/test/dummy/app/models/.gitkeep +0 -0
  24. data/test/dummy/app/views/application/home.html.erb +16 -0
  25. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  26. data/test/dummy/config/application.rb +59 -0
  27. data/test/dummy/config/boot.rb +10 -0
  28. data/test/dummy/config/database.yml +25 -0
  29. data/test/dummy/config/environment.rb +5 -0
  30. data/test/dummy/config/environments/development.rb +37 -0
  31. data/test/dummy/config/environments/production.rb +67 -0
  32. data/test/dummy/config/environments/test.rb +37 -0
  33. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  34. data/test/dummy/config/initializers/inflections.rb +15 -0
  35. data/test/dummy/config/initializers/mime_types.rb +5 -0
  36. data/test/dummy/config/initializers/secret_token.rb +7 -0
  37. data/test/dummy/config/initializers/session_store.rb +8 -0
  38. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  39. data/test/dummy/config/locales/en.yml +5 -0
  40. data/test/dummy/config/routes.rb +3 -0
  41. data/test/dummy/config.ru +4 -0
  42. data/test/dummy/db/development.sqlite3 +0 -0
  43. data/test/dummy/db/test.sqlite3 +0 -0
  44. data/test/dummy/lib/assets/.gitkeep +0 -0
  45. data/test/dummy/log/.gitkeep +0 -0
  46. data/test/dummy/log/development.log +2533 -0
  47. data/test/dummy/log/test.log +1881 -0
  48. data/test/dummy/public/404.html +26 -0
  49. data/test/dummy/public/422.html +26 -0
  50. data/test/dummy/public/500.html +25 -0
  51. data/test/dummy/public/favicon.ico +0 -0
  52. data/test/dummy/script/rails +6 -0
  53. data/test/dummy/tmp/cache/assets/C4C/3E0/sprockets%2F316d5a21a3694448e136215cf552c829 +0 -0
  54. data/test/dummy/tmp/cache/assets/C81/FF0/sprockets%2F839f902c241150aec5ba59751228875d +0 -0
  55. data/test/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
  56. data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  57. data/test/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
  58. data/test/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
  59. data/test/dummy/tmp/cache/assets/DAA/B50/sprockets%2F7e7e12081cd77f6e2a99a61ee35b8cce +0 -0
  60. data/test/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
  61. data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  62. data/test/dummy/tmp/cache/assets/E21/3E0/sprockets%2Ff9530da0d9bbb15351aef3eb378bddbc +0 -0
  63. data/test/export_test.rb +46 -0
  64. data/test/greentable_test.rb +90 -0
  65. data/test/test.iml +27 -0
  66. data/test/test_helper.rb +28 -0
  67. metadata +195 -5
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ *.iml
2
+ *.ipr
3
+ *.iws
4
+ test/*.iml
5
+ *.gem
6
+ .idea
7
+ .bundle/
8
+ log/*.log
9
+ pkg/
10
+ test/dummy/log/*.log
11
+ test/dummy/tmp/
12
+ test/dummy/.sass-cache
13
+ coverage
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 1.9.3
5
+ # - jruby-18mode # JRuby in 1.8 mode
6
+ # - jruby-19mode # JRuby in 1.9 mode
7
+ # - rbx-2.1.1
8
+ # - 1.8.7
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in greentable.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # jquery-rails is used by the dummy application
9
+ gem "jquery-rails"
10
+
11
+ # Declare any dependencies that are still in development here instead of in
12
+ # your gemspec. These might include edge Rails or gems from your path or
13
+ # Git. Remember to move these dependencies to your gemspec before releasing
14
+ # your gem to rubygems.org.
15
+
16
+ # To use debugger
17
+ # gem 'debugger'
data/Gemfile.lock ADDED
@@ -0,0 +1,135 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ greentable (0.9.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actionmailer (3.2.17)
10
+ actionpack (= 3.2.17)
11
+ mail (~> 2.5.4)
12
+ actionpack (3.2.17)
13
+ activemodel (= 3.2.17)
14
+ activesupport (= 3.2.17)
15
+ builder (~> 3.0.0)
16
+ erubis (~> 2.7.0)
17
+ journey (~> 1.0.4)
18
+ rack (~> 1.4.5)
19
+ rack-cache (~> 1.2)
20
+ rack-test (~> 0.6.1)
21
+ sprockets (~> 2.2.1)
22
+ activemodel (3.2.17)
23
+ activesupport (= 3.2.17)
24
+ builder (~> 3.0.0)
25
+ activerecord (3.2.17)
26
+ activemodel (= 3.2.17)
27
+ activesupport (= 3.2.17)
28
+ arel (~> 3.0.2)
29
+ tzinfo (~> 0.3.29)
30
+ activeresource (3.2.17)
31
+ activemodel (= 3.2.17)
32
+ activesupport (= 3.2.17)
33
+ activesupport (3.2.17)
34
+ i18n (~> 0.6, >= 0.6.4)
35
+ multi_json (~> 1.0)
36
+ arel (3.0.3)
37
+ builder (3.0.4)
38
+ coderay (1.0.9)
39
+ erubis (2.7.0)
40
+ fastercsv (1.5.5)
41
+ ffi (1.9.0)
42
+ formatador (0.2.4)
43
+ guard (1.8.2)
44
+ formatador (>= 0.2.4)
45
+ listen (>= 1.0.0)
46
+ lumberjack (>= 1.0.2)
47
+ pry (>= 0.9.10)
48
+ thor (>= 0.14.6)
49
+ guard-test (1.0.0)
50
+ guard (>= 1.8)
51
+ test-unit (~> 2.2)
52
+ hike (1.2.3)
53
+ i18n (0.6.9)
54
+ journey (1.0.4)
55
+ jquery-rails (3.1.0)
56
+ railties (>= 3.0, < 5.0)
57
+ thor (>= 0.14, < 2.0)
58
+ json (1.8.1)
59
+ listen (1.3.0)
60
+ rb-fsevent (>= 0.9.3)
61
+ rb-inotify (>= 0.9)
62
+ rb-kqueue (>= 0.2)
63
+ lumberjack (1.0.4)
64
+ mail (2.5.4)
65
+ mime-types (~> 1.16)
66
+ treetop (~> 1.4.8)
67
+ method_source (0.8.2)
68
+ mime-types (1.25.1)
69
+ mini_portile (0.5.2)
70
+ multi_json (1.9.2)
71
+ nokogiri (1.6.1)
72
+ mini_portile (~> 0.5.0)
73
+ polyglot (0.3.4)
74
+ pry (0.9.12.2)
75
+ coderay (~> 1.0.5)
76
+ method_source (~> 0.8)
77
+ slop (~> 3.4)
78
+ rack (1.4.5)
79
+ rack-cache (1.2)
80
+ rack (>= 0.4)
81
+ rack-ssl (1.3.4)
82
+ rack
83
+ rack-test (0.6.2)
84
+ rack (>= 1.0)
85
+ rails (3.2.17)
86
+ actionmailer (= 3.2.17)
87
+ actionpack (= 3.2.17)
88
+ activerecord (= 3.2.17)
89
+ activeresource (= 3.2.17)
90
+ activesupport (= 3.2.17)
91
+ bundler (~> 1.0)
92
+ railties (= 3.2.17)
93
+ railties (3.2.17)
94
+ actionpack (= 3.2.17)
95
+ activesupport (= 3.2.17)
96
+ rack-ssl (~> 1.3.2)
97
+ rake (>= 0.8.7)
98
+ rdoc (~> 3.4)
99
+ thor (>= 0.14.6, < 2.0)
100
+ rake (10.2.2)
101
+ rb-fsevent (0.9.3)
102
+ rb-inotify (0.9.1)
103
+ ffi (>= 0.5.0)
104
+ rb-kqueue (0.2.0)
105
+ ffi (>= 0.5.0)
106
+ rdoc (3.12.2)
107
+ json (~> 1.4)
108
+ slop (3.4.6)
109
+ sprockets (2.2.2)
110
+ hike (~> 1.2)
111
+ multi_json (~> 1.0)
112
+ rack (~> 1.0)
113
+ tilt (~> 1.1, != 1.3.0)
114
+ sqlite3 (1.3.9)
115
+ test-unit (2.5.5)
116
+ thor (0.19.1)
117
+ tilt (1.4.1)
118
+ treetop (1.4.15)
119
+ polyglot
120
+ polyglot (>= 0.3.1)
121
+ tzinfo (0.3.39)
122
+
123
+ PLATFORMS
124
+ ruby
125
+
126
+ DEPENDENCIES
127
+ fastercsv
128
+ greentable!
129
+ guard-test
130
+ jquery-rails
131
+ nokogiri
132
+ rack-test
133
+ rails (~> 3.2.17)
134
+ sqlite3
135
+ test-unit
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :test, :drb => false, :all_on_start => true do
5
+ watch(%r{^test/.+_test\.rb$})
6
+ end
7
+
8
+
data/README.md CHANGED
@@ -1,12 +1,8 @@
1
+ [![Build Status](https://travis-ci.org/waelchatila/greentable.svg?branch=master)](https://travis-ci.org/waelchatila/greentable)
1
2
  Greentable
2
3
  ==========
3
-
4
4
  Greentable produces HTML tables from an array without you having to deal with any HTML elements.
5
- Greentable is an declarative approach to HTML tables and adds additional export (csv,xls,pdf,xml) features.
6
-
7
- Please note that the export module is still under development.
8
-
9
- Tested on rails 3.2, using ruby 1.9.3
5
+ Greentable is an declarative approach to HTML tables and adds additional export features.
10
6
 
11
7
  ## Installation
12
8
  ```ruby
@@ -14,7 +10,7 @@ gem 'greentable'
14
10
  ```
15
11
 
16
12
  ## Usage
17
- The Greentable gem provides two methods only. To produce a table from an array of elements, do the following in a view:
13
+ To produce a table from an array of elements, do the following in a view:
18
14
 
19
15
  ```haml
20
16
  =greentable(users) do |gt,user|
@@ -47,38 +43,67 @@ which will produce the following html:
47
43
  </table>
48
44
  ```
49
45
 
50
- ## Optional Options
46
+ ## Options
51
47
 
52
48
  Both greentable and greentable.col may be called with an optional hash
53
49
 
54
50
  ```haml
55
- =greentable(array, class: 'a', style: 'b:c', tr: {class: 'd'}, th: {onclick: 'e();'}, td: {'data-target' => 'f'} ) do |gt, el|
56
- -gt.col('Col 1', class: 'g', th: {class: 'h'}) do
57
- = el.col1
58
- -gt.col('Col 2') do
59
- = el.col2
51
+ =greentable(array, class: 'table_class') do |gt, el|
52
+ -gt.col('col0', th: {class: 'th_class'}) do
53
+ =el.col0
54
+ -gt.col('col1', td: {class: 'td_class'}) do
55
+ =el.col1
56
+ ```
57
+
58
+ will produce
59
+
60
+ ```html
61
+ <table class="table_class"">
62
+ <thead>
63
+ <tr>
64
+ <th class="th_class">col0</th>
65
+ <th>col1</th>
66
+ </tr>
67
+ </thead>
68
+ <tbody>
69
+ <tr>
70
+ <td>...</td>
71
+ <td class="td_class">...</td>
72
+ </tr>
73
+ </tbody>
74
+ </table>
75
+ ```
76
+
77
+ A comprehensive example:
78
+
79
+ ```haml
80
+ =greentable(array, class: 'a aa', style: 'b:c', tr: {class: 'd'}, th: {onclick: 'e();'}, td: {class: 'f', 'data-target' => 'g'} ) do |gt, el|
81
+ -gt.col('col0', class: 'h', th: {class: 'i'}) do
82
+ =el.col0
83
+ -gt.col('col1', td: {class: 'j'}) do
84
+ =el.col1
60
85
  ```
61
86
 
62
87
  will produce
63
88
 
64
89
  ```html
65
- <table class='a' style='b:c'>
90
+ <table class="a aa" style="b:c">
66
91
  <thead>
67
- <tr class='d'>
68
- <th class='h' onclick='e();'>Col 1</th>
69
- <th onclick='e();'>Col 2</th>
92
+ <tr class="d">
93
+ <th onclick="e();" class="h i ee">col0</th>
94
+ <th onclick="e();" class="ee">col1</th>
70
95
  </tr>
71
96
  </thead>
72
97
  <tbody>
73
- <tr class='d'>
74
- <td class='g' data-target='f'>...</td>
75
- <td data-target='f'>...</td>
98
+ <tr class="d">
99
+ <td data-target="f" class="h ff">0</td>
100
+ <td data-target="f" class="j ff">0</td>
76
101
  </tr>
77
102
  </tbody>
78
103
  </table>
79
104
  ```
80
105
 
81
- If you need column names or options to be a little more dynamic, you can use procs:
106
+ If you need column names or options to be dynamic, you can use procs:
82
107
 
83
108
  ```haml
84
109
  =greentable(years) do |gt,year|
@@ -160,11 +185,39 @@ will produce
160
185
  </table>
161
186
  ```
162
187
 
163
- ## Green Export
188
+ ## Green Export &amp; Green Printing
189
+
190
+ Greentable enables you to export or print your greentable data for download in various formats seamlessly by a rack middleware.
191
+ Add a couple of http query parameters in your view and your done:
192
+
193
+ ```erb
194
+ <a href="/?greentable_export=csv&greentable_id=greentable_id">Export CSV</a>
195
+ |
196
+ <a href="/?greentable_export=print&greentable_id=greentable_id">Print</a>
197
+
198
+ <%= greentable([0], :id => 'greentable_id') do |gt, el| %>
199
+ <% gt.col('i') do %>
200
+ <%= el %>
201
+ <% end %>
202
+ <% gt.col('i+1') do %>
203
+ <%= el + 1 %>
204
+ <% end %>
205
+ <% end %>
206
+ ```
207
+
208
+ ### Available Query Parameters
164
209
 
165
- Greentable enables you to export your greentable data for download in various formats seamlessly by a rack middleware.
210
+ * **greentable_id**=*any string* [REQUIRED]. A unique html tag id to tell the rack middleware which greentable to extract.
211
+ * **greentable_export**=**csv** | **print** [REQUIRED].
212
+ **csv** will produce a CSV file. Headers will be the table headers. Colspans are honored (row spans are not).
213
+ **print** will extract the green table by id and insert a javascript snippet to print the doc
214
+ ```javascript
215
+ window.onload = function() { window.focus(); window.print(); }
216
+ ```
217
+ Any css styling done within the html *HEAD* will remain. Anything within the *BODY* will be replaced with the greentable content.
218
+ * **greentable_export_filename**=*any string* [OPTIONAL]. *default: 'export.csv'*.
219
+ Specifies the filename in the *Content-Disposition* header. Only used if *greentable_export=csv*
166
220
 
167
221
 
168
- [export still under development. more soon]
169
222
 
170
223
 
data/Rakefile CHANGED
@@ -1 +1,38 @@
1
- require "bundler/gem_tasks"
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Greentable'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
data/greentable.gemspec CHANGED
@@ -1,18 +1,33 @@
1
- Gem::Specification.new do |s|
2
- s.name = "greentable"
3
- s.version = "0.0.9"
4
- s.author = "Wael Chatila"
5
- s.homepage = "https://github.com/waelchatila/greentable"
6
- s.summary = "Rails declarative html tables with export features"
7
- s.description = "Greentable produces HTML tables from an array without you having to deal with any HTML elements."
8
- s.license = 'LGPLv3+'
1
+ $:.push File.expand_path("../lib", __FILE__)
9
2
 
10
- s.files = `git ls-files`.split("\n")
11
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
12
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
13
- s.require_paths = ["lib"]
14
- #s.add_dependency 'name', 'version'
3
+ require "greentable/version"
15
4
 
16
- s.add_development_dependency 'rake'
17
- s.add_development_dependency 'testunit'
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "greentable"
7
+ spec.version = Greentable::VERSION
8
+ spec.authors = ["Wael Chatila"]
9
+ spec.description = "Greentable produces HTML tables from an array without you having to deal with any HTML elements."
10
+ spec.summary = "Rails declarative html tables with export features"
11
+ spec.homepage = "https://github.com/waelchatila/greentable"
12
+ spec.license = 'LGPLv3+'
13
+
14
+ spec.files = Dir["{app,config,db,lib}/**/*"] + ["Rakefile", "README.md"]
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ #spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.test_files = Dir["test/**/*"]
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rails", "~> 3.2.17"
22
+ spec.add_development_dependency "sqlite3"
23
+ spec.add_development_dependency 'test-unit'
24
+ spec.add_development_dependency "nokogiri"
25
+ spec.add_development_dependency "fastercsv"
26
+ spec.add_development_dependency "guard-test"
27
+ spec.add_development_dependency "rack-test"
28
+ #spec.add_development_dependency 'simplecov'
29
+ #spec.add_development_dependency 'coveralls'
18
30
  end
31
+
32
+
33
+
@@ -1,3 +1,6 @@
1
+ require 'csv'
2
+ require 'nokogiri'
3
+
1
4
  module Greentable
2
5
  class Export
3
6
  def initialize(app, *args, &block)
@@ -10,44 +13,51 @@ module Greentable
10
13
  end
11
14
 
12
15
  def _call(env)
13
- @status, @headers, @response = @app.call(env) rescue nil
14
- @ret = nil
15
- if env['QUERY_STRING'] =~ /greentable_export=([csv|rtf|xml]+)/i
16
+ status, headers, response = @app.call(env) rescue nil
17
+ greentable_export = (env['QUERY_STRING'] || '').scan(/greentable_export=([csv|print]+)/i)[0][0] rescue nil
18
+ if greentable_export
16
19
  request = Rack::Request.new(env)
17
20
  greentable_id = request.params['greentable_id']
18
21
  if greentable_id
19
- format = $1
20
- body = @response.respond_to?(:body) ? @response.body : @response.join
21
- doc = Hpricot(body.to_s)
22
- @ret= ""
23
- (doc/"##{greentable_id}//tr").each do |tr|
24
- row = []
25
- col = 0
26
- (tr/"/th | /td").each do |x|
27
- colspan = [(x.attributes['colspan'] || 1).to_i, 1].max
28
- row[col] = x.to_plain_text
29
- col += colspan
22
+ body = response.respond_to?(:body) ? response.body : response.join
23
+ doc = Nokogiri(body.to_s)
24
+ if greentable_export == 'csv'
25
+ ret= ""
26
+ (doc/"##{greentable_id}//tr").each do |tr|
27
+ row = []
28
+ col = 0
29
+ (tr/"./th | ./td").each do |x|
30
+ colspan = (x.attributes['colspan'] || 1).to_i
31
+ colspan = [colspan,1].max
32
+ row[col] = (x.inner_text || '').strip
33
+ col += colspan
34
+ end
35
+ CSV.generate(ret, :encoding => 'UTF-8'){ |csv| csv << row }
30
36
  end
31
- CSV.generate_row(row, row.length, @ret)
37
+ filename = request.params['greentable_export_filename'] || "export"
38
+ headers["Content-Length"] = ret.length.to_s
39
+ headers["Content-Type"] = "text/csv"
40
+ headers["Content-Disposition"] = "attachment; filename=#{filename}.#{greentable_export}"
41
+ headers.delete('ETag')
42
+ headers.delete('Cache-Control')
43
+ response = [ret]
44
+ elsif greentable_export == 'print'
45
+ table_node = doc.css("##{greentable_id}").remove
46
+ body = doc.css('body')[0]
47
+ js_print = "<script>window.onload = function() { window.focus(); window.print(); }</script>"
48
+ body.inner_html = table_node.to_html + js_print
49
+ response = [doc.to_html]
32
50
  end
33
-
34
- filename = request.params['greentable_export_filename'] || "export"
35
-
36
- @headers["Content-Length"] = @ret.length.to_s
37
- @headers["Content-Type"] = "text/csv"
38
- @headers["Content-Disposition"] = "attachment; filename=#{filename}.#{format}"
39
- @headers.delete('ETag')
40
- @headers.delete('Cache-Control')
41
51
  end
42
52
  end
43
53
 
44
- [@status, @headers, self]
54
+ [status, headers, response]
45
55
  end
46
56
 
47
- def each(&block)
48
- block.call(@ret) if @ret
49
- @response.each(&block)
50
- end
57
+ #def each(&block)
58
+ # block.call(ret) if ret
59
+ # response.each(&block)
60
+ #end
51
61
 
52
62
  end
53
63
  end
@@ -7,11 +7,11 @@ module Greentable
7
7
  def initialize(parent, records, opts)
8
8
  @parent = parent
9
9
  @records = records
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)
10
+ defaults = Greentable.configuration.defaults.dup rescue {}
11
+ @defaults_tr = deep_merge(defaults.delete(:tr), opts.delete(:tr))
12
+ @defaults_th = deep_merge(defaults.delete(:th), opts.delete(:th))
13
+ @defaults_td = deep_merge(defaults.delete(:td), opts.delete(:td))
14
+ @opts = deep_merge(defaults, opts)
15
15
 
16
16
  #rows
17
17
  @tr_attributes = []
@@ -32,8 +32,10 @@ module Greentable
32
32
 
33
33
  def col(th = nil, opts = {}, &block)
34
34
  @th_html[@current_col] = th
35
- @th_attributes[@current_col] = opts.delete(:th) || {}
36
- @td_attributes[@current_col] = opts
35
+ th_opts = opts.delete(:th) || {}
36
+ td_opts = opts.delete(:td) || {}
37
+ @th_attributes[@current_col] = deep_merge(th_opts, opts)
38
+ @td_attributes[@current_col] = deep_merge(td_opts, opts)
37
39
 
38
40
  @td_html[@row_counter.i] ||= []
39
41
  @td_html[@row_counter.i][@current_col] = @parent.capture(&block)
@@ -50,6 +52,8 @@ module Greentable
50
52
  block.call(self, record)
51
53
  @row_counter.inc
52
54
  end
55
+
56
+ self
53
57
  end
54
58
 
55
59
  def to_s
@@ -58,9 +62,9 @@ module Greentable
58
62
  ret << "<table#{do_attributes(nil,@opts)}>"
59
63
  unless @th_html.compact.empty?
60
64
  ret << "<thead>"
61
- ret << "<tr>"
65
+ ret << "<tr#{do_attributes(nil, @defaults_tr)}>"
62
66
  @th_html.each_with_index do |th,col|
63
- ret << "<th#{do_attributes(nil,@th_attributes[col])}>#{th.is_a?(Proc) ? th.call.to_s : th}</th>"
67
+ ret << "<th#{do_attributes(nil, deep_merge(@defaults_th, @th_attributes[col]))}>#{th.is_a?(Proc) ? th.call.to_s : th}</th>"
64
68
  end
65
69
  ret << "</tr>"
66
70
  ret << "</thead>"
@@ -68,14 +72,15 @@ module Greentable
68
72
  ret << "<tbody>"
69
73
 
70
74
  @row_counter.i.times do |row|
71
- ret << "<tr#{do_attributes(row,@defaults_tr.deep_merge(@tr_attributes[row]||{}))}>"
75
+ ret << "<tr#{do_attributes(row, deep_merge(@defaults_tr, @tr_attributes[row]))}>"
72
76
  @td_html[row].each_with_index do |td,col|
73
- ret << "<td#{do_attributes(row,@defaults_td.deep_merge(@td_attributes[col]||{}))}>#{td}</td>"
77
+ ret << "<td#{do_attributes(row, deep_merge(@defaults_td, @td_attributes[col]))}>#{td}</td>"
74
78
  end
75
79
  ret << "</tr>"
76
80
  end
77
81
  ret << "</tbody>"
78
82
  ret << "</table>"
83
+ ret.html_safe
79
84
  end
80
85
 
81
86
  private
@@ -87,5 +92,29 @@ module Greentable
87
92
  return ret
88
93
  end
89
94
 
95
+ def deep_merge(source_hash, specialized_hash)
96
+ deep_merge!((source_hash||{}).dup, (specialized_hash||{}).dup)
97
+ end
98
+
99
+ def deep_merge!(source_hash, specialized_hash)
100
+ #this code is originally from the gem hash-deep-merge, but has been modified slightly
101
+ specialized_hash.each_pair do |rkey, rval|
102
+ if source_hash.has_key?(rkey) then
103
+ lval = source_hash[rkey]
104
+ if rval.is_a?(Hash) and lval.is_a?(Hash) then
105
+ deep_merge(source_hash[rkey], rval)
106
+ elsif rval == source_hash[rkey] then
107
+ elsif rval.is_a?(String) and lval.is_a?(String)
108
+ source_hash[rkey] = "#{rval} #{lval}"
109
+ else
110
+ source_hash[rkey] = rval
111
+ end
112
+ else
113
+ source_hash[rkey] = rval
114
+ end
115
+ end
116
+ return source_hash
117
+ end
118
+
90
119
  end
91
120
  end
@@ -7,8 +7,8 @@ module Greentable
7
7
  ActionView::Base.send :include, ViewHelpers
8
8
  end
9
9
 
10
- #initializer "greentable.rackexport" do |app|
11
- # app.middleware.use Greentable::Export
12
- #end
10
+ initializer "greentable.rackexport" do |app|
11
+ app.middleware.use Greentable::Export
12
+ end
13
13
  end
14
14
  end
@@ -0,0 +1,3 @@
1
+ module Greentable
2
+ VERSION = "0.9.0"
3
+ end
@@ -5,7 +5,6 @@ module Greentable
5
5
  def greentable(records, opts = {}, &block)
6
6
  gt = Table.new(self,records, opts)
7
7
  gt.process(&block)
8
- gt.to_s.html_safe
9
8
  end
10
9
  end
11
10
  end
data/lib/greentable.rb CHANGED
@@ -1,5 +1,5 @@
1
-
2
1
  module Greentable
3
2
  require 'greentable/railtie' if defined?(Rails)
4
3
  require 'greentable/configuration'
4
+ require 'greentable/greentable_table'
5
5
  end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :greentable do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .