mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
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:
parent
3f513fa165
commit
33087977ef
2 changed files with 19 additions and 3 deletions
|
@ -695,7 +695,7 @@ module Sinatra
|
||||||
|
|
||||||
public
|
public
|
||||||
def helpers(*extensions, &block)
|
def helpers(*extensions, &block)
|
||||||
extensions << Module.new(&block) if block
|
class_eval(&block) if block_given?
|
||||||
include *extensions
|
include *extensions
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -384,7 +384,7 @@ module HelperOne; def one; '1'; end; end
|
||||||
module HelperTwo; def two; '2'; end; end
|
module HelperTwo; def two; '2'; end; end
|
||||||
|
|
||||||
describe 'Adding new helpers' do
|
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 {
|
mock_app {
|
||||||
helpers HelperOne, HelperTwo
|
helpers HelperOne, HelperTwo
|
||||||
|
|
||||||
|
@ -404,7 +404,7 @@ describe 'Adding new helpers' do
|
||||||
assert_equal '2', body
|
assert_equal '2', body
|
||||||
end
|
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 {
|
mock_app {
|
||||||
helpers do
|
helpers do
|
||||||
def foo
|
def foo
|
||||||
|
@ -420,4 +420,20 @@ describe 'Adding new helpers' do
|
||||||
get '/'
|
get '/'
|
||||||
assert_equal 'foo', body
|
assert_equal 'foo', body
|
||||||
end
|
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 < 43', body
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue