table_go 0.2.2 → 0.2.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 11ffd5d8824ce99aa33d4b2971ec89d32a450aff
4
- data.tar.gz: 4522febd5dfeceb72a8db1ad82702dd6b81d7e65
5
2
  SHA512:
6
- metadata.gz: 95e9071b51873d44a8f3296e0df969c99181f4dfaf0af5e821220128ea93da448ce49c47c16846788d4caf20691c9e5f20580b4149f0919960963d26ef92279d
7
- data.tar.gz: c618637e6020b59ff7cc0d2968826112b4b7562b3008ca3447264aec5c0741f698ef7ab115770f38a78ef6b4dba24b692ea7914062fe0b4440e7cdd18dc09dbd
3
+ metadata.gz: 98d1c35fd3e8d901ed141a60dae4c72feb84288f71e40953ccb6a509c923fdb0f4ac5069446a4df37d2a183e378a5fe38682999f4e290390552e49895c890477
4
+ data.tar.gz: 245dbc606d4525f4338fefc23b6cc7a8aaa9bbdf06f76a6c9cde4f89a998e66f99170b22e637e8951a02e0091705ccd19007f5b0c4f491bdfdd41010c7b002af
5
+ SHA1:
6
+ metadata.gz: fd07ac423cdbee16255d07adae5b52d3fbb0e705
7
+ data.tar.gz: 0eb4b6402e9df3792144845b1e4373aef069936f
data/README.md CHANGED
@@ -76,6 +76,30 @@ example in HAML
76
76
  - add sorting for column
77
77
 
78
78
 
79
+ ## Changelog
80
+
81
+ #### 0.2.3
82
+
83
+ - speed optimisation for haml templates
84
+ - allowing block style table options
85
+
86
+ #### 0.2.2
87
+
88
+ - small fixes
89
+ - tests
90
+
91
+ #### 0.2.1
92
+
93
+ - tests
94
+
95
+ #### 0.2.0
96
+
97
+ - allow multiline block level customization of columns
98
+
99
+ #### 0.1.9
100
+
101
+ - simple, flexible and fast html table generator
102
+
79
103
 
80
104
  ## Contributing
81
105
 
data/lib/table_go.rb CHANGED
@@ -2,7 +2,6 @@ require 'table_go/version'
2
2
 
3
3
  module TableGo
4
4
  autoload :Table, 'table_go/table'
5
- autoload :Columns, 'table_go/columns'
6
5
  autoload :Column, 'table_go/column'
7
6
  autoload :TableRenderer, 'table_go/table_renderer'
8
7
  autoload :Renderers, 'table_go/renderers'
@@ -19,11 +18,10 @@ module TableGo
19
18
  end
20
19
 
21
20
  def self.render(collection, model_klass, renderer_klass, template, options = {}, &block)
22
- table = Table.new(collection, model_klass, &block)
21
+ table = Table.new(collection, model_klass, options, &block)
23
22
  renderer = TableRenderer.new(table)
24
23
  renderer.renderer_klass = renderer_klass
25
24
  renderer.template = template
26
- renderer.apply_options(options)
27
25
  renderer.render_template
28
26
  end
29
27
 
@@ -64,7 +64,7 @@ module TableGo
64
64
  end
65
65
 
66
66
  def self.apply_send(formatter, record, column, value)
67
- value.send(formatter).to_s.html_safe
67
+ value.send(formatter)
68
68
  end
69
69
 
70
70
  end
@@ -6,9 +6,9 @@ module TableGo
6
6
 
7
7
  def render_template
8
8
  ::FasterCSV.generate(:col_sep => ";", :row_sep => "\n", :force_quotes => true, :quote_char => '"') do |csv|
9
- csv << source_table.columns.map { |column| label_for_column(column) }
10
- source_table.collection.each do |record|
11
- csv << source_table.columns.map do |column|
9
+ csv << table.columns.map { |column| label_for_column(column) }
10
+ table.collection.each do |record|
11
+ csv << table.columns.map do |column|
12
12
  value = value_from_record_by_column(record, column)
13
13
  apply_formatter(record, column, value)
14
14
  end
@@ -4,8 +4,8 @@ module TableGo
4
4
  include RendererBase
5
5
 
6
6
  def render_template
7
- content_tag(:table, table_html) do
8
- concat(content_tag(:caption, title)) if title
7
+ content_tag(:table, table.table_html) do
8
+ concat(content_tag(:caption, table.title)) if table.title
9
9
  concat(table_head)
10
10
  concat(table_body)
11
11
  end
@@ -14,7 +14,7 @@ module TableGo
14
14
  def table_head
15
15
  content_tag(:thead) do
16
16
  content_tag(:tr) do
17
- source_table.columns.each do |column|
17
+ table.columns.each do |column|
18
18
  concat(content_tag(:th, label_for_column(column), html_options_for_header(column)))
19
19
  end
20
20
  end
@@ -23,9 +23,9 @@ module TableGo
23
23
 
24
24
  def table_body
25
25
  content_tag(:tbody) do
26
- source_table.collection.each do |record|
26
+ table.collection.each do |record|
27
27
  tr = content_tag(:tr, html_options_for_row(record)) do
28
- source_table.columns.each do |column|
28
+ table.columns.each do |column|
29
29
  value = value_from_record_by_column(record, column)
30
30
  concat(content_tag(:td, apply_formatter(record, column, value), html_options_for_cell(record, column, value)))
31
31
  end
@@ -4,30 +4,21 @@ module TableGo
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
- attr_accessor :source_table, :title, :table_html, :row_html, :template
7
+ attr_accessor :table, :template
8
8
  delegate :content_tag, :concat, :to => :template
9
9
  end
10
10
 
11
-
12
- def apply_options(options)
13
- self.title = options.delete(:title)
14
- self.table_html = options.delete(:table_html)
15
- self.row_html = options.delete(:row_html)
16
- end
17
-
18
11
  def render_template
19
12
  raise ArgumentError.new('implement #render_template in concrete renderer')
20
13
  end
21
14
 
22
-
23
-
24
15
  private
25
16
 
26
17
  def label_for_column(column)
27
18
  column.label || begin
28
- if column.method && 1==3# reflection = source_table.model_klass.reflections[column.name]
19
+ if column.method && 1==3# reflection = table.model_klass.reflections[column.name]
29
20
  reflection.klass.human_attribute_name(column.method).html_safe
30
- # if column.method && reflection = source_table.model_klass.reflections[column.name]
21
+ # if column.method && reflection = table.model_klass.reflections[column.name]
31
22
  # reflection.klass.human_attribute_name(column.method).html_safe
32
23
  else
33
24
  column.human_attribute_name
@@ -45,7 +36,7 @@ module TableGo
45
36
 
46
37
  def html_options_for_row(record)
47
38
  {}.tap do |h|
48
- (row_html || {}).each do |k, v|
39
+ (table.row_html || {}).each do |k, v|
49
40
  h[k] = v.is_a?(Proc) ? v.call(record) : v
50
41
  end
51
42
  end
@@ -69,40 +60,131 @@ module TableGo
69
60
  end
70
61
 
71
62
  def apply_formatter(record, column, value)
72
- case
73
- when formatter = column.as
74
- Formatter.apply(formatter, record, column, value)
75
- when formatter = column.send
76
- Formatter.apply_send(formatter, record, column, value)
77
- when formatter = column.block
78
- apply_formatter_for_block(formatter, record, column, value)
79
- else
80
- value
81
- end
63
+ begin
64
+ case
65
+ when formatter = column.as
66
+ Formatter.apply(formatter, record, column, value)
67
+ when formatter = column.send
68
+ Formatter.apply_send(formatter, record, column, value)
69
+ when formatter = column.block
70
+ apply_formatter_for_block(formatter, record, column, value)
71
+ else
72
+ value
73
+ end
74
+ end.to_s.html_safe
82
75
  end
83
76
 
84
77
 
85
78
 
86
79
  def apply_formatter_for_block(formatter, record, column, value)
87
- s = nil
80
+ # template.capture { Formatter.apply(formatter, record, column, value )}
81
+ # template.capture_haml { Formatter.apply(formatter, record, column, value )}
82
+ # template.send(:capture_haml) { Formatter.apply(formatter, record, column, value )}
83
+ string = nil
88
84
  capture_view do
89
- s = Formatter.apply(formatter, record, column, value )
90
- end.presence || s # for compatibility to legacy haml "- t.column :ident"
85
+ string = Formatter.apply(formatter, record, column, value )
86
+ end.presence || string # for compatibility to legacy haml "- t.column :ident"
91
87
  end
92
88
 
93
- def capture_view(&proc)
94
- @capturer ||= template.is_haml? ? capture_haml : capture_action_view
95
- @capturer.call(proc)
96
- end
97
89
 
98
- def capture_haml(&proc)
99
- lambda { |c| template.capture_haml { c.call } }
90
+ def capture_view
91
+ template.is_haml? ? capture_haml { yield } : template.capture { yield }
100
92
  end
101
93
 
102
- def capture_action_view
103
- lambda { |c| template.capture { c.call } }
94
+ delegate :with_haml_buffer, :haml_buffer, :to => :template
95
+
96
+ # stripped down ripoff from Haml's capture_haml, needed for speed
97
+ def capture_haml(*args, &block)
98
+ # buffer = eval('if defined? _hamlout then _hamlout else nil end', block.binding) || haml_buffer
99
+ # buffer = haml_buffer
100
+ # with_haml_buffer(buffer) do
101
+ position = haml_buffer.buffer.length
102
+
103
+ haml_buffer.capture_position = position
104
+ block.call(*args)
105
+
106
+ # captured = haml_buffer.buffer.slice!(0..-1)
107
+ captured = haml_buffer.buffer.slice!(position..-1)
108
+ # return captured if haml_buffer.options[:ugly]
109
+
110
+ # debugger
111
+ # Note that the "reject" is needed for rbx 1.2.4, which includes empty
112
+ # strings in the returned array when splitting by /^/.
113
+ # captured = captured.split(/^/).reject {|x| x == ""}
114
+
115
+ # min_tabs = nil
116
+ # captured.each do |line|
117
+ # tabs = line.index(/[^ ]/) || line.length
118
+ # min_tabs ||= tabs
119
+ # min_tabs = min_tabs > tabs ? tabs : min_tabs
120
+ # end
121
+
122
+ # captured.map do |line|
123
+ # line.slice(min_tabs, line.length)
124
+ # end.join
125
+ # end
126
+ ensure
127
+ haml_buffer.capture_position = nil
104
128
  end
105
129
 
130
+
131
+ # def capture_haml(*args, &block)
132
+ # # buffer = eval('if defined? _hamlout then _hamlout else nil end', block.binding) || haml_buffer
133
+ # # buffer = haml_buffer
134
+ # # with_haml_buffer(buffer) do
135
+ # position = haml_buffer.buffer.length
136
+
137
+ # haml_buffer.capture_position = position
138
+ # block.call(*args)
139
+
140
+ # captured = haml_buffer.buffer.slice!(position..-1)
141
+ # # return captured if haml_buffer.options[:ugly]
142
+
143
+ # # debugger
144
+ # # Note that the "reject" is needed for rbx 1.2.4, which includes empty
145
+ # # strings in the returned array when splitting by /^/.
146
+ # captured = captured.split(/^/).reject {|x| x == ""}
147
+
148
+ # # min_tabs = nil
149
+ # # captured.each do |line|
150
+ # # tabs = line.index(/[^ ]/) || line.length
151
+ # # min_tabs ||= tabs
152
+ # # min_tabs = min_tabs > tabs ? tabs : min_tabs
153
+ # # end
154
+
155
+ # # captured.map do |line|
156
+ # # line.slice(min_tabs, line.length)
157
+ # # end.join
158
+ # # end
159
+ # ensure
160
+ # haml_buffer.capture_position = nil
161
+ # end
162
+
163
+ # def capture_view
164
+ # template.is_haml? ? template.capture_haml { yield } : template.capture { yield }
165
+ # end
166
+
167
+ # def capture_view
168
+ # template.send(capture_method) { yield }
169
+ # end
170
+
171
+ # def capture_method
172
+ # @capture_method ||= template.is_haml? ? :capture_haml : :capture
173
+ # end
174
+
175
+ # def capture_view(&proc)
176
+ # @capturer ||= template.is_haml? ? capture_haml : capture_action_view
177
+ # @capturer.call(proc)
178
+ # end
179
+
180
+ # def capture_haml(&proc)
181
+ # lambda { |c| template.capture_haml { c.call } }
182
+ # end
183
+
184
+ # def capture_action_view
185
+ # lambda { |c| template.capture { c.call } }
186
+ # end
187
+
106
188
  end
107
189
  end
108
190
  end
@@ -1,21 +1,24 @@
1
1
  module TableGo
2
2
  class Table
3
3
 
4
- attr_accessor :collection, :model_klass, :columns
4
+ attr_accessor :collection, :model_klass
5
+ attr_accessor :columns
5
6
 
6
- def initialize(collection, model_klass, &block)
7
+ def initialize(collection, model_klass, options, &block)
7
8
  @collection = collection
8
9
  @model_klass = model_klass
9
- @columns = Columns.new(self)
10
- evaluate_dsl(block)
10
+ @columns = []
11
+ apply_options!(options)
12
+ evaluate_dsl!(block)
11
13
  end
12
14
 
13
- def evaluate_dsl(block)
15
+ def evaluate_dsl!(block)
14
16
  if block
15
- block.call(@columns)
17
+ # instance_eval(&block)
18
+ block.call(self)
16
19
  else
17
20
  attribute_names_from_model_klass.each do |column_name|
18
- @columns.column(column_name)
21
+ column(column_name)
19
22
  end
20
23
  end
21
24
  end
@@ -24,9 +27,35 @@ module TableGo
24
27
  # @model_klass_reflection_keys ||= model_klass.reflections.keys
25
28
  # end
26
29
 
30
+
27
31
  def attribute_names_from_model_klass
28
32
  model_klass.respond_to?(:column_names) ? model_klass.column_names : []
29
33
  end
30
34
 
35
+ def apply_options!(options)
36
+ options.each { |k, v| send(k, v) }
37
+ end
38
+
39
+
40
+
41
+ def column(name, options = {}, &block)
42
+ @columns << Column.new(self, name, options, &block)
43
+ end
44
+
45
+ def title(title = nil)
46
+ @title = title if title
47
+ @title
48
+ end
49
+
50
+ def table_html(table_html = nil)
51
+ @table_html = table_html if table_html
52
+ @table_html
53
+ end
54
+
55
+ def row_html(row_html = nil)
56
+ @row_html = row_html if row_html
57
+ @row_html
58
+ end
59
+
31
60
  end
32
61
  end
@@ -1,16 +1,16 @@
1
1
  module TableGo
2
2
  class TableRenderer
3
3
  attr_accessor :renderer_klass, :template
4
- delegate :apply_options, :render_template, :to => :renderer
4
+ delegate :render_template, :to => :renderer
5
5
 
6
- def initialize(source_table)
7
- @source_table = source_table
6
+ def initialize(table)
7
+ @table = table
8
8
  end
9
9
 
10
10
  def renderer
11
11
  @renderer ||= renderer_klass.new.tap do |r|
12
- r.template = template
13
- r.source_table = @source_table
12
+ r.template = template
13
+ r.table = @table
14
14
  end
15
15
  end
16
16
 
@@ -1,3 +1,3 @@
1
1
  module TableGo
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
@@ -1,3 +1,4 @@
1
+ %p Suppe
1
2
  = table_go_for(articles, Article) do |t|
2
3
  - t.column :ident
3
4
  - t.column :custom_single_cell do |value, record, column|
@@ -7,3 +8,8 @@
7
8
  = " Title: %s" % record.title
8
9
  - t.column :custom_single_cell_with_backwards_compatibility do |value, record, column|
9
10
  - "Ident: %s" % record.ident
11
+ - t.column :field_with_html_markup, :label => 'field with_html_markup' do |value, record, column|
12
+ = link_to('click me', 'http://nowhere.com')
13
+ %br
14
+ = link_to('and here', 'http://otherwhere.com')
15
+ %p Pampe
@@ -23,9 +23,9 @@ describe TableGo::Helpers do
23
23
  Haml::Engine.new(read_file_from_fixtures_path('simple_table.html.haml')).render(template, :articles => articles)
24
24
  end
25
25
 
26
-
27
26
  it "it should render in haml" do
28
- subject.cleanup_html.should == %Q(
27
+ subject.cleanup_html.should eql %Q(
28
+ <p>Suppe</p>
29
29
  <table>
30
30
  <thead>
31
31
  <tr>
@@ -33,6 +33,7 @@ describe TableGo::Helpers do
33
33
  <th>Custom single cell</th>
34
34
  <th>Custom multiline cell</th>
35
35
  <th>Custom single cell with backwards compatibility</th>
36
+ <th>field with_html_markup</th>
36
37
  </tr></thead>
37
38
  <tbody>
38
39
  <tr>
@@ -40,20 +41,38 @@ describe TableGo::Helpers do
40
41
  <td>Ident: 1 - Title: iPutz</td>
41
42
  <td>Ident: 1 - Title: iPutz</td>
42
43
  <td>Ident: 1</td>
44
+ <td><a href="http://nowhere.com">click me</a><br /><a href="http://otherwhere.com">and here</a></td>
43
45
  </tr>
44
46
  <tr>
45
47
  <td>2</td>
46
48
  <td>Ident: 2 - Title: Nutzbook</td>
47
49
  <td>Ident: 2 - Title: Nutzbook</td>
48
50
  <td>Ident: 2</td>
51
+ <td><a href="http://nowhere.com">click me</a><br /><a href="http://otherwhere.com">and here</a></td>
49
52
  </tr>
50
53
  </tbody>
51
54
  </table>
55
+ <p>Pampe</p>
52
56
  ).cleanup_html
53
57
  end
54
58
 
55
59
 
56
60
  end
57
61
 
62
+ # context 'speedtest' do
63
+
64
+ # let(:more_articles) { 1000.times.map { articles }.flatten }
65
+
66
+
67
+ # it 'should run fast' do
68
+ # puts Benchmark.measure {
69
+ # 2.times do
70
+ # Haml::Engine.new(read_file_from_fixtures_path('simple_table.html.haml')).render(template, :articles => more_articles)
71
+ # end
72
+ # }
73
+ # end
74
+
75
+ # end
76
+
58
77
  end
59
78
 
@@ -20,7 +20,7 @@ describe TableGo::Renderers::HtmlRenderer do
20
20
  subject { TableGo.render_html(articles, Article, template, {}) }
21
21
 
22
22
  it 'should render a simple automatic html table' do
23
- subject.cleanup_html.should == %Q(
23
+ subject.cleanup_html.should eql %Q(
24
24
  <table>
25
25
  <thead>
26
26
  <tr>
@@ -96,7 +96,7 @@ describe TableGo::Renderers::HtmlRenderer do
96
96
  t.column :info_text,
97
97
  :label => 'with block level custom formatter' do |value, record, column|
98
98
 
99
- "a special<br/>value"
99
+ "a special<br/>value"
100
100
 
101
101
  end
102
102
 
@@ -114,7 +114,7 @@ describe TableGo::Renderers::HtmlRenderer do
114
114
  end
115
115
 
116
116
  it 'should render a html table', 'with custom attributes' do
117
- subject.cleanup_html.should == %Q(
117
+ subject.cleanup_html.should eql %Q(
118
118
  <table id="articles">
119
119
  <caption>one Table</caption>
120
120
  <thead>
@@ -157,10 +157,39 @@ describe TableGo::Renderers::HtmlRenderer do
157
157
  </table>
158
158
  ).cleanup_html
159
159
  end
160
+ end
160
161
 
161
162
 
162
- end
163
+ describe "block style options" do
164
+ let(:table_with_hash_options) do
165
+ TableGo.render_html(articles, Article, template,
166
+ :title => 'one Table',
167
+ :table_html => { :id => :articles },
168
+ :row_html => { :class => :row_css_class,
169
+ :id => lambda { |record| "row_#{record.ident}" }}) do |t|
170
+ t.column :ident,
171
+ :column_html => { :class => lambda { |record, column, value| value.even? ? :even : :odd } }
172
+ end
173
+ end
163
174
 
175
+ subject(:table_with_block_options) do
176
+ TableGo.render_html(articles, Article, template) do |t|
177
+ t.title 'one Table'
178
+ t.table_html :id => :articles
179
+ t.row_html :class => :row_css_class,
180
+ :id => lambda { |record| "row_#{record.ident}" }
181
+
182
+ t.column :ident,
183
+ :column_html => { :class => lambda { |record, column, value| value.even? ? :even : :odd } }
184
+ end
185
+ end
186
+
187
+
188
+ it "should render the same way as with hash style options" do
189
+ subject.should eql table_with_hash_options
190
+ end
191
+
192
+ end
164
193
 
165
194
  end
166
195
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_go
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lars Gollnow
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2013-09-27 00:00:00 Z
13
+ date: 2013-10-09 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: actionpack
@@ -67,7 +67,6 @@ files:
67
67
  - Rakefile
68
68
  - lib/table_go.rb
69
69
  - lib/table_go/column.rb
70
- - lib/table_go/columns.rb
71
70
  - lib/table_go/formatter.rb
72
71
  - lib/table_go/helpers.rb
73
72
  - lib/table_go/railtie.rb
@@ -80,7 +79,7 @@ files:
80
79
  - lib/table_go/version.rb
81
80
  - spec/fixtures/simple_table.html.haml
82
81
  - spec/spec_helper.rb
83
- - spec/table_go/helper_spec.rb
82
+ - spec/table_go/helpers_spec.rb
84
83
  - spec/table_go/renderers/csv_renderer_spec.rb
85
84
  - spec/table_go/renderers/html_renderer_spec.rb
86
85
  - table_go.gemspec
@@ -110,6 +109,6 @@ summary: ""
110
109
  test_files:
111
110
  - spec/fixtures/simple_table.html.haml
112
111
  - spec/spec_helper.rb
113
- - spec/table_go/helper_spec.rb
112
+ - spec/table_go/helpers_spec.rb
114
113
  - spec/table_go/renderers/csv_renderer_spec.rb
115
114
  - spec/table_go/renderers/html_renderer_spec.rb
@@ -1,19 +0,0 @@
1
- module TableGo
2
- class Columns
3
- include Enumerable
4
- attr_accessor :table
5
-
6
- def initialize(table)
7
- @table, @columns = table, []
8
- end
9
-
10
- def column(name, options = {}, &block)
11
- @columns << Column.new(table, name, options, &block)
12
- end
13
-
14
- def each(&block)
15
- @columns.each(&block)
16
- end
17
-
18
- end
19
- end