Skip to content

Commit

Permalink
Merge pull request rails#44586 from Shopify/action-view-eager-loading
Browse files Browse the repository at this point in the history
Eager load controllers `view_context_class`
  • Loading branch information
byroot authored Mar 2, 2022
2 parents db0c464 + 2fd3427 commit c6b6833
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 14 deletions.
4 changes: 4 additions & 0 deletions actionmailer/lib/action_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 0 additions & 6 deletions actionmailer/lib/action_mailer/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions actionpack/lib/abstract_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions actionpack/lib/abstract_controller/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ def method_added(name)
super
clear_action_methods!
end

def eager_load! # :nodoc:
action_methods
nil
end
end

abstract!
Expand Down
7 changes: 1 addition & 6 deletions actionpack/lib/action_controller/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -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 &&
Expand Down
6 changes: 6 additions & 0 deletions actionview/lib/action_view/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 4 additions & 2 deletions railties/test/application/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c6b6833

Please sign in to comment.