diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-08-11 17:54:04 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-11 17:54:04 -0400 |
commit | b9e0b4f19940fdd7105db3dffc507cbd89ac3705 (patch) | |
tree | 3f24b1728f9bf86c029a7897c91e1fa36d43204d | |
parent | c8bbeb31dd402bb4a907a9b8ef021bf8e1afe2aa (diff) | |
parent | 0668c22a41e4c88ccb7600fc5b4bfb1a5b76e45d (diff) | |
download | rails-b9e0b4f19940fdd7105db3dffc507cbd89ac3705.tar.gz rails-b9e0b4f19940fdd7105db3dffc507cbd89ac3705.tar.bz2 rails-b9e0b4f19940fdd7105db3dffc507cbd89ac3705.zip |
Merge pull request #29559 from kirs/eager-load-controller-actions
Eager load controller actions to reduce response time of the first request
-rw-r--r-- | actionmailer/lib/action_mailer/railtie.rb | 6 | ||||
-rw-r--r-- | actionpack/lib/action_controller/railtie.rb | 6 | ||||
-rw-r--r-- | railties/test/application/configuration_test.rb | 60 |
3 files changed, 72 insertions, 0 deletions
diff --git a/actionmailer/lib/action_mailer/railtie.rb b/actionmailer/lib/action_mailer/railtie.rb index 36c2e5866d..69578471b0 100644 --- a/actionmailer/lib/action_mailer/railtie.rb +++ b/actionmailer/lib/action_mailer/railtie.rb @@ -58,6 +58,12 @@ module ActionMailer 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 diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb index 7dea22931c..cc02c9a53a 100644 --- a/actionpack/lib/action_controller/railtie.rb +++ b/actionpack/lib/action_controller/railtie.rb @@ -79,5 +79,11 @@ module ActionController end 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 end end diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index c2f6a5a95c..482da98a45 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -238,6 +238,66 @@ module ApplicationTests assert_instance_of Pathname, Rails.public_path end + test "does not eager load controller actions in development" do + app_file "app/controllers/posts_controller.rb", <<-RUBY + class PostsController < ActionController::Base + def index;end + def show;end + end + RUBY + + app "development" + + assert_nil PostsController.instance_variable_get(:@action_methods) + end + + test "eager loads controller actions in production" do + app_file "app/controllers/posts_controller.rb", <<-RUBY + class PostsController < ActionController::Base + def index;end + def show;end + end + RUBY + + add_to_config <<-RUBY + config.eager_load = true + config.cache_classes = true + RUBY + + app "production" + + assert_equal %w(index show).to_set, PostsController.instance_variable_get(:@action_methods) + end + + test "does not eager load mailer actions in development" do + app_file "app/mailers/posts_mailer.rb", <<-RUBY + class PostsMailer < ActionMailer::Base + def noop_email;end + end + RUBY + + app "development" + + assert_nil PostsMailer.instance_variable_get(:@action_methods) + end + + test "eager loads mailer actions in production" do + app_file "app/mailers/posts_mailer.rb", <<-RUBY + class PostsMailer < ActionMailer::Base + def noop_email;end + end + RUBY + + add_to_config <<-RUBY + config.eager_load = true + config.cache_classes = true + RUBY + + app "production" + + assert_equal %w(noop_email).to_set, PostsMailer.instance_variable_get(:@action_methods) + end + test "initialize an eager loaded, cache classes app" do add_to_config <<-RUBY config.eager_load = true |