前にこんな記事を書いた。
今度は config/initializers/*.rb
で Rails.application.config.action_mailer.deliver_later_queue_name
を書き換えたんだけど、反映されなかった。
いずれも config/application.rb
に書けば反映される。
うーん、config/initializers/*.rb
に書いていいのはどんな処理なんだろう。
なお、環境は Rails 5.1.4。
Rails の初期化プロセス
Rails の初期化プロセスについては Rails ガイドの Rails の初期化プロセス が詳しいが、railties-5.14/lib/rails/application.rb
のドキュメントにもまとめられている。
1) require "config/boot.rb" to setup load paths
2) require railties and engines
3) Define Rails.application as "class MyApp::Application < Rails::Application"
4) Run config.before_configuration callbacks
5) Load config/environments/ENV.rb
6) Run config.before_initialize callbacks
7) Run Railtie#initializer defined by railties, engines and application.
One by one, each engine sets up its load paths, routes and runs its config/initializers/* files.
8) Custom Railtie#initializers added by railties, engines and applications are executed
9) Build the middleware stack and run to_prepare callbacks
10) Run config.before_eager_load and eager_load! if eager_load is true
11) Run config.after_initialize callbacks
上記の 7 で ActionMailer::Railtie
などで定義された初期化処理と並んで、config/initializers/*
が実行される。
config/application.rb
はこれらより前に (上記の 3 で) 実行されるので ActionMailer などの設定が反映されることがわかる。
initializer の実行順
では config/initializers/*
はいつ実行されるのか。
rake initializers
で初期化時に実行される initializer の一覧が実行順で得られる。
rails new foo
で rake initializers | nl
した結果がこれだ。
1 ActionView::Railtie.set_load_path
2 ActionCable::Engine.set_load_path
3 Coffee::Rails::Engine.set_load_path
4 Turbolinks::Engine.set_load_path
5 Foo::Application.set_load_path
6 ActionView::Railtie.set_autoload_paths
7 ActionCable::Engine.set_autoload_paths
8 Coffee::Rails::Engine.set_autoload_paths
9 Turbolinks::Engine.set_autoload_paths
10 Foo::Application.set_autoload_paths
11 ActionView::Railtie.add_routing_paths
12 ActionCable::Engine.add_routing_paths
13 Coffee::Rails::Engine.add_routing_paths
14 Turbolinks::Engine.add_routing_paths
15 Foo::Application.add_routing_paths
16 ActionView::Railtie.add_locales
17 ActionCable::Engine.add_locales
18 Coffee::Rails::Engine.add_locales
19 Turbolinks::Engine.add_locales
20 Foo::Application.add_locales
21 ActionView::Railtie.add_view_paths
22 ActionCable::Engine.add_view_paths
23 Coffee::Rails::Engine.add_view_paths
24 Turbolinks::Engine.add_view_paths
25 Foo::Application.add_view_paths
26 ActionView::Railtie.load_environment_config
27 ActionCable::Engine.load_environment_config
28 Coffee::Rails::Engine.load_environment_config
29 Turbolinks::Engine.load_environment_config
30 Foo::Application.load_environment_config
31 Foo::Application.load_environment_hook
32 Foo::Application.load_active_support
33 Foo::Application.set_eager_load
34 Foo::Application.initialize_logger
35 Foo::Application.initialize_cache
36 Foo::Application.initialize_dependency_mechanism
37 Foo::Application.bootstrap_hook
38 Foo::Application.set_secrets_root
39 ActiveSupport::Railtie.active_support.deprecation_behavior
40 ActiveSupport::Railtie.active_support.initialize_time_zone
41 ActiveSupport::Railtie.active_support.initialize_beginning_of_week
42 ActiveSupport::Railtie.active_support.set_configs
43 ActionDispatch::Railtie.action_dispatch.configure
44 ActiveModel::Railtie.active_model.secure_password
45 ActionController::Railtie.action_controller.assets_config
46 ActionController::Railtie.action_controller.set_helpers_path
47 ActionController::Railtie.action_controller.parameters_config
48 ActionController::Railtie.action_controller.set_configs
49 ActionController::Railtie.action_controller.compile_config_methods
50 ActiveRecord::Railtie.active_record.initialize_timezone
51 ActiveRecord::Railtie.active_record.logger
52 ActiveRecord::Railtie.active_record.migration_error
53 ActiveRecord::Railtie.active_record.check_schema_cache_dump
54 ActiveRecord::Railtie.active_record.warn_on_records_fetched_greater_than
55 ActiveRecord::Railtie.active_record.set_configs
56 ActiveRecord::Railtie.active_record.initialize_database
57 ActiveRecord::Railtie.active_record.log_runtime
58 ActiveRecord::Railtie.active_record.set_reloader_hooks
59 ActiveRecord::Railtie.active_record.set_executor_hooks
60 ActiveRecord::Railtie.active_record.add_watchable_files
61 GlobalID::Railtie.global_id
62 ActiveJob::Railtie.active_job.logger
63 ActiveJob::Railtie.active_job.set_configs
64 ActiveJob::Railtie.active_job.set_reloader_hook
65 ActionMailer::Railtie.action_mailer.logger
66 ActionMailer::Railtie.action_mailer.set_configs
67 ActionMailer::Railtie.action_mailer.compile_config_methods
68 Rails::TestUnitRailtie.test_unit.line_filtering
69 Sprockets::Railtie.set_default_precompile
70 Sprockets::Railtie.quiet_assets
71 Sass::Rails::Railtie.setup_sass
72 Sass::Rails::Railtie.setup_compression
73 Jbuilder::Railtie.jbuilder
74 WebConsole::Railtie.web_console.initialize
75 WebConsole::Railtie.web_console.development_only
76 WebConsole::Railtie.web_console.insert_middleware
77 WebConsole::Railtie.web_console.mount_point
78 WebConsole::Railtie.web_console.template_paths
79 WebConsole::Railtie.web_console.whitelisted_ips
80 WebConsole::Railtie.web_console.whiny_requests
81 WebConsole::Railtie.i18n.load_path
82 ActionView::Railtie.prepend_helpers_path
83 ActionCable::Engine.prepend_helpers_path
84 Coffee::Rails::Engine.prepend_helpers_path
85 Turbolinks::Engine.prepend_helpers_path
86 Foo::Application.prepend_helpers_path
87 ActionView::Railtie.load_config_initializers
88 ActionCable::Engine.load_config_initializers
89 Coffee::Rails::Engine.load_config_initializers
90 Turbolinks::Engine.load_config_initializers
91 Foo::Application.load_config_initializers
92 ActionView::Railtie.engines_blank_point
93 ActionCable::Engine.engines_blank_point
94 Coffee::Rails::Engine.engines_blank_point
95 Turbolinks::Engine.engines_blank_point
96 Foo::Application.engines_blank_point
97 ActionView::Railtie.append_assets_path
98 ActionView::Railtie.action_view.embed_authenticity_token_in_remote_forms
99 ActionView::Railtie.action_view.form_with_generates_remote_forms
100 ActionView::Railtie.action_view.logger
101 ActionView::Railtie.action_view.set_configs
102 ActionView::Railtie.action_view.caching
103 ActionView::Railtie.action_view.per_request_digest_cache
104 ActionView::Railtie.action_view.setup_action_pack
105 ActionView::Railtie.action_view.collection_caching
106 ActionCable::Engine.append_assets_path
107 ActionCable::Engine.action_cable.helpers
108 ActionCable::Engine.action_cable.logger
109 ActionCable::Engine.action_cable.set_configs
110 ActionCable::Engine.action_cable.routes
111 ActionCable::Engine.action_cable.set_work_hooks
112 Coffee::Rails::Engine.append_assets_path
113 Coffee::Rails::Engine.override js_template hook
114 Turbolinks::Engine.append_assets_path
115 Turbolinks::Engine.turbolinks
116 Foo::Application.append_assets_path
117 Foo::Application.add_generator_templates
118 Foo::Application.ensure_autoload_once_paths_as_subset
119 Foo::Application.add_builtin_route
120 Foo::Application.setup_default_session_store
121 Foo::Application.build_middleware_stack
122 Foo::Application.define_main_app_helper
123 Foo::Application.add_to_prepare_blocks
124 Foo::Application.run_prepare_callbacks
125 Foo::Application.eager_load!
126 Foo::Application.finisher_hook
127 Foo::Application.configure_executor_for_concurrency
128 Foo::Application.set_routes_reloader_hook
129 Foo::Application.set_clear_dependencies_hook
130 Foo::Application.disable_dependency_loading
このうち、アプリケーションの config/initializers/*
を実行しているのは 91 行目の Foo::Application.load_config_initializers
だ。
いっぽう、time_zone を設定しているのは 40 行目の ActiveSupport::Railtie.active_support.initialize_time_zone
、ActionMailer の設定をしているのは 66 行目の ActionMailer::Railtie.action_mailer.set_configs
なので、config/initializers/*
より前に実行され、設定が反映されないことがわかる。
結局どうする?
config/initializers/*.rb
を書くときは rake initializers
を見て アプリケーション名::Application.load_config_initializers
より前に、それに関連しそうな処理がないか確認するくらいしか、いまのところ方法がなさそう。