tablemaker 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +72 -18
- data/lib/tablemaker.rb +0 -1
- data/lib/tablemaker/table_helper.rb +88 -0
- data/lib/tablemaker/version.rb +1 -1
- data/lib/tablemaker/view_helpers.rb +1 -89
- data/test/test_action_view_helper.rb +29 -12
- data/test/test_tablemaker.rb +0 -2
- metadata +6 -21
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: 2020838ec57d806d09cdf6f7cdca6e510445af70
|
4
|
+
data.tar.gz: 6fbd9ee26378d4a235523e09cace326bab7bd8e9
|
5
|
+
!binary "U0hBNTEy":
|
6
|
+
metadata.gz: 1147d0331ddfe3b0a755396da495ed0041a92c4a92b64fefba6f018958b547d547578a9b2be922bac06c39a1a6ba3805116d4c27dfc7ce7d92129a29616c7339
|
7
|
+
data.tar.gz: b8acde1b9d40dd5fd0433d35a3ae0946ffd1b10655e80fa56cb72403206fdf2fac0d5deb2d3b207e2e23e509dd37e452327823ba1c6390dfce00d5fe1d761cc4
|
data/README.md
CHANGED
@@ -3,7 +3,61 @@
|
|
3
3
|
HTML table generator that allows arbitrary nested cell subdivisions
|
4
4
|
and applies colspan/rowspan as needed.
|
5
5
|
|
6
|
-
|
6
|
+
Including the gem in your Rails project will give you a new view helper `make_table`
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
= make_table(class: 'foo') do |t|
|
11
|
+
- t.row do
|
12
|
+
- t.th("A")
|
13
|
+
- t.th("B")
|
14
|
+
- t.th("C")
|
15
|
+
- t.row do
|
16
|
+
- t.column do
|
17
|
+
- t.td("E")
|
18
|
+
- t.td("F)
|
19
|
+
- t.td(style: 'background: green') do
|
20
|
+
%p cell content
|
21
|
+
|
22
|
+
this will generate this output:
|
23
|
+
|
24
|
+
<table class='foo'>
|
25
|
+
<tr>
|
26
|
+
<td>A</td>
|
27
|
+
<td>B</td>
|
28
|
+
<td>C</td>
|
29
|
+
</tr>
|
30
|
+
<tr>
|
31
|
+
<td>E</td>
|
32
|
+
<td style='background: green' rowspan='2' colspan='2'><p>cell centent</p></td>
|
33
|
+
</tr>
|
34
|
+
<tr>
|
35
|
+
<td>F</td>
|
36
|
+
</tr>
|
37
|
+
</table>
|
38
|
+
|
39
|
+
|
40
|
+
source:
|
41
|
+
|
42
|
+
<table class='foo'>
|
43
|
+
<tr>
|
44
|
+
<td>A</td>
|
45
|
+
<td>B</td>
|
46
|
+
<td>C</td>
|
47
|
+
</tr>
|
48
|
+
<tr>
|
49
|
+
<td>E</td>
|
50
|
+
<td style='background: green' rowspan='2' colspan='2'>
|
51
|
+
<p>cell centent</p>
|
52
|
+
</td>
|
53
|
+
</tr>
|
54
|
+
<tr>
|
55
|
+
<td>F</td>
|
56
|
+
</tr>
|
57
|
+
</table>
|
58
|
+
|
59
|
+
|
60
|
+
## Examples outside Rails
|
7
61
|
|
8
62
|
A very basic table:
|
9
63
|
|
@@ -72,23 +126,23 @@ A more advanced example:
|
|
72
126
|
|
73
127
|
this will generate the following output:
|
74
128
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
129
|
+
<table>
|
130
|
+
<tr>
|
131
|
+
<td rowspan=4>A</td>
|
132
|
+
<td rowspan=2>B</td>
|
133
|
+
<td>D</td>
|
134
|
+
</tr>
|
135
|
+
<tr>
|
136
|
+
<td>E</td>
|
137
|
+
</tr>
|
138
|
+
<tr>
|
139
|
+
<td rowspan=2>C</td>
|
140
|
+
<td>F</td>
|
141
|
+
</tr>
|
142
|
+
<tr>
|
143
|
+
<td>G</td>
|
144
|
+
</tr>
|
145
|
+
</table>
|
92
146
|
|
93
147
|
|
94
148
|
Tablemaker keeps track of all the rowspan/colspan attributes required to generate a valid HTML table
|
data/lib/tablemaker.rb
CHANGED
@@ -0,0 +1,88 @@
|
|
1
|
+
module Tablemaker
|
2
|
+
class TableHelper
|
3
|
+
def initialize(context, &block)
|
4
|
+
@context = context
|
5
|
+
@stack = []
|
6
|
+
|
7
|
+
@root = Tablemaker.column do |r|
|
8
|
+
stack(r) do
|
9
|
+
yield self
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def row(&block)
|
15
|
+
current.row do |r|
|
16
|
+
stack(r) do
|
17
|
+
yield
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def column(&block)
|
23
|
+
current.column do |c|
|
24
|
+
stack(c, &block)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def line(&block)
|
29
|
+
current.respond_to?(:row) ? row(&block) : column(&block)
|
30
|
+
end
|
31
|
+
|
32
|
+
def td(*args, &blk)
|
33
|
+
cell("td", *args, &blk)
|
34
|
+
end
|
35
|
+
|
36
|
+
def th(*args, &blk)
|
37
|
+
cell("th", *args, &blk)
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_html(attrs = {})
|
41
|
+
context.content_tag("table", attrs) do
|
42
|
+
@root.each_row do |r|
|
43
|
+
s = context.content_tag("tr") do
|
44
|
+
r.each do |c|
|
45
|
+
attrs = {}
|
46
|
+
attrs[:rowspan] = c.real_rows if c.real_rows > 1
|
47
|
+
attrs[:colspan] = c.real_cols if c.real_cols > 1
|
48
|
+
s2 = context.content_tag(c.data[:name], c.data[:opts].merge(attrs)) do
|
49
|
+
c.data[:text]
|
50
|
+
end
|
51
|
+
|
52
|
+
context.concat(s2)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
context.concat(s)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def cell(name, *args, &block)
|
64
|
+
opts = args.extract_options!
|
65
|
+
text = if block_given?
|
66
|
+
@context.capture(&block)
|
67
|
+
else
|
68
|
+
args.shift
|
69
|
+
end
|
70
|
+
current.cell(text: text, name: name, opts: opts)
|
71
|
+
end
|
72
|
+
|
73
|
+
def stack(frame)
|
74
|
+
@stack.push(frame)
|
75
|
+
yield
|
76
|
+
@stack.pop
|
77
|
+
end
|
78
|
+
|
79
|
+
def context
|
80
|
+
@context
|
81
|
+
end
|
82
|
+
|
83
|
+
def current
|
84
|
+
@stack.last
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
data/lib/tablemaker/version.rb
CHANGED
@@ -1,97 +1,9 @@
|
|
1
1
|
require 'action_view'
|
2
|
+
require 'tablemaker/table_helper'
|
2
3
|
|
3
4
|
module Tablemaker
|
4
5
|
module ViewHelpers
|
5
6
|
module ActionView
|
6
|
-
class TableHelper
|
7
|
-
def initialize(context, &block)
|
8
|
-
@context = context
|
9
|
-
@stack = []
|
10
|
-
@root = Tablemaker.column do |r|
|
11
|
-
stack(r) do
|
12
|
-
yield self
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def row
|
18
|
-
current.row do |r|
|
19
|
-
stack(r) do
|
20
|
-
yield
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def column(&blk)
|
26
|
-
if @stack.size == 1
|
27
|
-
row do
|
28
|
-
column(&blk)
|
29
|
-
end
|
30
|
-
else
|
31
|
-
current.column do |c|
|
32
|
-
stack(c, &blk)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def td(*args, &blk)
|
38
|
-
cell("td", *args, &blk)
|
39
|
-
end
|
40
|
-
|
41
|
-
def th(*args, &blk)
|
42
|
-
cell("th", *args, &blk)
|
43
|
-
end
|
44
|
-
|
45
|
-
def to_html(attrs = {})
|
46
|
-
context.content_tag("table", attrs) do
|
47
|
-
@root.each_row do |r|
|
48
|
-
s = context.content_tag("tr") do
|
49
|
-
r.each do |c|
|
50
|
-
attrs = {}
|
51
|
-
attrs[:rowspan] = c.real_rows if c.real_rows > 1
|
52
|
-
attrs[:colspan] = c.real_cols if c.real_cols > 1
|
53
|
-
s2 = context.content_tag(c.data[:name], c.data[:opts].merge(attrs)) do
|
54
|
-
c.data[:text]
|
55
|
-
end
|
56
|
-
|
57
|
-
context.concat(s2)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
context.concat(s)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
|
66
|
-
private
|
67
|
-
|
68
|
-
def cell(name, *args, &block)
|
69
|
-
opts = args.extract_options!
|
70
|
-
text = if block_given?
|
71
|
-
@context.capture(&block)
|
72
|
-
else
|
73
|
-
args.shift
|
74
|
-
end
|
75
|
-
current.cell(text: text, name: name, opts: opts)
|
76
|
-
end
|
77
|
-
|
78
|
-
def stack(frame)
|
79
|
-
@stack.push(frame)
|
80
|
-
yield
|
81
|
-
@stack.pop
|
82
|
-
end
|
83
|
-
|
84
|
-
def context
|
85
|
-
@context
|
86
|
-
end
|
87
|
-
|
88
|
-
def current
|
89
|
-
@stack.last
|
90
|
-
end
|
91
|
-
|
92
|
-
end
|
93
|
-
|
94
|
-
|
95
7
|
def make_table(opts = {}, &block)
|
96
8
|
table = TableHelper.new(self, &block).to_html(opts)
|
97
9
|
end
|
@@ -23,18 +23,6 @@ class TestTableMaker < ActionView::TestCase
|
|
23
23
|
assert_dom_equal expected, table
|
24
24
|
end
|
25
25
|
|
26
|
-
test "should output simple table starting with columns correctly" do
|
27
|
-
table = make_table do |t|
|
28
|
-
t.column do
|
29
|
-
t.td("A")
|
30
|
-
t.td("B")
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
expected = "<table><tr><td>A</td></tr><tr><td>B</td></tr></table>"
|
35
|
-
assert_dom_equal expected, table
|
36
|
-
end
|
37
|
-
|
38
26
|
test "should output complex table correctly" do
|
39
27
|
table = make_table do |t|
|
40
28
|
t.row do
|
@@ -59,4 +47,33 @@ class TestTableMaker < ActionView::TestCase
|
|
59
47
|
|
60
48
|
assert_dom_equal expected, table
|
61
49
|
end
|
50
|
+
|
51
|
+
test "should have generic 'line' method that alternates between rows and columns" do
|
52
|
+
table = make_table do |t|
|
53
|
+
t.line do
|
54
|
+
t.line do
|
55
|
+
t.line do
|
56
|
+
t.td("A")
|
57
|
+
t.td("B")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
t.line do
|
61
|
+
t.td("C")
|
62
|
+
t.td("D")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
t.line do
|
66
|
+
t.td("E")
|
67
|
+
t.td("F")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
expected = "<table>" +
|
72
|
+
"<tr><td rowspan=2>A</td><td rowspan=2>B</td><td>C</td></tr>" +
|
73
|
+
"<tr><td>D</td></tr>" +
|
74
|
+
"<tr><td>E</td><td colspan=2>F</td></tr>" +
|
75
|
+
"</table>"
|
76
|
+
|
77
|
+
assert_dom_equal expected, table
|
78
|
+
end
|
62
79
|
end
|
data/test/test_tablemaker.rb
CHANGED
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tablemaker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Levin Alexander
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-11-18 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rake
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ! '>='
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ! '>='
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: psych
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ! '>='
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ! '>='
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,7 +41,6 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: minitest
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ! '>='
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ! '>='
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -62,7 +55,6 @@ dependencies:
|
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: rails
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
59
|
- - ! '>='
|
68
60
|
- !ruby/object:Gem::Version
|
@@ -70,7 +62,6 @@ dependencies:
|
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
66
|
- - ! '>='
|
76
67
|
- !ruby/object:Gem::Version
|
@@ -92,6 +83,7 @@ files:
|
|
92
83
|
- lib/tablemaker.rb
|
93
84
|
- lib/tablemaker/frame.rb
|
94
85
|
- lib/tablemaker/railtie.rb
|
86
|
+
- lib/tablemaker/table_helper.rb
|
95
87
|
- lib/tablemaker/version.rb
|
96
88
|
- lib/tablemaker/view_helpers.rb
|
97
89
|
- tablemaker.gemspec
|
@@ -100,33 +92,26 @@ files:
|
|
100
92
|
- test/test_tablemaker.rb
|
101
93
|
homepage: https://github.com/levinalex/tablemaker
|
102
94
|
licenses: []
|
95
|
+
metadata: {}
|
103
96
|
post_install_message:
|
104
97
|
rdoc_options: []
|
105
98
|
require_paths:
|
106
99
|
- lib
|
107
100
|
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
-
none: false
|
109
101
|
requirements:
|
110
102
|
- - ! '>='
|
111
103
|
- !ruby/object:Gem::Version
|
112
104
|
version: '0'
|
113
|
-
segments:
|
114
|
-
- 0
|
115
|
-
hash: 3601937828848287223
|
116
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
-
none: false
|
118
106
|
requirements:
|
119
107
|
- - ! '>='
|
120
108
|
- !ruby/object:Gem::Version
|
121
109
|
version: '0'
|
122
|
-
segments:
|
123
|
-
- 0
|
124
|
-
hash: 3601937828848287223
|
125
110
|
requirements: []
|
126
111
|
rubyforge_project:
|
127
|
-
rubygems_version:
|
112
|
+
rubygems_version: 2.0.3
|
128
113
|
signing_key:
|
129
|
-
specification_version:
|
114
|
+
specification_version: 4
|
130
115
|
summary: ''
|
131
116
|
test_files:
|
132
117
|
- test/helper.rb
|