From e0438b1c071d8dc2c7fc87075485d4ac01f4eb07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Nie=C5=82acny?= Date: Thu, 16 May 2013 15:37:19 +0200 Subject: [PATCH] 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 --- actionpack/CHANGELOG.md | 7 ++++++ actionpack/lib/abstract_controller/helpers.rb | 7 +++++- actionpack/test/abstract/.helper_test.rb.swp | Bin 0 -> 12288 bytes actionpack/test/abstract/helper_test.rb | 20 ++++++++++++++++++ .../helpers_missing/invalid_require_helper.rb | 5 +++++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 actionpack/test/abstract/.helper_test.rb.swp create mode 100644 actionpack/test/fixtures/helpers_missing/invalid_require_helper.rb diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index ea1d090bc2..3105dbcc42 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,3 +1,10 @@ +* Fix an issue where rails raise exception about missing helper where it + should throw `LoadError`. When helper file exists and only loaded file from + this helper does not exist rails should throw LoadError instead of + `MissingHelperError`. + + *Piotr Niełacny* + * Fix `ActionDispatch::ParamsParser#parse_formatted_parameters` to rewind body input stream on parsing json params. diff --git a/actionpack/lib/abstract_controller/helpers.rb b/actionpack/lib/abstract_controller/helpers.rb index 5ae8c6c3b0..a928205963 100644 --- a/actionpack/lib/abstract_controller/helpers.rb +++ b/actionpack/lib/abstract_controller/helpers.rb @@ -150,7 +150,12 @@ module AbstractController @error = error @path = "helpers/#{path}.rb" set_backtrace error.backtrace - super("Missing helper file helpers/%s.rb" % path) + + if error.path =~ /^#{path}(\.rb)?$/ + super("Missing helper file helpers/%s.rb" % path) + else + raise error + end end end diff --git a/actionpack/test/abstract/.helper_test.rb.swp b/actionpack/test/abstract/.helper_test.rb.swp new file mode 100644 index 0000000000000000000000000000000000000000..d029f89288cc011c0a3e8b84892c01ecbea225f0 GIT binary patch literal 12288 zcmeI2O^g&p6vqqk3lWxx7hMkJ2<%S6&Y&A3v@8hA64tONEJC6tZO?QK({4}K(AB*k zF<^qxi;4+1j2?_6#*f69C;p zTc45*t8+Y@sfLc{i>gzPJRY>2`b;%qBC0wy5rw!|Z8E>jLR(&}gtf&jnma}Tqd;E@ z#8G2p+ZrdhqkvJsC}0%$e<%>OSCE%5 zlw0(0LjAd-@TtF=52Julz$jo8FbWt2i~>dhqkvJsC}0#Y3K#|cg9B>wN)4=I1T2(95@8Z;Ey{AxdP6Ex4=2j z1lz#?xO@j8--55em*5o;g9-2$u)sR-^X=#h-UMgBb6^zwej6dbfveyr@BwIob>Pyi z@ELpt7QiZS5i$G(ybs<5GNx~Xv%mpQf`ec$*aZf_TJS0U^JgA-pb55tIlKoBj2Il{$XUZ6S3pd(v&BJ7aae`9jV$>LK~jX^tq_Elu=vVtFY^}p0rOp z_2{48q&U>K{FxG}JJ7U44{Ne#)=HsoN2Gqn8rDi%D66!X5YBN%62sCq9ZLcSf3QhM zu;Km5UL0Xr<30j9#iAy6@lA(A-JJ+S9+v5gbhqBYGa1Y}zUS(`6(#SmrR6Tullpls zYf!n%$FB1r3c2rN*H7|Tn@N+lQ{!=D=c7b)-&D(@3I{nAalNTR9dYV&$E719be4q+ zwx?XPWgggp(_;O}Z~!*?9cJedAyU^1+4T-LWR7p^zIHq0HJ`PTG1p}TXkObRP>=4Q zyRnsOmQBa)HV-4ql2^wa!IW01Qt4SyGla_LbT4Z-u^&N_$J2;ZLMGZg5X`P|cR{O9 zb1mRy9f>q-&3RGNuKBz^Q!a+s3+jICGLeln;xC`4=il;?outYamm;h+CuFu`(T3v- z8O0lMSuC__-2a=;b)Pw*u;Vu3z-B!j3+<}toYvtvB;Z#3O|m$IBwNt0ea<;y`~Y7}Ngu@K0~k5OP3tXQhL zC+U_J_4JHVU2eMxJKn89YI(rBlx3+$BsEHBt{NJ3dGcMYI0e^}Sq-mJi^ov)wYN%$ z)RClucIJD=X71UkB{^9A4zY;A)HKCn5V$O)mKXS5fH#+&wA6Fr?ASEcSvc&7?;5t$ z(ov?Hc6MgAOt_>PEuQ^7o(3PWYD)7;w}oWSaxb&FEK!ZA<<#oE5+^%EN=qt_b$lq5 zo@Sqx3!HvkOCnlcGK&2MFzZaIyNKwAN?&xMxppggH%#uRiJDyki+PbX+e27gTcolD z`@O`?*M+4r9|4ndO1r;Q%fm@-VU{Fq)kH}1G?Vd}luhqj)*k)(rKOM)S{l<|J4v4Q zAVj{y@gTjTZ0Hq6*m6n361xxYtra%kb^Ifs Xgl(7cd%b!Ur9+OMU3MIJ(Gd9yC=$)0 literal 0 HcmV?d00001 diff --git a/actionpack/test/abstract/helper_test.rb b/actionpack/test/abstract/helper_test.rb index 7960e5b55b..2cc27fbecd 100644 --- a/actionpack/test/abstract/helper_test.rb +++ b/actionpack/test/abstract/helper_test.rb @@ -48,6 +48,14 @@ module AbstractController end end + class AbstractInvalidHelpers < AbstractHelpers + include ActionController::Helpers + + path = File.join(File.expand_path('../../fixtures', __FILE__), "helpers_missing") + $:.unshift(path) + self.helpers_path = path + end + class TestHelpers < ActiveSupport::TestCase def setup @controller = AbstractHelpers.new @@ -97,5 +105,17 @@ module AbstractController assert_equal "Hello Default", @controller.response_body end end + + class InvalidHelpersTest < ActiveSupport::TestCase + def test_controller_raise_error_about_real_require_problem + e = assert_raise(LoadError) { AbstractInvalidHelpers.helper(:invalid_require) } + assert_equal "No such file to load -- very_invalid_file_name", e.message + end + + def test_controller_raise_error_about_missing_helper + e = assert_raise(Helpers::ClassMethods::MissingHelperError) { AbstractInvalidHelpers.helper(:missing) } + assert_equal "Missing helper file helpers/missing_helper.rb", e.message + end + end end end diff --git a/actionpack/test/fixtures/helpers_missing/invalid_require_helper.rb b/actionpack/test/fixtures/helpers_missing/invalid_require_helper.rb new file mode 100644 index 0000000000..d8801e54d5 --- /dev/null +++ b/actionpack/test/fixtures/helpers_missing/invalid_require_helper.rb @@ -0,0 +1,5 @@ +require 'very_invalid_file_name' + +module InvalidRequireHelper +end +