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

Consistent behavior for session and cookies with to_h and to_hash method

This commit is contained in:
Igor Kasyanchuk 2018-01-31 13:36:01 -08:00
parent 148d0077c5
commit 2587cadd42
4 changed files with 11 additions and 0 deletions

View file

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

View file

@ -130,6 +130,7 @@ module ActionDispatch
load_for_read! load_for_read!
@delegate.dup.delete_if { |_, v| v.nil? } @delegate.dup.delete_if { |_, v| v.nil? }
end end
alias :to_h :to_hash
# Updates the session with given 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) assert_equal "bar", request.cookie_jar.fetch(:foo)
end 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 def test_fetch_type_error
assert_raises(KeyError) do assert_raises(KeyError) do
request.cookie_jar.fetch(:omglolwut) request.cookie_jar.fetch(:omglolwut)

View file

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