Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ActionController::Parameters#dig
This method will only be added when used with Ruby 2.3.0 or greater. This method has the same behavior as `Hash#dig`, except it will convert hashes to `ActionController::Parameters`, similar to `#[]` and `#fetch`.
- Loading branch information
Showing
3 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -430,6 +430,21 @@ def fetch(key, *args) | |||
) | ||||
end | ||||
|
||||
if Hash.method_defined?(:dig) | ||||
# Extracts the nested parameter from the given +keys+ by calling +dig+ | ||||
# at each step. Returns +nil+ if any intermediate step is +nil+. | ||||
# | ||||
# params = ActionController::Parameters.new(foo: { bar: { baz: 1 } }) | ||||
# params.dig(:foo, :bar, :baz) # => 1 | ||||
# params.dig(:foo, :zot, :xyz) # => nil | ||||
# | ||||
# params2 = ActionController::Parameters.new(foo: [10, 11, 12]) | ||||
# params2.dig(:foo, 1) | ||||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
prathamesh-sonpatki
Member
|
# params2.dig(:foo, 1) # => 11 |
This file contains 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
@sgrif Can you add the output for
params2.dig(:foo, 1)
also?