mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge remote-tracking branch 'apotonick/simplify-prefixes'
This is the rebased version of #15026 Closes #15026
This commit is contained in:
commit
51a52cb836
3 changed files with 75 additions and 15 deletions
|
@ -1,3 +1,7 @@
|
|||
* Deprecate `AbstractController::Base::parent_prefixes`. Override `AbstractController::Base::local_prefixes` when you want to change where to find views.
|
||||
|
||||
*Nick Sutterer*
|
||||
|
||||
* Take label values into account when doing I18n lookups for model attributes.
|
||||
|
||||
The following:
|
||||
|
|
|
@ -14,29 +14,36 @@ module ActionView
|
|||
:locale, :locale=, :to => :lookup_context
|
||||
|
||||
module ClassMethods
|
||||
def parent_prefixes
|
||||
@parent_prefixes ||= begin
|
||||
parent_controller = superclass
|
||||
prefixes = []
|
||||
def _prefixes
|
||||
@_prefixes ||= begin
|
||||
deprecated_prefixes = handle_deprecated_parent_prefixes and return deprecated_prefixes
|
||||
|
||||
until parent_controller.abstract?
|
||||
prefixes << parent_controller.controller_path
|
||||
parent_controller = parent_controller.superclass
|
||||
end
|
||||
|
||||
prefixes
|
||||
return local_prefixes if superclass.abstract?
|
||||
local_prefixes + superclass._prefixes
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Override this method in your controller if you want to change paths prefixes for finding views.
|
||||
# Prefixes defined here will still be added to parents' <tt>::_prefixes</tt>.
|
||||
def local_prefixes
|
||||
[controller_path]
|
||||
end
|
||||
|
||||
def handle_deprecated_parent_prefixes # TODO: remove in 4.3/5.0.
|
||||
return unless respond_to?(:parent_prefixes)
|
||||
ActiveSupport::Deprecation.warn "Overriding ActionController::Base::parent_prefixes is deprecated, override ::local_prefixes or ::_prefixes instead."
|
||||
local_prefixes + parent_prefixes
|
||||
end
|
||||
end
|
||||
|
||||
# The prefixes used in render "foo" shortcuts.
|
||||
def _prefixes
|
||||
@_prefixes ||= begin
|
||||
parent_prefixes = self.class.parent_prefixes
|
||||
parent_prefixes.dup.unshift(controller_path)
|
||||
end
|
||||
self.class._prefixes
|
||||
end
|
||||
|
||||
|
||||
# LookupContext is the object responsible to hold all information required to lookup
|
||||
# templates, i.e. view paths and details. Check ActionView::LookupContext for more
|
||||
# information.
|
||||
|
@ -93,4 +100,4 @@ module ActionView
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -150,6 +150,55 @@ module AbstractController
|
|||
end
|
||||
end
|
||||
|
||||
class OverridingLocalPrefixes < AbstractController::Base
|
||||
include AbstractController::Rendering
|
||||
include ActionView::Rendering
|
||||
append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views"))
|
||||
|
||||
|
||||
def index
|
||||
render
|
||||
end
|
||||
|
||||
def self.local_prefixes
|
||||
# this would usually return "abstract_controller/testing/overriding_local_prefixes"
|
||||
super + ["abstract_controller/testing/me3"]
|
||||
end
|
||||
|
||||
class Inheriting < self
|
||||
end
|
||||
end
|
||||
|
||||
class OverridingLocalPrefixesTest < ActiveSupport::TestCase # TODO: remove me in 5.0/4.3.
|
||||
test "overriding ::local_prefixes adds prefix" do
|
||||
@controller = OverridingLocalPrefixes.new
|
||||
@controller.process(:index)
|
||||
assert_equal "Hello from me3/index.erb", @controller.response_body
|
||||
end
|
||||
|
||||
test "::local_prefixes is inherited" do
|
||||
@controller = OverridingLocalPrefixes::Inheriting.new
|
||||
@controller.process(:index)
|
||||
assert_equal "Hello from me3/index.erb", @controller.response_body
|
||||
end
|
||||
end
|
||||
|
||||
class DeprecatedParentPrefixes < OverridingLocalPrefixes
|
||||
def self.parent_prefixes
|
||||
["abstract_controller/testing/me3"]
|
||||
end
|
||||
end
|
||||
|
||||
class DeprecatedParentPrefixesTest < ActiveSupport::TestCase # TODO: remove me in 5.0/4.3.
|
||||
test "overriding ::parent_prefixes is deprecated" do
|
||||
@controller = DeprecatedParentPrefixes.new
|
||||
assert_deprecated do
|
||||
@controller.process(:index)
|
||||
end
|
||||
assert_equal "Hello from me3/index.erb", @controller.response_body
|
||||
end
|
||||
end
|
||||
|
||||
# Test rendering with layouts
|
||||
# ====
|
||||
# self._layout is used when defined
|
||||
|
|
Loading…
Reference in a new issue