Add #dig to ActionDispatch::Request::Session#32446
Conversation
### 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 rails#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 ee5b621.
|
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @eileencodes (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
In rails#32446 was added method `dig` to `session`. Improve docs of method `dig`. [ci skip]
…ssion-23864 Add changelog entry for #32446
…r the Rails session The Rails session is Hash like - but not a Hash - see rails/rails#32446
Summary
The
sessionobject is not a real Hash but responds to many methods of Hashsuch as
[],[],fetch,has_key?.Since Ruby 2.3, Hash also supports a
digmethod.This commit adds a
digmethod toActionDispatch::Request::Sessionwith thesame behavior as
Hash#dig.This is useful if you store a hash in your session, such as:
Then you can shorten your code from
session[:user][:avatar_url]tosession.dig :user, :avatar_url.Other Information
I cherry-picked a commit from #23864, and modify a bit.
The changes are below:
fetchmethod.