Skip to content

Commit

Permalink
Use prepend instead of include for helpers. Fixes #1213
Browse files Browse the repository at this point in the history
  • Loading branch information
mwpastore authored and jkowens committed Mar 18, 2020
1 parent 63e81bc commit 8607e8a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/sinatra/base.rb
Expand Up @@ -1416,7 +1416,7 @@ def unlink(path, opts = {}, &bk) route 'UNLINK', path, opts, &bk end
# in `extensions` available to the handlers and templates
def helpers(*extensions, &block)
class_eval(&block) if block_given?
include(*extensions) if extensions.any?
prepend(*extensions) if extensions.any?
end

# Register an extension. Alternatively take a block from which an
Expand Down
2 changes: 1 addition & 1 deletion sinatra-contrib/lib/sinatra/namespace.rb
Expand Up @@ -233,7 +233,7 @@ def self.prefixed(*names)

def helpers(*extensions, &block)
class_eval(&block) if block_given?
include(*extensions) if extensions.any?
prepend(*extensions) if extensions.any?
end

def register(*extensions, &block)
Expand Down
22 changes: 22 additions & 0 deletions test/helpers_test.rb
Expand Up @@ -1941,5 +1941,27 @@ def foo
assert ok?
assert_equal '42 < 43', body
end

it 'prepends modules so previously-defined methods can be overridden consistently' do
mock_app do
helpers do
def one; nil end
def two; nil end
end

helpers ::HelperOne do
def two; '2' end
end

get('/one') { one }
get('/two') { two }
end

get '/one'
assert_equal '1', body

get '/two'
assert_equal '2', body
end
end
end

0 comments on commit 8607e8a

Please sign in to comment.