mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make AP test suite green once again and speed up performance in layouts lookup for some cases.
This commit is contained in:
parent
b3d2080278
commit
5273bd97e6
4 changed files with 39 additions and 45 deletions
|
@ -184,7 +184,7 @@ module ActionView #:nodoc:
|
||||||
attr_internal :captures, :request, :controller, :template, :config
|
attr_internal :captures, :request, :controller, :template, :config
|
||||||
|
|
||||||
delegate :find_template, :template_exists?, :formats, :formats=, :locale, :locale=,
|
delegate :find_template, :template_exists?, :formats, :formats=, :locale, :locale=,
|
||||||
:view_paths, :view_paths=, :with_fallbacks, :update_details, :to => :lookup_context
|
:view_paths, :view_paths=, :with_fallbacks, :update_details, :with_layout_format, :to => :lookup_context
|
||||||
|
|
||||||
delegate :request_forgery_protection_token, :template, :params, :session, :cookies, :response, :headers,
|
delegate :request_forgery_protection_token, :template, :params, :session, :cookies, :response, :headers,
|
||||||
:flash, :action_name, :controller_name, :to => :controller
|
:flash, :action_name, :controller_name, :to => :controller
|
||||||
|
|
|
@ -19,6 +19,7 @@ module ActionView
|
||||||
def self.register_detail(name, options = {}, &block)
|
def self.register_detail(name, options = {}, &block)
|
||||||
self.registered_details << name
|
self.registered_details << name
|
||||||
self.registered_detail_setters << [name, "#{name}="]
|
self.registered_detail_setters << [name, "#{name}="]
|
||||||
|
|
||||||
Accessors.send :define_method, :"_#{name}_defaults", &block
|
Accessors.send :define_method, :"_#{name}_defaults", &block
|
||||||
Accessors.module_eval <<-METHOD, __FILE__, __LINE__ + 1
|
Accessors.module_eval <<-METHOD, __FILE__, __LINE__ + 1
|
||||||
def #{name}
|
def #{name}
|
||||||
|
@ -27,12 +28,7 @@ module ActionView
|
||||||
|
|
||||||
def #{name}=(value)
|
def #{name}=(value)
|
||||||
value = Array.wrap(value.presence || _#{name}_defaults)
|
value = Array.wrap(value.presence || _#{name}_defaults)
|
||||||
|
_set_detail(:#{name}, value) if value != @details[:#{name}]
|
||||||
if value != @details[:#{name}]
|
|
||||||
@details_key = nil
|
|
||||||
@details = @details.dup if @details.frozen?
|
|
||||||
@details[:#{name}] = value.freeze
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
METHOD
|
METHOD
|
||||||
end
|
end
|
||||||
|
@ -63,8 +59,11 @@ module ActionView
|
||||||
def initialize(view_paths, details = {})
|
def initialize(view_paths, details = {})
|
||||||
@details, @details_key = { :handlers => default_handlers }, nil
|
@details, @details_key = { :handlers => default_handlers }, nil
|
||||||
@frozen_formats, @skip_default_locale = false, false
|
@frozen_formats, @skip_default_locale = false, false
|
||||||
|
|
||||||
self.view_paths = view_paths
|
self.view_paths = view_paths
|
||||||
self.initialize_details(details)
|
self.registered_detail_setters.each do |key, setter|
|
||||||
|
send(setter, details[key])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module ViewPaths
|
module ViewPaths
|
||||||
|
@ -177,11 +176,20 @@ module ActionView
|
||||||
super(@skip_default_locale ? I18n.locale : _locale_defaults)
|
super(@skip_default_locale ? I18n.locale : _locale_defaults)
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize_details(details)
|
# A method which only uses the first format in the formats array for layout lookup.
|
||||||
details = details.dup
|
# This method plays straight with instance variables for performance reasons.
|
||||||
|
def with_layout_format
|
||||||
registered_detail_setters.each do |key, setter|
|
if formats.size == 1
|
||||||
send(setter, details[key])
|
yield
|
||||||
|
else
|
||||||
|
old_formats = formats
|
||||||
|
_set_detail(:formats, formats[0,1])
|
||||||
|
|
||||||
|
begin
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
_set_detail(:formats, formats)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -195,14 +203,21 @@ module ActionView
|
||||||
send(setter, new_details[key]) if new_details.key?(key)
|
send(setter, new_details[key]) if new_details.key?(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
if block_given?
|
begin
|
||||||
begin
|
yield
|
||||||
yield
|
ensure
|
||||||
ensure
|
@details_key = nil
|
||||||
@details = old_details
|
@details = old_details
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def _set_detail(key, value)
|
||||||
|
@details_key = nil
|
||||||
|
@details = @details.dup if @details.frozen?
|
||||||
|
@details[key] = value.freeze
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
include Accessors
|
include Accessors
|
||||||
|
|
|
@ -57,15 +57,11 @@ module ActionView
|
||||||
# This is the method which actually finds the layout using details in the lookup
|
# This is the method which actually finds the layout using details in the lookup
|
||||||
# context object. If no layout is found, it checkes if at least a layout with
|
# context object. If no layout is found, it checkes if at least a layout with
|
||||||
# the given name exists across all details before raising the error.
|
# the given name exists across all details before raising the error.
|
||||||
#
|
|
||||||
# If self.formats contains several formats, just the first one is considered in
|
|
||||||
# the layout lookup.
|
|
||||||
def find_layout(layout)
|
def find_layout(layout)
|
||||||
begin
|
begin
|
||||||
if formats.size == 1
|
with_layout_format do
|
||||||
_find_layout(layout)
|
layout =~ /^\// ?
|
||||||
else
|
with_fallbacks { find_template(layout) } : find_template(layout)
|
||||||
update_details(:formats => [self.formats.first]) { _find_layout(layout) }
|
|
||||||
end
|
end
|
||||||
rescue ActionView::MissingTemplate => e
|
rescue ActionView::MissingTemplate => e
|
||||||
update_details(:formats => nil) do
|
update_details(:formats => nil) do
|
||||||
|
@ -74,11 +70,6 @@ module ActionView
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def _find_layout(layout) #:nodoc:
|
|
||||||
layout =~ /^\// ?
|
|
||||||
with_fallbacks { find_template(layout) } : find_template(layout)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Contains the logic that actually renders the layout.
|
# Contains the logic that actually renders the layout.
|
||||||
def _render_layout(layout, locals, &block) #:nodoc:
|
def _render_layout(layout, locals, &block) #:nodoc:
|
||||||
layout.render(self, locals){ |*name| _layout_for(*name, &block) }
|
layout.render(self, locals){ |*name| _layout_for(*name, &block) }
|
||||||
|
|
|
@ -26,18 +26,6 @@ class LookupContextTest < ActiveSupport::TestCase
|
||||||
assert_equal :en, @lookup_context.locale
|
assert_equal :en, @lookup_context.locale
|
||||||
end
|
end
|
||||||
|
|
||||||
test "allows me to update details" do
|
|
||||||
@lookup_context.update_details(:formats => [:html], :locale => :pt)
|
|
||||||
assert_equal [:html], @lookup_context.formats
|
|
||||||
assert_equal :pt, @lookup_context.locale
|
|
||||||
end
|
|
||||||
|
|
||||||
test "allows me to update an specific detail" do
|
|
||||||
@lookup_context.update_details(:locale => :pt)
|
|
||||||
assert_equal :pt, I18n.locale
|
|
||||||
assert_equal :pt, @lookup_context.locale
|
|
||||||
end
|
|
||||||
|
|
||||||
test "allows me to freeze and retrieve frozen formats" do
|
test "allows me to freeze and retrieve frozen formats" do
|
||||||
@lookup_context.formats.freeze
|
@lookup_context.formats.freeze
|
||||||
assert @lookup_context.formats.frozen?
|
assert @lookup_context.formats.frozen?
|
||||||
|
@ -54,7 +42,7 @@ class LookupContextTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test "provides getters and setters for formats" do
|
test "provides getters and setters for formats" do
|
||||||
@lookup_context.formats = :html
|
@lookup_context.formats = [:html]
|
||||||
assert_equal [:html], @lookup_context.formats
|
assert_equal [:html], @lookup_context.formats
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -138,7 +126,7 @@ class LookupContextTest < ActiveSupport::TestCase
|
||||||
keys << @lookup_context.details_key
|
keys << @lookup_context.details_key
|
||||||
assert_equal 2, keys.uniq.size
|
assert_equal 2, keys.uniq.size
|
||||||
|
|
||||||
@lookup_context.formats = :html
|
@lookup_context.formats = [:html]
|
||||||
keys << @lookup_context.details_key
|
keys << @lookup_context.details_key
|
||||||
assert_equal 3, keys.uniq.size
|
assert_equal 3, keys.uniq.size
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue