From 1f62443319c1b95c6e747a5de5e7e1041c64cbcb Mon Sep 17 00:00:00 2001 From: Andrew White Date: Wed, 2 Nov 2016 16:00:37 +0000 Subject: Fix inconsistencies in path with missing helpers Ruby 2.0 and later demonstrate some inconsistencies when a helper file is not found with the path method on LoadError. By creating a subclass of LoadError we can cater for the inconsistencies. --- actionpack/lib/abstract_controller/helpers.rb | 15 ++++++++++++++- actionpack/test/abstract/helper_test.rb | 5 ++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/abstract_controller/helpers.rb b/actionpack/lib/abstract_controller/helpers.rb index 77cc4c07d9..7a9055a679 100644 --- a/actionpack/lib/abstract_controller/helpers.rb +++ b/actionpack/lib/abstract_controller/helpers.rb @@ -12,6 +12,15 @@ module AbstractController self._helper_methods = Array.new end + class MissingHelperError < LoadError + def initialize(error, path) + @error = error + @path = "helpers/#{path}.rb" + set_backtrace error.backtrace + super("Missing helper file helpers/%s.rb" % path) + end + end + module ClassMethods # When a class is inherited, wrap its helper module in a new module. # This ensures that the parent class's module can be changed @@ -132,7 +141,11 @@ module AbstractController case arg when String, Symbol file_name = "#{arg.to_s.underscore}_helper" - require_dependency(file_name, "Missing helper file helpers/%s.rb") + begin + require_dependency(file_name) + rescue LoadError => e + raise MissingHelperError.new(e, file_name) + end file_name.camelize.constantize when Module arg diff --git a/actionpack/test/abstract/helper_test.rb b/actionpack/test/abstract/helper_test.rb index b28a5b5afb..2e86e7e859 100644 --- a/actionpack/test/abstract/helper_test.rb +++ b/actionpack/test/abstract/helper_test.rb @@ -69,7 +69,10 @@ module AbstractController end def test_declare_missing_helper - assert_raise(MissingSourceFile) { AbstractHelpers.helper :missing } + e = assert_raise AbstractController::Helpers::MissingHelperError do + AbstractHelpers.helper :missing + end + assert_equal "helpers/missing_helper.rb", e.path end def test_helpers_with_module_through_block -- cgit v1.2.3