greentable 0.9.1 → 0.9.2

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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- greentable (0.9.1)
4
+ greentable (0.9.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Guardfile CHANGED
@@ -2,6 +2,7 @@
2
2
  # More info at https://github.com/guard/guard#readme
3
3
 
4
4
  guard :test, :drb => false, :all_on_start => true do
5
+ watch(%r{^lib/greentable/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
5
6
  watch(%r{^test/.+_test\.rb$})
6
7
  end
7
8
 
data/README.md CHANGED
@@ -78,6 +78,8 @@ will produce
78
78
 
79
79
  A comprehensive example:
80
80
 
81
+ *note how gt.col(class: 'h') below is applied to both TH and TD elements*
82
+
81
83
  ```haml
82
84
  =greentable(array, class: 'a aa', style: 'b:c', tr: {class: 'd'}, th: {onclick: 'e();'}, td: {class: 'f', 'data-target' => 'g'} ) do |gt, el|
83
85
  -gt.col('col0', class: 'h', th: {class: 'i'}) do
@@ -122,10 +124,10 @@ will produce
122
124
  <table>
123
125
  <thead>
124
126
  <tr>
125
- <th>Year</th>
126
- <th>An even week</th>
127
- <th>An odd week</th>
128
- <th>An even week</th>
127
+ <th>Year</th>
128
+ <th>An even week</th>
129
+ <th>An odd week</th>
130
+ <th>An even week</th>
129
131
  </tr>
130
132
  </thead>
131
133
  <tbody>
@@ -148,6 +150,109 @@ will produce
148
150
  </table>
149
151
  ```
150
152
 
153
+ ## Counter Object
154
+ Sometimes, you'll need to know what row you're currently on. For that purpose Greentable exposes a counter object:
155
+
156
+ ```haml
157
+ =greentable([x,y,z]) do |gt, el|
158
+ -gt.col('i') do
159
+ =gt.counter.i
160
+ -gt.col('first?') do
161
+ =gt.counter.first?
162
+ -gt.col('last?') do
163
+ =gt.counter.last?
164
+ -gt.col('odd?') do
165
+ =gt.counter.odd?
166
+ -gt.col('even?') do
167
+ =gt.counter.even?
168
+ ```
169
+
170
+ will produce
171
+
172
+ ```html
173
+ <table>
174
+ <thead>
175
+ <tr>
176
+ <th>i</th>
177
+ <th>first?</th>
178
+ <th>last?</th>
179
+ <th>odd?</th>
180
+ <th>even?</th>
181
+ </tr>
182
+ </thead>
183
+ <tbody>
184
+ <tr>
185
+ <td>0</td>
186
+ <td>true</td>
187
+ <td>false</td>
188
+ <td>true</td>
189
+ <td>false</td>
190
+ </tr>
191
+ <tr>
192
+ <td>1</td>
193
+ <td>false</td>
194
+ <td>false</td>
195
+ <td>false</td>
196
+ <td>true</td>
197
+ </tr>
198
+ <tr>
199
+ <td>2</td>
200
+ <td>false</td>
201
+ <td>true</td>
202
+ <td>true</td>
203
+ <td>false</td>
204
+ </tr>
205
+ </tbody>
206
+ </table>
207
+ ```
208
+
209
+
210
+ ## Greentable Footer
211
+
212
+ Sometime, you'll need to display a total (or whathaveyou) at the end:
213
+
214
+ ```haml
215
+ =greentable(products) do |gt, product|
216
+ -gt.footer(class: 'a', style: 'border-top: 2px solid black;', tr: {class: 'footer_tr_class'}) do |footer|
217
+ -footer.col do
218
+ Total
219
+ -footer.col(style: 'font-weight: bold;') do
220
+ $3.14
221
+
222
+ -gt.col('name') do
223
+ =product
224
+ -gt.col('price') do
225
+ =product.price
226
+ ```
227
+
228
+ will produce
229
+
230
+ ```html
231
+ <table>
232
+ <thead>
233
+ <tr>
234
+ <th>name</th>
235
+ <th>price</th>
236
+ </tr>
237
+ </thead>
238
+ <tbody>
239
+ <tr>
240
+ <td>ProductA</td>
241
+ <td>$2.11</td>
242
+ </tr>
243
+ <tr>
244
+ <td>ProductB</td>
245
+ <td>$1.03</td>
246
+ </tr>
247
+ </tbody>
248
+ <tfoot>
249
+ <tr class='footer_tr_class'>
250
+ <td class='a' style: 'border-top: 2px solid black;'>Total</td>
251
+ <td class='a' style='border-top: 2px solid black; font-weight: bold;'>$3.14</td>
252
+ </tr>
253
+ </tfoot>
254
+ </table>
255
+ ```
151
256
 
152
257
  ## Global Defaults
153
258
  You can configure global defaults for all your greentables.
@@ -7,6 +7,8 @@ module Greentable
7
7
  def initialize(parent, records, opts)
8
8
  @parent = parent
9
9
  @records = records
10
+ @row_counter = Counter.new(@records.size)
11
+
10
12
  defaults = Greentable.configuration.defaults.dup rescue {}
11
13
  @defaults_tr = deep_merge(defaults.delete(:tr), opts.delete(:tr))
12
14
  @defaults_th = deep_merge(defaults.delete(:th), opts.delete(:th))
@@ -22,20 +24,19 @@ module Greentable
22
24
  @td_attributes = []
23
25
  @td_html = []
24
26
 
27
+ @tfoot = ""
25
28
  end
26
29
 
27
- def subrow(opts = {}, &block)
28
- return if capture_headers
29
- return if opts[:display_on] == :first && !@row_counter.first?
30
- return if opts[:display_on] == :last && !@row_counter.last?
30
+ def counter
31
+ @row_counter
31
32
  end
32
33
 
33
34
  def col(th = nil, opts = {}, &block)
34
35
  @th_html[@current_col] = th
35
36
  th_opts = opts.delete(:th) || {}
36
37
  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)
38
+ @th_attributes[@current_col] = deep_merge(opts, th_opts)
39
+ @td_attributes[@current_col] = deep_merge(opts, td_opts)
39
40
 
40
41
  @td_html[@row_counter.i] ||= []
41
42
  @td_html[@row_counter.i][@current_col] = @parent.capture(&block)
@@ -44,15 +45,22 @@ module Greentable
44
45
  return nil
45
46
  end
46
47
 
47
- def process(&block)
48
- @row_counter = Counter.new(@records.size)
48
+ def footer(opts = {}, &block)
49
+ return unless @tfoot.empty?
50
+ @tfoot << '<tfoot>'
51
+ tr_opts = deep_merge(@defaults_tr, opts.delete(:tr))
52
+ td_opts = deep_merge(opts, opts.delete(:td))
53
+ td_opts = deep_merge(@defaults_td, td_opts)
54
+ TableGroup.new(@parent, [nil], {td: td_opts, tr: tr_opts}).process(&block).send(:to_s_tbody_content, @tfoot)
55
+ @tfoot << '</tfoot>'
56
+ end
49
57
 
58
+ def process(&block)
50
59
  @records.each do |record|
51
60
  @current_col = 0
52
61
  block.call(self, record)
53
62
  @row_counter.inc
54
63
  end
55
-
56
64
  self
57
65
  end
58
66
 
@@ -60,6 +68,24 @@ module Greentable
60
68
  ret = ""
61
69
  return ret if @td_html.empty?
62
70
  ret << "<table#{do_attributes(nil,@opts)}>"
71
+ to_s_thead_content(ret)
72
+ ret << "<tbody>"
73
+ to_s_tbody_content(ret)
74
+ ret << "</tbody>"
75
+ ret << @tfoot
76
+ ret << "</table>"
77
+ ret.html_safe
78
+ end
79
+
80
+ class TableGroup < Table
81
+ def col(th = nil, opts = {}, &block)
82
+ super(nil, th || {}, &block)
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def to_s_thead_content(ret)
63
89
  unless @th_html.compact.empty?
64
90
  ret << "<thead>"
65
91
  ret << "<tr#{do_attributes(nil, @defaults_tr)}>"
@@ -69,22 +95,19 @@ module Greentable
69
95
  ret << "</tr>"
70
96
  ret << "</thead>"
71
97
  end
72
- ret << "<tbody>"
98
+ end
73
99
 
100
+ def to_s_tbody_content(ret)
74
101
  @row_counter.i.times do |row|
75
102
  ret << "<tr#{do_attributes(row, deep_merge(@defaults_tr, @tr_attributes[row]))}>"
76
- @td_html[row].each_with_index do |td,col|
103
+ @td_html[row].each_with_index do |td, col|
77
104
  ret << "<td#{do_attributes(row, deep_merge(@defaults_td, @td_attributes[col]))}>#{td}</td>"
78
105
  end
79
106
  ret << "</tr>"
80
107
  end
81
- ret << "</tbody>"
82
- ret << "</table>"
83
- ret.html_safe
84
108
  end
85
109
 
86
- private
87
- def do_attributes(i,o)
110
+ def do_attributes(i, o)
88
111
  instance = i.nil? ? nil : @records[i]
89
112
  return "" if o.nil? || o.empty?
90
113
  ret = o.map{|k,v| "#{k.is_a?(Proc) ? instance.instance_eval(&k).to_s : k.to_s}=\"#{v.is_a?(Proc) ? instance.instance_eval(&v).to_s : v.to_s}\""}.join(" ").strip
@@ -105,7 +128,7 @@ module Greentable
105
128
  deep_merge(source_hash[rkey], rval)
106
129
  elsif rval == source_hash[rkey] then
107
130
  elsif rval.is_a?(String) and lval.is_a?(String)
108
- source_hash[rkey] = "#{rval} #{lval}"
131
+ source_hash[rkey] = "#{lval} #{rval}"
109
132
  else
110
133
  source_hash[rkey] = rval
111
134
  end
@@ -1,3 +1,3 @@
1
1
  module Greentable
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  end
@@ -3,7 +3,7 @@ require 'greentable/greentable_table'
3
3
  module Greentable
4
4
  module ViewHelpers
5
5
  def greentable(records, opts = {}, &block)
6
- gt = Table.new(self,records, opts)
6
+ gt = Table.new(self, records, opts)
7
7
  gt.process(&block)
8
8
  end
9
9
  end
@@ -5,12 +5,32 @@ hello world
5
5
  <a href="/?greentable_export=csv&greentable_id=greentable_id">Export CSV</a>
6
6
  |
7
7
  <a href="/?greentable_export=print&greentable_id=greentable_id">Print</a>
8
- <%= greentable([0], :id => 'greentable_id') do |gt, el| %>
9
- <% gt.col('i') do %>
8
+ <%= greentable([100, 101, 102], :id => 'greentable_id') do |gt, el| %>
9
+ <% gt.footer(style: 'border-top: 2px solid red;') do |footer| %>
10
+ <% footer.col(style: 'background-color: #ccc;') do %>
11
+ footer td
12
+ <% end %>
13
+ <% end %>
14
+ <% gt.col('x') do %>
10
15
  <%= el %>
11
16
  <% end %>
12
- <% gt.col('i+1') do %>
17
+ <% gt.col('x+1') do %>
13
18
  <%= el + 1 %>
14
19
  <% end %>
20
+ <% gt.col('i') do %>
21
+ <%= gt.counter.i %>
22
+ <% end %>
23
+ <% gt.col('first?') do %>
24
+ <%= gt.counter.first? %>
25
+ <% end %>
26
+ <% gt.col('last?') do %>
27
+ <%= gt.counter.last? %>
28
+ <% end %>
29
+ <% gt.col('odd?') do %>
30
+ <%= gt.counter.odd? %>
31
+ <% end %>
32
+ <% gt.col('even?') do %>
33
+ <%= gt.counter.even? %>
34
+ <% end %>
15
35
  <% end %>
16
36
 
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class GreentableTest < ActiveSupport::TestCase
3
+ class GreentableTableTest < ActiveSupport::TestCase
4
4
 
5
5
  class String
6
6
  def html_safe
@@ -72,7 +72,7 @@ class GreentableTest < ActiveSupport::TestCase
72
72
  x
73
73
  end
74
74
  end
75
- assert_equal '<table><thead><tr><th class="proc1">proc0</th></tr></thead><tbody><tr><td style="proc2" class="proc1">0</td></tr></tbody></table>', gt.to_s
75
+ assert_equal '<table><thead><tr><th class="proc1">proc0</th></tr></thead><tbody><tr><td class="proc1" style="proc2">0</td></tr></tbody></table>', gt.to_s
76
76
  end
77
77
 
78
78
  test "table_opts_comprehensive" do
@@ -85,7 +85,7 @@ class GreentableTest < ActiveSupport::TestCase
85
85
  x
86
86
  end
87
87
  end
88
- assert_equal '<table class="a aa" style="b:c"><thead><tr class="d"><th onclick="e();" class="h i ee">col0</th><th onclick="e();" class="ee">col1</th></tr></thead><tbody><tr class="d"><td data-target="f" class="h ff">0</td><td data-target="f" class="j ff">0</td></tr></tbody></table>', gt.to_s
88
+ assert_equal '<table class="a aa" style="b:c"><thead><tr class="d"><th onclick="e();" class="ee h i">col0</th><th onclick="e();" class="ee">col1</th></tr></thead><tbody><tr class="d"><td data-target="f" class="ff h">0</td><td data-target="f" class="ff j">0</td></tr></tbody></table>', gt.to_s
89
89
  end
90
90
 
91
91
  test "global configuration" do
@@ -106,4 +106,76 @@ class GreentableTest < ActiveSupport::TestCase
106
106
  end
107
107
  end
108
108
  end
109
+
110
+ test "footer - outside process" do
111
+ gt = Greentable::Table.new(self,[1, 2], td: {class: 'a'})
112
+ gt.footer(class: 'b', :td => {:class => 'c'}) do |footer|
113
+ footer.col(colspan: 2) do
114
+ 'total'
115
+ end
116
+ footer.col do
117
+ 3
118
+ end
119
+ end
120
+ gt.process do |gt, x|
121
+ gt.col('name') do
122
+ "name#{x}"
123
+ end
124
+ gt.col('price') do
125
+ x
126
+ end
127
+ end
128
+ assert_equal "<table><thead><tr><th>name</th><th>price</th></tr></thead><tbody><tr><td class=\"a\">name1</td><td class=\"a\">1</td></tr><tr><td class=\"a\">name2</td><td class=\"a\">2</td></tr></tbody><tfoot><tr><td class=\"a b c\" colspan=\"2\">total</td><td class=\"a b c\">3</td></tr></tfoot></table>", gt.to_s
129
+ end
130
+
131
+ test "footer - inside process" do
132
+ gt = Greentable::Table.new(self, [0], {})
133
+ gt.process do |gt, x|
134
+ gt.footer(style: 'a', :tr => {:class => 'tr_class'}) do |footer|
135
+ footer.col(style: 'b') do
136
+ 'peace'
137
+ end
138
+ footer.col(style: 'c') do
139
+ 'out'
140
+ end
141
+ end
142
+ gt.col('col0') do
143
+ x
144
+ end
145
+ end
146
+ assert_equal "<table><thead><tr><th>col0</th></tr></thead><tbody><tr><td>0</td></tr></tbody><tfoot><tr class=\"tr_class\"><td style=\"a b\">peace</td><td style=\"a c\">out</td></tr></tfoot></table>", gt.to_s
147
+ end
148
+
149
+ test "row_counter" do
150
+ gt = Greentable::Table.new(self, [0, 1, 2], {})
151
+ gt.process do |gt, x|
152
+ gt.col do
153
+ case x
154
+ when 0
155
+ assert_equal "0", gt.counter.to_s
156
+ assert_equal 0, gt.counter.i
157
+ assert gt.counter.first?
158
+ refute gt.counter.last?
159
+ assert gt.counter.odd?
160
+ refute gt.counter.even?
161
+ when 1
162
+ assert_equal "1", gt.counter.to_s
163
+ assert_equal 1, gt.counter.i
164
+ refute gt.counter.first?
165
+ refute gt.counter.last?
166
+ refute gt.counter.odd?
167
+ assert gt.counter.even?
168
+ when 2
169
+ assert_equal "2", gt.counter.to_s
170
+ assert_equal 2, gt.counter.i
171
+ refute gt.counter.first?
172
+ assert gt.counter.last?
173
+ assert gt.counter.odd?
174
+ refute gt.counter.even?
175
+ else
176
+ fail("should not come here. x=#{x}")
177
+ end
178
+ end
179
+ end
180
+ end
109
181
  end
@@ -1,7 +1,8 @@
1
- require 'simplecov'
2
- SimpleCov.start('rails')
1
+ unless ARGV.any? {|e| e =~ /guard/ }
2
+ require 'simplecov'
3
+ SimpleCov.start('rails')
4
+ end
3
5
 
4
- # Configure Rails Environment
5
6
  ENV["RAILS_ENV"] = "test"
6
7
 
7
8
  require File.expand_path("../dummy/config/environment.rb", __FILE__)
@@ -9,18 +10,12 @@ require "rails/test_help"
9
10
 
10
11
  Rails.backtrace_cleaner.remove_silencers!
11
12
 
12
- # Load support files
13
- #Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
14
-
15
- # Load fixtures from the engine
16
- #if ActiveSupport::TestCase.method_defined?(:fixture_path=)
17
- # ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
18
- #end
19
-
20
-
21
13
  require 'greentable'
22
- require 'coveralls'
23
- Coveralls.wear!
14
+
15
+ unless ARGV.any? {|e| e =~ /guard/ }
16
+ require 'coveralls'
17
+ Coveralls.wear!
18
+ end
24
19
 
25
20
  public
26
21
 
data/todo.txt CHANGED
@@ -6,4 +6,6 @@
6
6
  greentable_id
7
7
  greentable_export_filename
8
8
  * assets/greentable.css
9
-
9
+ * add table caption support
10
+ * add colgroup support
11
+ * add col support
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.9.1
4
+ version: 0.9.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -216,7 +216,7 @@ files:
216
216
  - test/dummy/public/favicon.ico
217
217
  - test/dummy/script/rails
218
218
  - test/export_test.rb
219
- - test/greentable_test.rb
219
+ - test/greentable_table_test.rb
220
220
  - test/test_helper.rb
221
221
  - todo.txt
222
222
  - test/dummy/log/development.log
@@ -247,7 +247,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
247
247
  version: '0'
248
248
  segments:
249
249
  - 0
250
- hash: -3809614129295099888
250
+ hash: 2946809728237935580
251
251
  required_rubygems_version: !ruby/object:Gem::Requirement
252
252
  none: false
253
253
  requirements:
@@ -256,7 +256,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
256
256
  version: '0'
257
257
  segments:
258
258
  - 0
259
- hash: -3809614129295099888
259
+ hash: 2946809728237935580
260
260
  requirements: []
261
261
  rubyforge_project:
262
262
  rubygems_version: 1.8.25
@@ -307,6 +307,6 @@ test_files:
307
307
  - test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af
308
308
  - test/dummy/tmp/cache/assets/E21/3E0/sprockets%2Ff9530da0d9bbb15351aef3eb378bddbc
309
309
  - test/export_test.rb
310
- - test/greentable_test.rb
310
+ - test/greentable_table_test.rb
311
311
  - test/test.iml
312
312
  - test/test_helper.rb