Skip to content

Commit 51a52cb

Browse files
committed
Merge remote-tracking branch 'apotonick/simplify-prefixes'
This is the rebased version of rails#15026 Closes rails#15026
2 parents 307a519 + b8ad4b5 commit 51a52cb

File tree

3 files changed

+75
-15
lines changed

3 files changed

+75
-15
lines changed

actionview/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Deprecate `AbstractController::Base::parent_prefixes`. Override `AbstractController::Base::local_prefixes` when you want to change where to find views.
2+
3+
*Nick Sutterer*
4+
15
* Take label values into account when doing I18n lookups for model attributes.
26

37
The following:

actionview/lib/action_view/view_paths.rb

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,36 @@ module ViewPaths
1414
:locale, :locale=, :to => :lookup_context
1515

1616
module ClassMethods
17-
def parent_prefixes
18-
@parent_prefixes ||= begin
19-
parent_controller = superclass
20-
prefixes = []
17+
def _prefixes
18+
@_prefixes ||= begin
19+
deprecated_prefixes = handle_deprecated_parent_prefixes and return deprecated_prefixes
2120

22-
until parent_controller.abstract?
23-
prefixes << parent_controller.controller_path
24-
parent_controller = parent_controller.superclass
25-
end
26-
27-
prefixes
21+
return local_prefixes if superclass.abstract?
22+
local_prefixes + superclass._prefixes
2823
end
2924
end
25+
26+
private
27+
28+
# Override this method in your controller if you want to change paths prefixes for finding views.
29+
# Prefixes defined here will still be added to parents' <tt>::_prefixes</tt>.
30+
def local_prefixes
31+
[controller_path]
32+
end
33+
34+
def handle_deprecated_parent_prefixes # TODO: remove in 4.3/5.0.
35+
return unless respond_to?(:parent_prefixes)
36+
ActiveSupport::Deprecation.warn "Overriding ActionController::Base::parent_prefixes is deprecated, override ::local_prefixes or ::_prefixes instead."
37+
local_prefixes + parent_prefixes
38+
end
3039
end
3140

3241
# The prefixes used in render "foo" shortcuts.
3342
def _prefixes
34-
@_prefixes ||= begin
35-
parent_prefixes = self.class.parent_prefixes
36-
parent_prefixes.dup.unshift(controller_path)
37-
end
43+
self.class._prefixes
3844
end
3945

46+
4047
# LookupContext is the object responsible to hold all information required to lookup
4148
# templates, i.e. view paths and details. Check ActionView::LookupContext for more
4249
# information.
@@ -93,4 +100,4 @@ def view_paths=(paths)
93100
end
94101
end
95102
end
96-
end
103+
end

actionview/test/actionpack/abstract/abstract_controller_test.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,55 @@ def setup
150150
end
151151
end
152152

153+
class OverridingLocalPrefixes < AbstractController::Base
154+
include AbstractController::Rendering
155+
include ActionView::Rendering
156+
append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views"))
157+
158+
159+
def index
160+
render
161+
end
162+
163+
def self.local_prefixes
164+
# this would usually return "abstract_controller/testing/overriding_local_prefixes"
165+
super + ["abstract_controller/testing/me3"]
166+
end
167+
168+
class Inheriting < self
169+
end
170+
end
171+
172+
class OverridingLocalPrefixesTest < ActiveSupport::TestCase # TODO: remove me in 5.0/4.3.
173+
test "overriding ::local_prefixes adds prefix" do
174+
@controller = OverridingLocalPrefixes.new
175+
@controller.process(:index)
176+
assert_equal "Hello from me3/index.erb", @controller.response_body
177+
end
178+
179+
test "::local_prefixes is inherited" do
180+
@controller = OverridingLocalPrefixes::Inheriting.new
181+
@controller.process(:index)
182+
assert_equal "Hello from me3/index.erb", @controller.response_body
183+
end
184+
end
185+
186+
class DeprecatedParentPrefixes < OverridingLocalPrefixes
187+
def self.parent_prefixes
188+
["abstract_controller/testing/me3"]
189+
end
190+
end
191+
192+
class DeprecatedParentPrefixesTest < ActiveSupport::TestCase # TODO: remove me in 5.0/4.3.
193+
test "overriding ::parent_prefixes is deprecated" do
194+
@controller = DeprecatedParentPrefixes.new
195+
assert_deprecated do
196+
@controller.process(:index)
197+
end
198+
assert_equal "Hello from me3/index.erb", @controller.response_body
199+
end
200+
end
201+
153202
# Test rendering with layouts
154203
# ====
155204
# self._layout is used when defined

0 commit comments

Comments
 (0)