1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Delay ActionDispatch::Response configuration to load-time

It fixes the problem in propagating return_only_media_type_on_content_type
and fixes the corresponding test being ineffective.

The mentioned test addes the following line:
...config.action_dispatch.return_only_media_type_on_content_type = true
to the config and checks if it takes effect. However, in this scenario,
the value is already true before this line.
Moreover, the users are supposed to flip this from true to false in real
situations.

This commit flips the config in the test, making it to fail as
expected. The next commit will fix the failure.

In order for return_only_media_type_on_content_type to appropriately
take effect on ActionDispatch::Response, we want to know when
ActionDispatch::Response is loaded.
As load hooks for ActionDispatch would be too broad, the appropriate
registry is for ActionDispatch::Response itself.

Looking into other examples, a hook name is a full class name in
snake case with `_base` suffix omitted, if any. Therefore, in this case,
:action_dispatch_response seems appropriate.
This commit is contained in:
Masaki Hara 2019-09-30 14:40:49 +09:00
parent eaff375de4
commit 89f5789aad
3 changed files with 9 additions and 5 deletions

View file

@ -540,4 +540,6 @@ module ActionDispatch # :nodoc:
end
end
end
ActiveSupport.run_load_hooks(:action_dispatch_response, Response)
end

View file

@ -42,9 +42,11 @@ module ActionDispatch
ActionDispatch::Http::URL.tld_length = app.config.action_dispatch.tld_length
ActionDispatch::Request.ignore_accept_header = app.config.action_dispatch.ignore_accept_header
ActionDispatch::Request::Utils.perform_deep_munge = app.config.action_dispatch.perform_deep_munge
ActionDispatch::Response.default_charset = app.config.action_dispatch.default_charset || app.config.encoding
ActionDispatch::Response.default_headers = app.config.action_dispatch.default_headers
ActionDispatch::Response.return_only_media_type_on_content_type = app.config.action_dispatch.return_only_media_type_on_content_type
ActiveSupport.on_load(:action_dispatch_response) do
self.default_charset = app.config.action_dispatch.default_charset || app.config.encoding
self.default_headers = app.config.action_dispatch.default_headers
self.return_only_media_type_on_content_type = app.config.action_dispatch.return_only_media_type_on_content_type
end
ActionDispatch::ExceptionWrapper.rescue_responses.merge!(config.action_dispatch.rescue_responses)
ActionDispatch::ExceptionWrapper.rescue_templates.merge!(config.action_dispatch.rescue_templates)

View file

@ -2472,12 +2472,12 @@ module ApplicationTests
remove_from_config '.*config\.load_defaults.*\n'
app_file "config/initializers/new_framework_defaults_6_0.rb", <<-RUBY
Rails.application.config.action_dispatch.return_only_media_type_on_content_type = true
Rails.application.config.action_dispatch.return_only_media_type_on_content_type = false
RUBY
app "development"
assert_equal true, ActionDispatch::Response.return_only_media_type_on_content_type
assert_equal false, ActionDispatch::Response.return_only_media_type_on_content_type
end
test "ActionMailbox.logger is Rails.logger by default" do