Merge pull request #44586 from Shopify/action-view-eager-loading

Eager load controllers `view_context_class`
This commit is contained in:
Jean Boussier 2022-03-02 09:22:50 +01:00 committed by GitHub
commit c6b6833ec4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 14 deletions

@ -58,6 +58,10 @@ def self.eager_load!
require "mail"
Mail.eager_autoload!
Base.descendants.each do |mailer|
mailer.eager_load! unless mailer.abstract?
end
end
end

@ -74,12 +74,6 @@ class Railtie < Rails::Railtie # :nodoc:
end
end
initializer "action_mailer.eager_load_actions" do
ActiveSupport.on_load(:after_initialize) do
ActionMailer::Base.descendants.each(&:action_methods) if config.eager_load
end
end
config.after_initialize do |app|
options = app.config.action_mailer

@ -24,5 +24,10 @@ module AbstractController
def self.eager_load!
super
AbstractController::Caching.eager_load!
AbstractController::Base.descendants.each do |controller|
unless controller.abstract?
controller.eager_load!
end
end
end
end

@ -127,6 +127,11 @@ def method_added(name)
super
clear_action_methods!
end
def eager_load! # :nodoc:
action_methods
nil
end
end
abstract!

@ -14,6 +14,7 @@ class Railtie < Rails::Railtie # :nodoc:
config.action_controller.log_query_tags_around_actions = true
config.action_controller.wrap_parameters_by_default = false
config.eager_load_namespaces << AbstractController
config.eager_load_namespaces << ActionController
initializer "action_controller.assets_config", group: :all do |app|
@ -99,12 +100,6 @@ class Railtie < Rails::Railtie # :nodoc:
end
end
initializer "action_controller.eager_load_actions" do
ActiveSupport.on_load(:after_initialize) do
ActionController::Metal.descendants.each(&:action_methods) if config.eager_load
end
end
initializer "action_controller.query_log_tags" do |app|
query_logs_tags_enabled = app.config.respond_to?(:active_record) &&
app.config.active_record.query_log_tags_enabled &&

@ -72,6 +72,12 @@ def build_view_context_class(klass, supports_path, routes, helpers)
end
end
def eager_load!
super
view_context_class
nil
end
def view_context_class
klass = ActionView::LookupContext::DetailsKey.view_context_class(ActionView::Base)

@ -291,7 +291,7 @@ def change
assert_instance_of Pathname, Rails.public_path
end
test "does not eager load controller actions in development" do
test "does not eager load controllers state actions in development" do
app_file "app/controllers/posts_controller.rb", <<-RUBY
class PostsController < ActionController::Base
def index;end
@ -302,9 +302,10 @@ def show;end
app "development"
assert_nil PostsController.instance_variable_get(:@action_methods)
assert_nil PostsController.instance_variable_get(:@view_context_class)
end
test "eager loads controller actions in production" do
test "eager loads controllers state in production" do
app_file "app/controllers/posts_controller.rb", <<-RUBY
class PostsController < ActionController::Base
def index;end
@ -320,6 +321,7 @@ def show;end
app "production"
assert_equal %w(index show).to_set, PostsController.instance_variable_get(:@action_methods)
assert_not_nil PostsController.instance_variable_get(:@view_context_class)
end
test "does not eager load mailer actions in development" do