1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #31844 from igorkasyanchuk/add_to_hash_and_to_h_for_session_and_cookies

Consistent behavior for session and cookies with to_h and to_hash method
This commit is contained in:
Ryuta Kamizono 2018-02-01 08:13:38 +09:00 committed by GitHub
commit 4f0eb1ccd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 0 deletions

View file

@ -338,6 +338,9 @@ module ActionDispatch
end
alias :has_key? :key?
# Returns the cookies as Hash.
alias :to_hash :to_h
def update(other_hash)
@cookies.update other_hash.stringify_keys
self

View file

@ -130,6 +130,7 @@ module ActionDispatch
load_for_read!
@delegate.dup.delete_if { |_, v| v.nil? }
end
alias :to_h :to_hash
# Updates the session with given Hash.
#

View file

@ -36,6 +36,12 @@ class CookieJarTest < ActiveSupport::TestCase
assert_equal "bar", request.cookie_jar.fetch(:foo)
end
def test_to_hash
request.cookie_jar["foo"] = "bar"
assert_equal({ "foo" => "bar" }, request.cookie_jar.to_hash)
assert_equal({ "foo" => "bar" }, request.cookie_jar.to_h)
end
def test_fetch_type_error
assert_raises(KeyError) do
request.cookie_jar.fetch(:omglolwut)

View file

@ -22,6 +22,7 @@ module ActionDispatch
s["foo"] = "bar"
assert_equal "bar", s["foo"]
assert_equal({ "foo" => "bar" }, s.to_hash)
assert_equal({ "foo" => "bar" }, s.to_h)
end
def test_create_merges_old