Browse files

Got it working properly with nested block helpers

  • Loading branch information...
1 parent 7f86e62 commit 04b65809246aa9f7364a8b3885e004603ce2979d Mark Evans committed Oct 12, 2009
Showing with 40 additions and 2 deletions.
  1. +7 −2 lib/block_helpers.rb
  2. +33 −0 spec/helpers/block_helpers_spec.rb
View
9 lib/block_helpers.rb
@@ -17,11 +17,16 @@ def self.inherited(klass)
klass.parent.class_eval %(
def #{method_name}(*args, &block)
renderer = #{klass.name}.new(*args)
- renderer.send(:helper=, self)
+ top_level_helper = if self.is_a?(BlockHelpers::Base)
+ self.helper
+ else
+ self
+ end
+ renderer.send(:helper=, top_level_helper)
body = block ? capture(renderer, &block) : nil
processed_body = renderer.display(body)
if processed_body
- if method(:concat).arity == 2
+ if top_level_helper.method(:concat).arity == 2
concat processed_body, binding
else
concat processed_body
View
33 spec/helpers/block_helpers_spec.rb
@@ -281,4 +281,37 @@ def display(body)
end
end
+ describe "nested block helpers" do
+ it "should define a nested block helper method" do
+ module TestHelperModule
+ class OuterHelper < BlockHelpers::Base
+
+ def egg
+ 'bad egg'
+ end
+
+ def display(body)
+ "Outer #{body}"
+ end
+
+ class InnerHelper < BlockHelpers::Base
+ def egg
+ 'EGG'
+ end
+ def display(body)
+ "Inner #{body}"
+ end
+ end
+ end
+ end
+ eval_erb(%(
+ <% outer_helper do |o| %>
+ <% o.inner_helper do |i| %>
+ <%= i.egg %>
+ <% end %>
+ <% end %>
+ )).should match_html("Outer Inner EGG")
+ end
+ end
+
end

0 comments on commit 04b6580

Please sign in to comment.