Permalink
Browse files

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...
1 parent 3f462ca commit 2900371a4e3f0841007e2b3c521c462fc25aeb09 Mark Evans committed Aug 19, 2009
Showing with 91 additions and 22 deletions.
  1. +10 −0 README.markdown
  2. +0 −1 Rakefile
  3. +5 −2 lib/block_helpers.rb
  4. +76 −19 spec/helpers/block_helpers_spec.rb
View
@@ -168,6 +168,16 @@ To use in Rails, add to your `environment.rb`:
config.gem "markevans-block_helpers", :lib => "block_helpers", :source => "http://gems.github.com"
+for Merb, in `init.rb`:
+
+ dependency "markevans-block_helpers", :require_as => "block_helpers"
+
+Compatibility
+=============
+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).
+
+It works with both the one and two argument versions of `concat`, so should work with all recent versions of Rails and Merb.
+
Credits
=======
- <a href="http://github.com/markevans">Mark Evans</a> (author)
View
@@ -10,7 +10,6 @@ begin
gem.homepage = "http://github.com/markevans/block_helpers"
gem.authors = ["Mark Evans"]
gem.add_dependency('activesupport', '>= 2.0')
- gem.add_dependency('actionpack', '>= 2.0')
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
View
@@ -1,5 +1,4 @@
require 'activesupport'
-require 'action_view'
module BlockHelpers
@@ -20,7 +19,11 @@ def #{method_name}(*args, &block)
renderer = #{klass.name}.new(*args)
renderer.send(:helper=, self)
if renderer.public_methods(false).include? 'display'
- concat renderer.display(capture(renderer, &block))
+ if method(:concat).arity == 1
+ concat renderer.display(capture(renderer, &block))
+ else
+ concat renderer.display(capture(renderer, &block)), binding
+ end
else
block.call(renderer)
end
@@ -8,9 +8,12 @@ module TestHelperModule
describe "simple block_helper" do
before(:each) do
- class TestHelperModule::TestHelper < BlockHelpers::Base
- def hello
- 'Hi there'
+ module TestHelperModule
+ remove_const(:TestHelper) if defined?(TestHelper)
+ class TestHelper < BlockHelpers::Base
+ def hello
+ 'Hi there'
+ end
end
end
end
@@ -34,12 +37,13 @@ def hello
describe "access to other methods" do
before(:each) do
module TestHelperModule
-
+
def yoghurt
'Yoghurt'
end
- class TestHelper
+ remove_const(:TestHelper) if defined?(TestHelper)
+ class TestHelper < BlockHelpers::Base
def yog
yoghurt[0..2]
end
@@ -92,7 +96,7 @@ def cheese
<% end %>
)).should match_html('<label for="hi">Hi</label>')
end
- it "should work with methods like 'concat'" do
+ it "should work with methods like 'capture'" do
eval_erb(%(
<% test_helper do |r| %>
<% r.check_capture do %>
@@ -103,16 +107,66 @@ def cheese
end
end
+ describe "accesibility" do
+ def run_erb
+ eval_erb(%(
+ <% compat_helper do |r| %>
+ HELLO
+ <% end %>
+ ))
+ end
+
+ it "should work when concat has one arg" do
+ module TestHelperModule
+ def concat(html); super(html); end
+ remove_const(:CompatHelper) if defined?(CompatHelper)
+ class CompatHelper < BlockHelpers::Base
+ def display(body)
+ "Before...#{body}...after"
+ end
+ end
+ end
+ run_erb.should match_html("Before... HELLO ...after")
+ end
+ it "should work when concat has two args" do
+ module TestHelperModule
+ def concat(html, binding); super(html); end
+ remove_const(:CompatHelper) if defined?(:CompatHelper)
+ class CompatHelper < BlockHelpers::Base
+ def display(body)
+ "Before...#{body}...after"
+ end
+ end
+ end
+ run_erb.should match_html("Before... HELLO ...after")
+ end
+ it "should work when concat has one optional arg" do
+ module TestHelperModule
+ def concat(html, binding=nil); super(html); end
+ remove_const(:CompatHelper) if defined?(:CompatHelper)
+ class CompatHelper < BlockHelpers::Base
+ def display(body)
+ "Before...#{body}...after"
+ end
+ end
+ end
+ run_erb.should match_html("Before... HELLO ...after")
+ end
+ end
+
describe "surrounding the block" do
before(:each) do
- class TestHelperModule::TestHelperSurround < BlockHelpers::Base
- def display(body)
- %(
- <p>Before</p>
- #{body}
- <p>After</p>
- )
+ module TestHelperModule
+ remove_const(:TestHelperSurround) if defined?(TestHelperSurround)
+ class TestHelperSurround < BlockHelpers::Base
+ def display(body)
+ %(
+ <p>Before</p>
+ #{body}
+ <p>After</p>
+ )
+ end
end
end
end
@@ -128,12 +182,15 @@ def display(body)
describe "block helpers with arguments" do
before(:each) do
- class TestHelperModule::TestHelperWithArgs < BlockHelpers::Base
- def initialize(id, klass)
- @id, @klass = id, klass
- end
- def hello
- %(<p class="#{@klass}" id="#{@id}">Hello</p>)
+ module TestHelperModule
+ remove_const(:TestHelperWithArgs) if defined?(TestHelperWithArgs)
+ class TestHelperWithArgs < BlockHelpers::Base
+ def initialize(id, klass)
+ @id, @klass = id, klass
+ end
+ def hello
+ %(<p class="#{@klass}" id="#{@id}">Hello</p>)
+ end
end
end
end

0 comments on commit 2900371

Please sign in to comment.