mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
398267b142
### Summary
The `session` object is not a real Hash but responds to many methods of Hash
such as `[]`, `[]`, `fetch`, `has_key?`.
Since Ruby 2.3, Hash also supports a `dig` method.
This commit adds a `dig` method to `ActionDispatch::Request::Session` with the
same behavior as `Hash#dig`.
This is useful if you store a hash in your session, such as:
```ruby
session[:user] = { id: 1, avatar_url: "http://example.org/nyancat.jpg" }
```
Then you can shorten your code from `session[:user][:avatar_url]` to `session.dig :user, :avatar_url`.
### Other Information
I cherry-picked a commit from https://github.com/rails/rails/pull/23864, and modify a bit.
The changes are below:
* Converts only the first key to a string adjust to the `fetch` method.
* Fixes a test case because we cannot use the indifferent access since
|
||
---|---|---|
.. | ||
json_params_parsing_test.rb | ||
multipart_params_parsing_test.rb | ||
query_string_parsing_test.rb | ||
session_test.rb | ||
url_encoded_params_parsing_test.rb |