Skip to content
This repository
Browse code

Got rid of dependency on actionview and allowed it to work with both …

…one and two arg versions of concat, so will work with older Merb / Rails
  • Loading branch information...
commit 2900371a4e3f0841007e2b3c521c462fc25aeb09 1 parent 3f462ca
Mark Evans authored August 19, 2009
10  README.markdown
Source Rendered
@@ -168,6 +168,16 @@ To use in Rails, add to your `environment.rb`:
168 168
 
169 169
     config.gem "markevans-block_helpers", :lib => "block_helpers", :source => "http://gems.github.com"
170 170
 
  171
+for Merb, in `init.rb`:
  172
+
  173
+    dependency "markevans-block_helpers", :require_as => "block_helpers"
  174
+
  175
+Compatibility
  176
+=============
  177
+Currently it depends on activesupport, and requires that the helper already has the methods `concat` and `capture` available (which is the case in Rails and Merb).
  178
+
  179
+It works with both the one and two argument versions of `concat`, so should work with all recent versions of Rails and Merb.
  180
+
171 181
 Credits
172 182
 =======
173 183
 - <a href="http://github.com/markevans">Mark Evans</a> (author)
1  Rakefile
@@ -10,7 +10,6 @@ begin
10 10
     gem.homepage = "http://github.com/markevans/block_helpers"
11 11
     gem.authors = ["Mark Evans"]
12 12
     gem.add_dependency('activesupport', '>= 2.0')
13  
-    gem.add_dependency('actionpack', '>= 2.0')
14 13
     # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15 14
   end
16 15
 
7  lib/block_helpers.rb
... ...
@@ -1,5 +1,4 @@
1 1
 require 'activesupport'
2  
-require 'action_view'
3 2
 
4 3
 module BlockHelpers
5 4
 
@@ -20,7 +19,11 @@ def #{method_name}(*args, &block)
20 19
           renderer = #{klass.name}.new(*args)
21 20
           renderer.send(:helper=, self)
22 21
           if renderer.public_methods(false).include? 'display'
23  
-            concat renderer.display(capture(renderer, &block))
  22
+            if method(:concat).arity == 1
  23
+              concat renderer.display(capture(renderer, &block))
  24
+            else
  25
+              concat renderer.display(capture(renderer, &block)), binding
  26
+            end
24 27
           else
25 28
             block.call(renderer)
26 29
           end
95  spec/helpers/block_helpers_spec.rb
@@ -8,9 +8,12 @@ module TestHelperModule
8 8
   describe "simple block_helper" do
9 9
     
10 10
     before(:each) do
11  
-      class TestHelperModule::TestHelper < BlockHelpers::Base
12  
-        def hello
13  
-          'Hi there'
  11
+      module TestHelperModule
  12
+        remove_const(:TestHelper) if defined?(TestHelper)
  13
+        class TestHelper < BlockHelpers::Base
  14
+          def hello
  15
+            'Hi there'
  16
+          end
14 17
         end
15 18
       end
16 19
     end
@@ -34,12 +37,13 @@ def hello
34 37
   describe "access to other methods" do
35 38
     before(:each) do
36 39
       module TestHelperModule
37  
-        
  40
+
38 41
         def yoghurt
39 42
           'Yoghurt'
40 43
         end
41 44
         
42  
-        class TestHelper
  45
+        remove_const(:TestHelper) if defined?(TestHelper)
  46
+        class TestHelper < BlockHelpers::Base
43 47
           def yog
44 48
             yoghurt[0..2]
45 49
           end
@@ -92,7 +96,7 @@ def cheese
92 96
         <% end %>
93 97
       )).should match_html('<label for="hi">Hi</label>')
94 98
     end
95  
-    it "should work with methods like 'concat'" do
  99
+    it "should work with methods like 'capture'" do
96 100
       eval_erb(%(
97 101
         <% test_helper do |r| %>
98 102
           <% r.check_capture do %>
@@ -103,16 +107,66 @@ def cheese
103 107
     end
104 108
   end
105 109
   
  110
+  describe "accesibility" do
  111
+    def run_erb
  112
+      eval_erb(%(
  113
+        <% compat_helper do |r| %>
  114
+          HELLO
  115
+        <% end %>
  116
+      ))
  117
+    end
  118
+
  119
+    it "should work when concat has one arg" do
  120
+      module TestHelperModule
  121
+        def concat(html); super(html); end
  122
+        remove_const(:CompatHelper) if defined?(CompatHelper)
  123
+        class CompatHelper < BlockHelpers::Base
  124
+          def display(body)
  125
+            "Before...#{body}...after"
  126
+          end
  127
+        end
  128
+      end
  129
+      run_erb.should match_html("Before... HELLO ...after")
  130
+    end
  131
+    it "should work when concat has two args" do
  132
+      module TestHelperModule
  133
+        def concat(html, binding); super(html); end
  134
+        remove_const(:CompatHelper) if defined?(:CompatHelper)
  135
+        class CompatHelper < BlockHelpers::Base
  136
+          def display(body)
  137
+            "Before...#{body}...after"
  138
+          end
  139
+        end
  140
+      end
  141
+      run_erb.should match_html("Before... HELLO ...after")
  142
+    end
  143
+    it "should work when concat has one optional arg" do
  144
+      module TestHelperModule
  145
+        def concat(html, binding=nil); super(html); end
  146
+        remove_const(:CompatHelper) if defined?(:CompatHelper)
  147
+        class CompatHelper < BlockHelpers::Base
  148
+          def display(body)
  149
+            "Before...#{body}...after"
  150
+          end
  151
+        end
  152
+      end
  153
+      run_erb.should match_html("Before... HELLO ...after")
  154
+    end
  155
+  end
  156
+  
106 157
   describe "surrounding the block" do
107 158
 
108 159
     before(:each) do
109  
-      class TestHelperModule::TestHelperSurround < BlockHelpers::Base
110  
-        def display(body)
111  
-          %(
112  
-            <p>Before</p>
113  
-            #{body}
114  
-            <p>After</p>
115  
-          )
  160
+      module TestHelperModule
  161
+        remove_const(:TestHelperSurround) if defined?(TestHelperSurround)
  162
+        class TestHelperSurround < BlockHelpers::Base
  163
+          def display(body)
  164
+            %(
  165
+              <p>Before</p>
  166
+              #{body}
  167
+              <p>After</p>
  168
+            )
  169
+          end
116 170
         end
117 171
       end
118 172
     end
@@ -128,12 +182,15 @@ def display(body)
128 182
   
129 183
   describe "block helpers with arguments" do
130 184
     before(:each) do
131  
-      class TestHelperModule::TestHelperWithArgs < BlockHelpers::Base
132  
-        def initialize(id, klass)
133  
-          @id, @klass = id, klass
134  
-        end
135  
-        def hello
136  
-          %(<p class="#{@klass}" id="#{@id}">Hello</p>)
  185
+      module TestHelperModule
  186
+        remove_const(:TestHelperWithArgs) if defined?(TestHelperWithArgs)
  187
+        class TestHelperWithArgs < BlockHelpers::Base
  188
+          def initialize(id, klass)
  189
+            @id, @klass = id, klass
  190
+          end
  191
+          def hello
  192
+            %(<p class="#{@klass}" id="#{@id}">Hello</p>)
  193
+          end
137 194
         end
138 195
       end
139 196
     end

0 notes on commit 2900371

Please sign in to comment.
Something went wrong with that request. Please try again.