1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/test/dispatch/request
claudiob 398267b142
Add #dig to ActionDispatch::Request::Session
### 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 ee5b621e2f.
2018-04-04 16:20:16 +09:00
..
json_params_parsing_test.rb Use frozen string literal in actionpack/ 2017-07-29 14:02:40 +03:00
multipart_params_parsing_test.rb Use frozen string literal in actionpack/ 2017-07-29 14:02:40 +03:00
query_string_parsing_test.rb Use frozen string literal in actionpack/ 2017-07-29 14:02:40 +03:00
session_test.rb Add #dig to ActionDispatch::Request::Session 2018-04-04 16:20:16 +09:00
url_encoded_params_parsing_test.rb Use frozen string literal in actionpack/ 2017-07-29 14:02:40 +03:00