Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use prepend instead of include for helpers. Fixes #1213 #1214

Merged
merged 1 commit into from Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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