Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #dig to ActionDispatch::Request::Session #32446

Merged
merged 1 commit into from
Apr 4, 2018

Conversation

sinsoku
Copy link
Contributor

@sinsoku sinsoku commented Apr 4, 2018

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:

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 #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.

### 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.
@rails-bot
Copy link

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.

@rafaelfranca rafaelfranca merged commit 8d62ff0 into rails:master Apr 4, 2018
@sinsoku sinsoku deleted the add_dig_to_session branch April 5, 2018 08:43
bogdanvlviv added a commit to bogdanvlviv/rails that referenced this pull request Apr 6, 2018
In rails#32446 was added method `dig` to `session`.
Improve docs of method `dig`.

[ci skip]
rafaelfranca added a commit that referenced this pull request Apr 6, 2018
feralbob added a commit to feralbob/passkeys-rails that referenced this pull request Nov 25, 2023
…r the Rails session

The Rails session is Hash like - but not a Hash - see rails/rails#32446
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants