Fix helpers blocks failing when aliasing Base methods

The following would fail with a "undefined method `escape_html'"
exception because the block was being evaluated within a new module
and then included in Base. i.e., the following no longer worked:

    helpers { alias_method :h, :escape_html }

This was introduced just recently as part of the extensions work
foca did.
This commit is contained in:
Ryan Tomayko 2009-02-07 11:23:05 -08:00
parent 3f513fa165
commit 33087977ef
2 changed files with 19 additions and 3 deletions

View File

@ -695,7 +695,7 @@ module Sinatra
public
def helpers(*extensions, &block)
extensions << Module.new(&block) if block
class_eval(&block) if block_given?
include *extensions
end

View File

@ -384,7 +384,7 @@ module HelperOne; def one; '1'; end; end
module HelperTwo; def two; '2'; end; end
describe 'Adding new helpers' do
it 'should allow passing a list of modules' do
it 'takes a list of modules to mix into the app' do
mock_app {
helpers HelperOne, HelperTwo
@ -404,7 +404,7 @@ describe 'Adding new helpers' do
assert_equal '2', body
end
it 'should take a block and mix it into the app' do
it 'takes a block to mix into the app' do
mock_app {
helpers do
def foo
@ -420,4 +420,20 @@ describe 'Adding new helpers' do
get '/'
assert_equal 'foo', body
end
it 'evaluates the block in class context so that methods can be aliased' do
mock_app {
helpers do
alias_method :h, :escape_html
end
get '/' do
h('42 < 43')
end
}
get '/'
assert ok?
assert_equal '42 &lt; 43', body
end
end