diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index f33f475161..cb1945c5af 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -1441,7 +1441,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? - prepend(*extensions) if extensions.any? + include(*extensions) if extensions.any? end # Register an extension. Alternatively take a block from which an diff --git a/sinatra-contrib/lib/sinatra/namespace.rb b/sinatra-contrib/lib/sinatra/namespace.rb index 2aa7231718..a482666214 100644 --- a/sinatra-contrib/lib/sinatra/namespace.rb +++ b/sinatra-contrib/lib/sinatra/namespace.rb @@ -238,7 +238,7 @@ def self.prefixed(*names) def helpers(*extensions, &block) class_eval(&block) if block_given? - prepend(*extensions) if extensions.any? + include(*extensions) if extensions.any? end def register(*extensions, &block) diff --git a/test/helpers_test.rb b/test/helpers_test.rb index 9b56ad3cd7..c48da4f54b 100644 --- a/test/helpers_test.rb +++ b/test/helpers_test.rb @@ -1949,6 +1949,10 @@ def foo end it 'prepends modules so previously-defined methods can be overridden consistently' do + skip <<-EOS + This test will be helpful after switching #helpers's code from Module#include to Module#prepend + See more details: https://github.com/sinatra/sinatra/pull/1214 + EOS mock_app do helpers do def one; nil end @@ -1969,5 +1973,53 @@ def two; '2' end get '/two' assert_equal '2', body end + + module HelpersOverloadingBaseHelper + def my_test + 'BaseHelper#test' + end + end + + class HelpersOverloadingIncludeAndOverride < Sinatra::Base + helpers HelpersOverloadingBaseHelper + + get '/' do + my_test + end + + helpers do + def my_test + 'InlineHelper#test' + end + end + end + + it 'uses overloaded inline helper' do + mock_app(HelpersOverloadingIncludeAndOverride) + get '/' + assert ok? + assert_equal 'InlineHelper#test', body + end + + module HelperWithIncluded + def self.included(base) + base.extend(ClassMethods) + end + + module ClassMethods + def nickname(name) + # do something. + end + end + end + + class ServerApp < Sinatra::Base + helpers HelperWithIncluded + # `nickname` method should be available. + end + + it 'calls included method of helpers' do + assert ServerApp.respond_to?(:nickname) + end end end