Deprecate mutating the default_url_options: - #63
Open
Edouard-chin wants to merge 1 commit into
Open
Conversation
- ### Context
This patch is about the `default_url_options` used to tweak the generated
urls when calling for instance `my_route_path` inside
*controllers/views*. There is another `default_url_options` at the RouteSet
but this patch is not about this one.
### Problem
Mutating the `default_url_options` is nowhere documented and the
recommended way to customize generated urls is to override the
`default_url_options` method and override it.
The issue being that the Rails tests suite uses that pattern, and
while I don't know whether this is also done in the wild, I picked
the precautious path.
Mutating the `default_url_options` can create super weird behaviour:
```ruby
class ApplicationController
def hello_world
redirect_to(root_path)
end
end
class MyController < ApplicationController
default_url_options[:lang] = "en"
end
```
In a autoloaded context, if a user hit the `/hello_world` path
it will get redirected to `/`. Once the `MyController` is autoloaded
, revisiting `/hello_world` will this time redirect to `/?lang=en`.
### Solution
This patch freeze the default_url_options by default but still allows
the hash to be mutated and trigger a deprecation message.
This is done using a proxy which rescue from FrozenError anytime a
mutation happens and duplicate the underlying hash.
The `default_url_options` w
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
This patch is about the
default_url_optionsused to tweak the generated urls when calling for instancemy_route_pathinside controllers/views. There is anotherdefault_url_optionsat the RouteSet but this patch is not about this one.Problem
Mutating the
default_url_optionsis nowhere documented and the recommended way to customize generated urls is to override thedefault_url_optionsmethod and override it.The issue being that the Rails tests suite uses that pattern, and while I don't know whether this is also done in the wild, I picked the precautious path.
Mutating the
default_url_optionscan create super weird behaviour:In a autoloaded context, if a user hit the
/hello_worldpath it will get redirected to/. Once theMyControlleris autoloaded , revisiting/hello_worldwill this time redirect to/?lang=en.Solution
This patch freeze the default_url_options by default but still allows the hash to be mutated and trigger a deprecation message.
This is done using a proxy which rescue from FrozenError anytime a mutation happens and duplicate the underlying hash.