Permalink
Browse files

Got 'helper' working in initialize method of block helper

- may not be thread safe as such due to setting class level 'current_helper' but shouldn't matter because this 'current_helper' will always look the same anyway
  • Loading branch information...
1 parent 23bda13 commit b002902a2add54b1af0e1be101bf1b22735b5e83 Mark Evans committed Nov 6, 2009
Showing with 55 additions and 29 deletions.
  1. +40 −29 lib/block_helpers.rb
  2. +15 −0 spec/helpers/block_helpers_spec.rb
View
@@ -4,37 +4,44 @@ module BlockHelpers
class Base
- def self.inherited(klass)
- # Define the helper method
- # e.g. for a class:
- # class HelloHelper < BlockHelpers::Base
- # #.....
- # end
- #
- # then we define a helper method 'hello_helper'
- #
- method_name = klass.name.split('::').last.underscore
- klass.parent.class_eval %(
- def #{method_name}(*args, &block)
- renderer = #{klass.name}.new(*args)
- 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 top_level_helper.method(:concat).arity == 2
- concat processed_body, binding
+ class << self
+
+ def inherited(klass)
+ # Define the helper method
+ # e.g. for a class:
+ # class HelloHelper < BlockHelpers::Base
+ # #.....
+ # end
+ #
+ # then we define a helper method 'hello_helper'
+ #
+ method_name = klass.name.split('::').last.underscore
+ klass.parent.class_eval %(
+ def #{method_name}(*args, &block)
+ top_level_helper = if self.is_a?(BlockHelpers::Base)
+ self.helper
else
- concat processed_body
+ self
end
+ #{klass.name}.current_helper = top_level_helper
+ renderer = #{klass.name}.new(*args)
+ renderer.send(:helper=, top_level_helper)
+ body = block ? capture(renderer, &block) : nil
+ processed_body = renderer.display(body)
+ if processed_body
+ if top_level_helper.method(:concat).arity == 2
+ concat processed_body, binding
+ else
+ concat processed_body
+ end
+ end
+ renderer
end
- renderer
- end
- )
+ )
+ end
+
+ attr_accessor :current_helper
+
end
def display(body)
@@ -47,7 +54,11 @@ def respond_to?(method)
protected
- attr_accessor :helper
+ attr_writer :helper
+
+ def helper
+ @helper ||= self.class.current_helper
+ end
def method_missing(method, *args, &block)
if helper.respond_to?(method)
@@ -95,6 +95,21 @@ def cheese
<% end %>
)).should match_html("<div>jelly</div>")
end
+ it "should give the yielded renderer access to normal actionview helper methods even in initialize" do
+ module TestHelperModule
+ class JokeHelper < BlockHelpers::Base
+ def initialize
+ @joke = truncate("What's the different between half a duck?", :length => 6)
+ end
+ attr_reader :joke
+ end
+ end
+ eval_erb(%(
+ <% joke_helper do |r| %>
+ <%= r.joke %>
+ <% end %>
+ )).should match_html("Wha...")
+ end
it "should give the yielded renderer access to other methods via 'helper'" do
eval_erb(%(
<% test_helper do |r| %>

0 comments on commit b002902

Please sign in to comment.