table_go 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- data.tar.gz: 466df5bf94ac3a8efe5fe4c6d65d5e8ff45c5218
4
- metadata.gz: 9b876f90007f7dde16692e8bb973b3115ed37966
5
2
  SHA512:
6
- data.tar.gz: 077564168872054f188cb8a5b7ba79a5eb424de627fcb6a522f342a1757e9b065af699e2a7862ef260bb0f85764241ae3df10bbf13de9f98ff2dcd30928a75fd
7
- metadata.gz: 501d9814cbea4d8cc017b570e38ff3cf6784f0b4c3b02cc0ee67005b7c6168c16ce82750c5027d7e5ac7f7b6c7c35b5367e386182ea4b8210b76351a5f5e6de6
3
+ data.tar.gz: 481cf5ca22106044d461a815a0ef99d2ce8ee3b520344a706bb820ecd73aa257da15d27626ac094779d151d4b8d0bd1bb39db1d6df76658bad2ec536bb11fb14
4
+ metadata.gz: f197dd914b380fb7b8aec24e85a799d78a8763e6f20f724ab8087267e46d9a728e775f2b4b2441772c83fa4327622ac7d6a9ed77724a9773f67274dd9bfb6f0a
5
+ SHA1:
6
+ data.tar.gz: 634fdc3e6767ebdd65407ea8bcc0c687cd867911
7
+ metadata.gz: b72b3f7fe7cbe432565be223b5c454714fd89a04
data/.rspec CHANGED
@@ -1 +1 @@
1
- --format documentation --color --fail-fast
1
+ --format documentation --color
data/Gemfile CHANGED
@@ -1,17 +1,11 @@
1
1
  source 'http://rubygems.org'
2
- gem 'rails'
2
+ gem 'actionpack'
3
3
  gem 'fastercsv'
4
4
 
5
5
  group :development, :test do
6
- # gem 'sqlite3-ruby'
7
- gem 'rspec-rails'
8
- #gem 'pry-rails'
9
- #gem 'ruby-debug'
6
+ gem 'rspec'
7
+ gem 'haml'
8
+ gem 'haml-rails'
9
+ # gem 'pry-rails'
10
+ gem 'ruby-debug'
10
11
  end
11
-
12
- group :test do
13
- end
14
-
15
-
16
- # Specify your gem's dependencies in table_go.gemspec
17
- # gemspec
data/Gemfile.lock CHANGED
@@ -1,9 +1,6 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- actionmailer (3.2.9)
5
- actionpack (= 3.2.9)
6
- mail (~> 2.4.4)
7
4
  actionpack (3.2.9)
8
5
  activemodel (= 3.2.9)
9
6
  activesupport (= 3.2.9)
@@ -17,33 +14,27 @@ GEM
17
14
  activemodel (3.2.9)
18
15
  activesupport (= 3.2.9)
19
16
  builder (~> 3.0.0)
20
- activerecord (3.2.9)
21
- activemodel (= 3.2.9)
22
- activesupport (= 3.2.9)
23
- arel (~> 3.0.2)
24
- tzinfo (~> 0.3.29)
25
- activeresource (3.2.9)
26
- activemodel (= 3.2.9)
27
- activesupport (= 3.2.9)
28
17
  activesupport (3.2.9)
29
18
  i18n (~> 0.6)
30
19
  multi_json (~> 1.0)
31
- arel (3.0.2)
32
20
  builder (3.0.4)
33
- diff-lcs (1.1.3)
21
+ columnize (0.3.4)
22
+ diff-lcs (1.2.1)
34
23
  erubis (2.7.0)
35
24
  fastercsv (1.5.4)
25
+ haml (3.1.3)
26
+ haml-rails (0.3.4)
27
+ actionpack (~> 3.0)
28
+ activesupport (~> 3.0)
29
+ haml (~> 3.0)
30
+ railties (~> 3.0)
36
31
  hike (1.2.1)
37
32
  i18n (0.6.1)
38
33
  journey (1.0.4)
39
34
  json (1.7.5)
40
- mail (2.4.4)
41
- i18n (>= 0.4.0)
42
- mime-types (~> 1.16)
43
- treetop (~> 1.4.8)
44
- mime-types (1.19)
35
+ linecache (0.46)
36
+ rbx-require-relative (> 0.0.4)
45
37
  multi_json (1.4.0)
46
- polyglot (0.3.3)
47
38
  rack (1.4.1)
48
39
  rack-cache (1.2)
49
40
  rack (>= 0.4)
@@ -51,14 +42,6 @@ GEM
51
42
  rack
52
43
  rack-test (0.6.2)
53
44
  rack (>= 1.0)
54
- rails (3.2.9)
55
- actionmailer (= 3.2.9)
56
- actionpack (= 3.2.9)
57
- activerecord (= 3.2.9)
58
- activeresource (= 3.2.9)
59
- activesupport (= 3.2.9)
60
- bundler (~> 1.0)
61
- railties (= 3.2.9)
62
45
  railties (3.2.9)
63
46
  actionpack (= 3.2.9)
64
47
  activesupport (= 3.2.9)
@@ -67,19 +50,22 @@ GEM
67
50
  rdoc (~> 3.4)
68
51
  thor (>= 0.14.6, < 2.0)
69
52
  rake (10.0.2)
53
+ rbx-require-relative (0.0.5)
70
54
  rdoc (3.12)
71
55
  json (~> 1.4)
72
- rspec-core (2.12.1)
73
- rspec-expectations (2.12.0)
74
- diff-lcs (~> 1.1.3)
75
- rspec-mocks (2.12.0)
76
- rspec-rails (2.12.0)
77
- actionpack (>= 3.0)
78
- activesupport (>= 3.0)
79
- railties (>= 3.0)
80
- rspec-core (~> 2.12.0)
81
- rspec-expectations (~> 2.12.0)
82
- rspec-mocks (~> 2.12.0)
56
+ rspec (2.13.0)
57
+ rspec-core (~> 2.13.0)
58
+ rspec-expectations (~> 2.13.0)
59
+ rspec-mocks (~> 2.13.0)
60
+ rspec-core (2.13.0)
61
+ rspec-expectations (2.13.0)
62
+ diff-lcs (>= 1.1.3, < 2.0)
63
+ rspec-mocks (2.13.0)
64
+ ruby-debug (0.10.4)
65
+ columnize (>= 0.1)
66
+ ruby-debug-base (~> 0.10.4.0)
67
+ ruby-debug-base (0.10.4)
68
+ linecache (>= 0.3)
83
69
  sprockets (2.2.2)
84
70
  hike (~> 1.2)
85
71
  multi_json (~> 1.0)
@@ -87,15 +73,14 @@ GEM
87
73
  tilt (~> 1.1, != 1.3.0)
88
74
  thor (0.16.0)
89
75
  tilt (1.3.3)
90
- treetop (1.4.12)
91
- polyglot
92
- polyglot (>= 0.3.1)
93
- tzinfo (0.3.35)
94
76
 
95
77
  PLATFORMS
96
78
  ruby
97
79
 
98
80
  DEPENDENCIES
81
+ actionpack
99
82
  fastercsv
100
- rails
101
- rspec-rails
83
+ haml
84
+ haml-rails
85
+ rspec
86
+ ruby-debug
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Lars Gollnow
1
+ Copyright (c) 2012 Lars Gollnow, Vladimir Glusic
2
2
 
3
3
  MIT License
4
4
 
@@ -69,7 +69,3 @@ module TableGo
69
69
 
70
70
  end
71
71
  end
72
-
73
-
74
- # when Time
75
- # I18n.l(value.localtime, :format => :compact)
@@ -2,10 +2,12 @@ module TableGo
2
2
  module Helpers
3
3
 
4
4
  def table_go_for(collection, model_klass, options = {}, &block)
5
- if request && request.format.csv?
6
- capture { TableGo.render_csv(collection, model_klass, options, &block).html_safe }
7
- else
8
- capture { TableGo.render_html(collection, model_klass, self, options, &block) }
5
+ capture do
6
+ if request && request.format.csv?
7
+ TableGo.render_csv(collection, model_klass, options, &block).html_safe
8
+ else
9
+ TableGo.render_html(collection, model_klass, self, options, &block)
10
+ end
9
11
  end
10
12
  end
11
13
 
@@ -4,42 +4,40 @@ module TableGo
4
4
  include RendererBase
5
5
 
6
6
  def render_template
7
- template.content_tag :table, table_html do
8
- template.concat template.content_tag(:caption, title) if title
9
- template.concat table_head
10
- template.concat table_body
7
+ content_tag(:table, table_html) do
8
+ concat(content_tag(:caption, title)) if title
9
+ concat(table_head)
10
+ concat(table_body)
11
11
  end
12
12
  end
13
13
 
14
14
  def table_head
15
- template.content_tag :thead do
16
- template.content_tag(:tr) do
15
+ content_tag(:thead) do
16
+ content_tag(:tr) do
17
17
  source_table.columns.each do |column|
18
- template.concat template.content_tag(:th, label_for_column(column), html_options_for_header(column))
18
+ concat(content_tag(:th, label_for_column(column), html_options_for_header(column)))
19
19
  end
20
20
  end
21
21
  end
22
-
23
22
  end
24
23
 
25
24
  def table_body
26
- template.content_tag :tbody do
27
-
25
+ content_tag(:tbody) do
28
26
  source_table.collection.each do |record|
29
- tr = template.content_tag(:tr, html_options_for_row(record)) do
27
+ tr = content_tag(:tr, html_options_for_row(record)) do
30
28
  source_table.columns.each do |column|
31
29
  value = value_from_record_by_column(record, column)
32
- template.concat template.content_tag(:td, apply_formatter(record, column, value), html_options_for_cell(record, column, value))
30
+ concat(content_tag(:td, apply_formatter(record, column, value), html_options_for_cell(record, column, value)))
33
31
  end
34
32
  end
35
- template.concat tr
33
+ concat(tr)
36
34
  end
37
35
  end
38
36
  end
39
37
 
40
38
  def table_foot
41
- template.content_tag :tfoot do
42
- template.content_tag :tr do
39
+ content_tag(:tfoot) do
40
+ content_tag(:tr) do
43
41
  end
44
42
  end
45
43
  end
@@ -2,13 +2,13 @@ module TableGo
2
2
  module Renderers
3
3
  module RendererBase
4
4
  extend ActiveSupport::Concern
5
- include ActionView::Helpers::CaptureHelper
6
5
 
7
6
  included do
8
7
  attr_accessor :source_table, :title, :table_html, :row_html, :template
9
- # attr_accessor :output_buffer
8
+ delegate :content_tag, :concat, :to => :template
10
9
  end
11
10
 
11
+
12
12
  def apply_options(options)
13
13
  self.title = options.delete(:title)
14
14
  self.table_html = options.delete(:table_html)
@@ -20,68 +20,88 @@ module TableGo
20
20
  end
21
21
 
22
22
 
23
- def label_for_column(column)
24
- column.label || begin
25
- if column.method && 1==3# reflection = source_table.model_klass.reflections[column.name]
26
- reflection.klass.human_attribute_name(column.method).html_safe
27
- # if column.method && reflection = source_table.model_klass.reflections[column.name]
28
- # reflection.klass.human_attribute_name(column.method).html_safe
29
- else
30
- column.human_attribute_name
23
+
24
+ private
25
+
26
+ def label_for_column(column)
27
+ column.label || begin
28
+ if column.method && 1==3# reflection = source_table.model_klass.reflections[column.name]
29
+ reflection.klass.human_attribute_name(column.method).html_safe
30
+ # if column.method && reflection = source_table.model_klass.reflections[column.name]
31
+ # reflection.klass.human_attribute_name(column.method).html_safe
32
+ else
33
+ column.human_attribute_name
34
+ end
31
35
  end
32
36
  end
33
- end
34
37
 
35
- def html_options_for_header(column)
36
- {}.tap do |h|
37
- (column.header_html || {}).each do |k, v|
38
- h[k] = v.is_a?(Proc) ? v.call(column) : v
38
+ def html_options_for_header(column)
39
+ {}.tap do |h|
40
+ (column.header_html || {}).each do |k, v|
41
+ h[k] = v.is_a?(Proc) ? v.call(column) : v
42
+ end
39
43
  end
40
44
  end
41
- end
42
45
 
43
- def html_options_for_row(record)
44
- {}.tap do |h|
45
- (row_html || {}).each do |k, v|
46
- h[k] = v.is_a?(Proc) ? v.call(record) : v
46
+ def html_options_for_row(record)
47
+ {}.tap do |h|
48
+ (row_html || {}).each do |k, v|
49
+ h[k] = v.is_a?(Proc) ? v.call(record) : v
50
+ end
47
51
  end
48
52
  end
49
- end
50
53
 
51
- def html_options_for_cell(record, column, value)
52
- {}.tap do |h|
53
- (column.column_html || {}).each do |k, v|
54
- h[k] = v.is_a?(Proc) ? v.call(record, column, value) : v
54
+ def html_options_for_cell(record, column, value)
55
+ {}.tap do |h|
56
+ (column.column_html || {}).each do |k, v|
57
+ h[k] = v.is_a?(Proc) ? v.call(record, column, value) : v
58
+ end
55
59
  end
56
60
  end
57
- end
58
61
 
59
- # def render_record(record, column)
60
- # apply_formatter(record, column, value_from_record_by_column(record, column))
61
- # end
62
+ def value_from_record_by_column(record, column)
63
+ if record.respond_to?(column.name)
64
+ value = record.send(column.name)
65
+ column.method ? value.send(column.method) : value
66
+ else
67
+ ''
68
+ end
69
+ end
62
70
 
63
- def value_from_record_by_column(record, column)
64
- if record.respond_to?(column.name)
65
- value = record.send(column.name)
66
- column.method ? value.send(column.method) : value
67
- else
68
- ''
71
+ 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
69
82
  end
70
- end
71
83
 
72
84
 
73
- def apply_formatter(record, column, value)
74
- if formatter = column.as
75
- Formatter.apply(formatter, record, column, value)
76
- elsif formatter = column.send
77
- Formatter.apply_send(formatter, record, column, value)
78
- elsif formatter = column.block
79
- Formatter.apply(formatter, record, column, value )
80
- # capture { Formatter.apply(formatter, record, column, value )}
81
- else
82
- value
85
+
86
+ def apply_formatter_for_block(formatter, record, column, value)
87
+ s = nil
88
+ capture_view do
89
+ s = Formatter.apply(formatter, record, column, value )
90
+ end.presence || s # for compatibility to legacy haml "- t.column :ident"
91
+ end
92
+
93
+ def capture_view(&proc)
94
+ @capturer ||= template.is_haml? ? capture_haml : capture_action_view
95
+ @capturer.call(proc)
96
+ end
97
+
98
+ def capture_haml(&proc)
99
+ lambda { |c| template.capture_haml { c.call } }
100
+ end
101
+
102
+ def capture_action_view
103
+ lambda { |c| template.capture { c.call } }
83
104
  end
84
- end
85
105
 
86
106
  end
87
107
  end
@@ -1,3 +1,3 @@
1
1
  module TableGo
2
- VERSION = '0.1.9'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/table_go.rb CHANGED
@@ -27,6 +27,11 @@ module TableGo
27
27
  renderer.render_template
28
28
  end
29
29
 
30
+ def self.formatters
31
+ Formatter.formatters
32
+ end
33
+
30
34
  end
31
35
 
36
+
32
37
  require 'table_go/railtie' if defined?(Rails)
@@ -0,0 +1,9 @@
1
+ = table_go_for(articles, Article) do |t|
2
+ - t.column :ident
3
+ - t.column :custom_single_cell do |value, record, column|
4
+ = "Ident: %s - Title: %s" % [record.ident, record.title]
5
+ - t.column :custom_multiline_cell do |value, record, column|
6
+ = "Ident: %s -" % record.ident
7
+ = " Title: %s" % record.title
8
+ - t.column :custom_single_cell_with_backwards_compatibility do |value, record, column|
9
+ - "Ident: %s" % record.ident
data/spec/spec_helper.rb CHANGED
@@ -1,17 +1,19 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
- require 'rails'
4
- require 'rspec-rails'
3
+ # require 'rails'
4
+ require 'rspec'
5
5
  require 'action_controller'
6
6
  # require 'active_model'
7
7
  require 'ostruct'
8
8
  require 'table_go'
9
+ require 'haml'
9
10
  # require 'pry'
10
11
 
11
12
  RSpec.configure do |config|
12
- # some (optional) config here
13
13
  end
14
14
 
15
+
16
+
15
17
  class String
16
18
  def cleanup_html
17
19
  self.gsub(/\n/,'').gsub(/>\s*</, "><").strip
@@ -30,6 +32,9 @@ class Article < OpenStruct
30
32
  def self.column_names
31
33
  [:ident, :title, :date_of_order, :vat, :price, :xmas_bonus, :my_type]
32
34
  end
33
-
34
35
  end
35
36
 
37
+
38
+ def read_file_from_fixtures_path(file)
39
+ File.read(File.dirname(__FILE__) + '/fixtures/%s' % file)
40
+ end
@@ -0,0 +1,63 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ ActionView::Base.send :include, TableGo::Helpers
5
+
6
+ describe TableGo::Helpers do
7
+
8
+ let(:articles) do
9
+ [ Article.new(:title => 'iPutz',
10
+ :date_of_order => Date.new(2012), :ident => 1, :vat => 19, :price => 5, :xmas_bonus => true,
11
+ :my_type => 'super_type'),
12
+ Article.new(:title => 'Nutzbook',
13
+ :date_of_order => Date.new(2012), :ident => 2, :vat => 19, :price => 5, :xmas_bonus => false,
14
+ :my_type => 'hardware_type') ]
15
+ end
16
+
17
+ let(:template) do
18
+ ActionView::Base.new.tap do |view|
19
+ view.output_buffer = ActiveSupport::SafeBuffer.new rescue ''
20
+ end
21
+ end
22
+
23
+
24
+ describe 'integtation in haml template' do
25
+
26
+ let(:subject) do
27
+ Haml::Engine.new(read_file_from_fixtures_path('simple_table.html.haml'))
28
+ end
29
+
30
+
31
+ it "description" do
32
+ subject.render(template, :articles => articles).cleanup_html.should == %Q(
33
+ <table>
34
+ <thead>
35
+ <tr>
36
+ <th>Ident</th>
37
+ <th>Custom single cell</th>
38
+ <th>Custom multiline cell</th>
39
+ <th>Custom single cell with backwards compatibility</th>
40
+ </tr></thead>
41
+ <tbody>
42
+ <tr>
43
+ <td>1</td>
44
+ <td>Ident: 1 - Title: iPutz</td>
45
+ <td>Ident: 1 - Title: iPutz</td>
46
+ <td>Ident: 1</td>
47
+ </tr>
48
+ <tr>
49
+ <td>2</td>
50
+ <td>Ident: 2 - Title: Nutzbook</td>
51
+ <td>Ident: 2 - Title: Nutzbook</td>
52
+ <td>Ident: 2</td>
53
+ </tr>
54
+ </tbody>
55
+ </table>
56
+ ).cleanup_html
57
+ end
58
+
59
+
60
+ end
61
+
62
+ end
63
+
@@ -1,6 +1,7 @@
1
1
  # encoding: UTF-8
2
2
  require 'spec_helper'
3
3
 
4
+
4
5
  describe TableGo::Renderers::HtmlRenderer do
5
6
 
6
7
  let(:articles) do
@@ -97,12 +98,12 @@ describe TableGo::Renderers::HtmlRenderer do
97
98
  :label => 'with custom formatter',
98
99
  :as => lambda { |value, record, column| value.to_s.reverse }
99
100
 
100
- # t.column :date_of_order,
101
- # :label => 'with block level custom formatter' do |value, record, column|
101
+ t.column :info_text,
102
+ :label => 'with block level custom formatter' do |value, record, column|
102
103
 
103
- # "a special<br/>value"
104
+ "a special<br/>value"
104
105
 
105
- # end
106
+ end
106
107
 
107
108
  t.column :xmas_bonus,
108
109
  :as => :boolean,
@@ -129,6 +130,7 @@ describe TableGo::Renderers::HtmlRenderer do
129
130
  <th class="date" id="date_column" style="xyz">Date of order</th>
130
131
  <th>Date of order</th>
131
132
  <th>with custom formatter</th>
133
+ <th>with block level custom formatter</th>
132
134
  <th>as boolean</th>
133
135
  <th>My type</th>
134
136
  </tr>
@@ -141,6 +143,7 @@ describe TableGo::Renderers::HtmlRenderer do
141
143
  <td class="date" id="date_1" style="xyz">2012-01-01</td>
142
144
  <td>Jan 01</td>
143
145
  <td>10-10-2102</td>
146
+ <td>a special<br/>value</td>
144
147
  <td>&#10004;</td>
145
148
  <td>Super Type</td>
146
149
  </tr>
@@ -151,6 +154,7 @@ describe TableGo::Renderers::HtmlRenderer do
151
154
  <td class="date" id="date_2" style="xyz">2012-01-01</td>
152
155
  <td>Jan 01</td>
153
156
  <td>10-10-2102</td>
157
+ <td>a special<br/>value</td>
154
158
  <td>&#10008;</td>
155
159
  <td>Hardware Type</td>
156
160
  </tr>
@@ -162,5 +166,6 @@ describe TableGo::Renderers::HtmlRenderer do
162
166
 
163
167
  end
164
168
 
169
+
165
170
  end
166
171
 
data/table_go.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |gem|
21
21
  gem.add_dependency 'fastercsv'
22
22
 
23
23
  gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'haml'
24
25
  end
25
26
 
26
27
 
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.1.9
4
+ version: 0.2.0
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-25 00:00:00 Z
13
+ date: 2013-09-27 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: actionpack
@@ -39,6 +39,14 @@ dependencies:
39
39
  - *id002
40
40
  type: :development
41
41
  version_requirements: *id004
42
+ - !ruby/object:Gem::Dependency
43
+ name: haml
44
+ prerelease: false
45
+ requirement: &id005 !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - *id002
48
+ type: :development
49
+ version_requirements: *id005
42
50
  description: simple, flexible and fast html table generator
43
51
  email:
44
52
  - lg@megorei.com
@@ -70,7 +78,9 @@ files:
70
78
  - lib/table_go/table.rb
71
79
  - lib/table_go/table_renderer.rb
72
80
  - lib/table_go/version.rb
81
+ - spec/fixtures/simple_table.html.haml
73
82
  - spec/spec_helper.rb
83
+ - spec/table_go/helper_spec.rb
74
84
  - spec/table_go/renderers/csv_renderer_spec.rb
75
85
  - spec/table_go/renderers/html_renderer_spec.rb
76
86
  - table_go.gemspec
@@ -98,6 +108,8 @@ signing_key:
98
108
  specification_version: 4
99
109
  summary: ""
100
110
  test_files:
111
+ - spec/fixtures/simple_table.html.haml
101
112
  - spec/spec_helper.rb
113
+ - spec/table_go/helper_spec.rb
102
114
  - spec/table_go/renderers/csv_renderer_spec.rb
103
115
  - spec/table_go/renderers/html_renderer_spec.rb