Skip to content
This repository
Browse code

Show real LoadError on helpers require

When helper try to require missing file rails will throw exception about
missing helper.

  # app/helpers/my_helper.rb

  require 'missing'

  module MyHelper
  end

And when we try do load helper

  class ApplicationController
    helper :my
  end

Rails will throw exception. This is wrong because there is a helper
file.

  Missing helper file helpers/my_helper.rb

Now when helper try to require non-existed file rails will throw proper
exception.

  No such file to load -- missing
  • Loading branch information...
commit e0438b1c071d8dc2c7fc87075485d4ac01f4eb07 1 parent cd0d590
Piotr Niełacny LTe authored
7 actionpack/CHANGELOG.md
Source Rendered
... ... @@ -1,3 +1,10 @@
  1 +* Fix an issue where rails raise exception about missing helper where it
  2 + should throw `LoadError`. When helper file exists and only loaded file from
  3 + this helper does not exist rails should throw LoadError instead of
  4 + `MissingHelperError`.
  5 +
  6 + *Piotr Niełacny*
  7 +
1 8 * Fix `ActionDispatch::ParamsParser#parse_formatted_parameters` to rewind body input stream on
2 9 parsing json params.
3 10
7 actionpack/lib/abstract_controller/helpers.rb
@@ -150,7 +150,12 @@ def initialize(error, path)
150 150 @error = error
151 151 @path = "helpers/#{path}.rb"
152 152 set_backtrace error.backtrace
153   - super("Missing helper file helpers/%s.rb" % path)
  153 +
  154 + if error.path =~ /^#{path}(\.rb)?$/
  155 + super("Missing helper file helpers/%s.rb" % path)
  156 + else
  157 + raise error
  158 + end
154 159 end
155 160 end
156 161
BIN  actionpack/test/abstract/.helper_test.rb.swp
Binary file not shown
20 actionpack/test/abstract/helper_test.rb
@@ -48,6 +48,14 @@ class AbstractHelpersBlock < ControllerWithHelpers
48 48 end
49 49 end
50 50
  51 + class AbstractInvalidHelpers < AbstractHelpers
  52 + include ActionController::Helpers
  53 +
  54 + path = File.join(File.expand_path('../../fixtures', __FILE__), "helpers_missing")
  55 + $:.unshift(path)
  56 + self.helpers_path = path
  57 + end
  58 +
51 59 class TestHelpers < ActiveSupport::TestCase
52 60 def setup
53 61 @controller = AbstractHelpers.new
@@ -97,5 +105,17 @@ def test_includes_controller_default_helper
97 105 assert_equal "Hello Default", @controller.response_body
98 106 end
99 107 end
  108 +
  109 + class InvalidHelpersTest < ActiveSupport::TestCase
  110 + def test_controller_raise_error_about_real_require_problem
  111 + e = assert_raise(LoadError) { AbstractInvalidHelpers.helper(:invalid_require) }
  112 + assert_equal "No such file to load -- very_invalid_file_name", e.message
  113 + end
  114 +
  115 + def test_controller_raise_error_about_missing_helper
  116 + e = assert_raise(Helpers::ClassMethods::MissingHelperError) { AbstractInvalidHelpers.helper(:missing) }
  117 + assert_equal "Missing helper file helpers/missing_helper.rb", e.message
  118 + end
  119 + end
100 120 end
101 121 end
5 actionpack/test/fixtures/helpers_missing/invalid_require_helper.rb
... ... @@ -0,0 +1,5 @@
  1 +require 'very_invalid_file_name'
  2 +
  3 +module InvalidRequireHelper
  4 +end
  5 +

0 comments on commit e0438b1

Please sign in to comment.
Something went wrong with that request. Please try again.