Skip to content

Commit

Permalink
Use prepend instead of include for helpers. Fixes #1213 (#1214)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwpastore committed Mar 18, 2020
1 parent ecf687d commit 2db247d
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 @@ -1439,7 +1439,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 @@ -232,7 +232,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 @@ -1947,5 +1947,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 2db247d

Please sign in to comment.