markevans-block_helpers 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.
- data/README.markdown +33 -5
- data/VERSION +1 -1
- data/block_helpers.gemspec +1 -1
- data/lib/block_helpers.rb +7 -3
- data/spec/helpers/block_helpers_spec.rb +34 -5
- metadata +1 -1
data/README.markdown
CHANGED
@@ -51,7 +51,35 @@ This will generate the following:
|
|
51
51
|
Accessing other helper methods
|
52
52
|
------------------------------
|
53
53
|
|
54
|
-
Methods available in the parent helper are available to the block helper class
|
54
|
+
Methods available in the parent helper are available to the block helper class.
|
55
|
+
In case of name clashes, you can also access those methods via the protected object `helper`.
|
56
|
+
In the helper:
|
57
|
+
|
58
|
+
module MyHelper
|
59
|
+
|
60
|
+
def angry
|
61
|
+
"I'm very angry"
|
62
|
+
end
|
63
|
+
|
64
|
+
class MyBlockHelper < ActionView::BlockHelper
|
65
|
+
|
66
|
+
def angry
|
67
|
+
content_tag :div, helper.angry
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
In the view:
|
75
|
+
|
76
|
+
<% my_block_helper do |h| %>
|
77
|
+
<%= h.angry %>
|
78
|
+
<% end %>
|
79
|
+
|
80
|
+
This generates:
|
81
|
+
|
82
|
+
<div>I'm very angry</div>
|
55
83
|
|
56
84
|
Using arguments
|
57
85
|
---------------
|
@@ -68,7 +96,7 @@ In the helper:
|
|
68
96
|
end
|
69
97
|
|
70
98
|
def hello(name)
|
71
|
-
|
99
|
+
content_tag @tag_type, "Hi there #{name}!"
|
72
100
|
end
|
73
101
|
|
74
102
|
end
|
@@ -88,14 +116,14 @@ This generates:
|
|
88
116
|
Surrounding markup
|
89
117
|
------------------
|
90
118
|
|
91
|
-
Use the `
|
119
|
+
Use the `display` method to surround the block with markup, e.g.
|
92
120
|
In the helper:
|
93
121
|
|
94
122
|
module MyHelper
|
95
123
|
|
96
124
|
class RoundedBox < ActionView::BlockHelper
|
97
125
|
|
98
|
-
def
|
126
|
+
def display(body)
|
99
127
|
%(
|
100
128
|
<div class="tl">
|
101
129
|
<div class="tr">
|
@@ -131,7 +159,7 @@ This generates:
|
|
131
159
|
</div>
|
132
160
|
</div>
|
133
161
|
|
134
|
-
Of course, you could use `
|
162
|
+
Of course, you could use `display` for more than just surrounding markup.
|
135
163
|
|
136
164
|
Installation
|
137
165
|
============
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
data/block_helpers.gemspec
CHANGED
data/lib/block_helpers.rb
CHANGED
@@ -18,8 +18,8 @@ module ActionView
|
|
18
18
|
klass.parent.class_eval %(
|
19
19
|
def #{method_name}(*args, &block)
|
20
20
|
renderer = #{klass.name}.new(*args)
|
21
|
-
if renderer.public_methods(false).include? '
|
22
|
-
concat renderer.
|
21
|
+
if renderer.public_methods(false).include? 'display'
|
22
|
+
concat renderer.display(capture(renderer, &block))
|
23
23
|
else
|
24
24
|
block.call(renderer)
|
25
25
|
end
|
@@ -27,9 +27,13 @@ module ActionView
|
|
27
27
|
)
|
28
28
|
|
29
29
|
# Make a 'helper' object available, for calling
|
30
|
-
# other helper methods / action view helpers
|
30
|
+
# other helper methods / action view helpers,
|
31
|
+
# in case of name clashes
|
31
32
|
klass.class_eval do
|
32
33
|
|
34
|
+
include klass.parent
|
35
|
+
include ActionView::Helpers
|
36
|
+
|
33
37
|
protected
|
34
38
|
define_method :helper do
|
35
39
|
if @helper.nil?
|
@@ -31,18 +31,33 @@ describe TestHelperModule do
|
|
31
31
|
|
32
32
|
end
|
33
33
|
|
34
|
-
describe "access to other methods
|
34
|
+
describe "access to other methods" do
|
35
35
|
before(:each) do
|
36
36
|
module TestHelperModule
|
37
|
-
|
37
|
+
|
38
|
+
def yoghurt
|
39
|
+
'Yoghurt'
|
40
|
+
end
|
41
|
+
|
38
42
|
class TestHelper
|
39
43
|
def yog
|
40
|
-
|
44
|
+
yoghurt[0..2]
|
41
45
|
end
|
42
46
|
def jelly_in_div
|
43
|
-
|
47
|
+
content_tag :div, 'jelly'
|
48
|
+
end
|
49
|
+
def cheese
|
50
|
+
helper.cheese[0..3]
|
51
|
+
end
|
52
|
+
def label_tag(text)
|
53
|
+
helper.label_tag(text[0..1])
|
44
54
|
end
|
45
55
|
end
|
56
|
+
|
57
|
+
def cheese
|
58
|
+
'Cheese'
|
59
|
+
end
|
60
|
+
|
46
61
|
end
|
47
62
|
end
|
48
63
|
it "should give the yielded renderer access to other methods" do
|
@@ -59,13 +74,27 @@ describe TestHelperModule do
|
|
59
74
|
<% end %>
|
60
75
|
)).should match_html("<div>jelly</div>")
|
61
76
|
end
|
77
|
+
it "should give the yielded renderer access to other methods via 'helper'" do
|
78
|
+
eval_erb(%(
|
79
|
+
<% test_helper do |r| %>
|
80
|
+
<%= r.cheese %>
|
81
|
+
<% end %>
|
82
|
+
)).should match_html("Chee")
|
83
|
+
end
|
84
|
+
it "should give the yielded renderer access to normal actionview helper methods via 'helper'" do
|
85
|
+
eval_erb(%(
|
86
|
+
<% test_helper do |r| %>
|
87
|
+
<%= r.label_tag 'hide' %>
|
88
|
+
<% end %>
|
89
|
+
)).should match_html('<label for="hi">Hi</label>')
|
90
|
+
end
|
62
91
|
end
|
63
92
|
|
64
93
|
describe "surrounding the block" do
|
65
94
|
|
66
95
|
before(:each) do
|
67
96
|
class TestHelperModule::TestHelperSurround < ActionView::BlockHelper
|
68
|
-
def
|
97
|
+
def display(body)
|
69
98
|
%(
|
70
99
|
<p>Before</p>
|
71
100
|
#{body}
|