Skip to content

Commit 4f0eb1c

Browse files
authored
Merge pull request rails#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
2 parents 57cc6f4 + 2587cad commit 4f0eb1c

File tree

4 files changed

+11
-0
lines changed

4 files changed

+11
-0
lines changed

actionpack/lib/action_dispatch/middleware/cookies.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ def key?(name)
338338
end
339339
alias :has_key? :key?
340340

341+
# Returns the cookies as Hash.
342+
alias :to_hash :to_h
343+
341344
def update(other_hash)
342345
@cookies.update other_hash.stringify_keys
343346
self

actionpack/lib/action_dispatch/request/session.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def to_hash
130130
load_for_read!
131131
@delegate.dup.delete_if { |_, v| v.nil? }
132132
end
133+
alias :to_h :to_hash
133134

134135
# Updates the session with given Hash.
135136
#

actionpack/test/dispatch/cookies_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ def test_key_is_to_s
3636
assert_equal "bar", request.cookie_jar.fetch(:foo)
3737
end
3838

39+
def test_to_hash
40+
request.cookie_jar["foo"] = "bar"
41+
assert_equal({ "foo" => "bar" }, request.cookie_jar.to_hash)
42+
assert_equal({ "foo" => "bar" }, request.cookie_jar.to_h)
43+
end
44+
3945
def test_fetch_type_error
4046
assert_raises(KeyError) do
4147
request.cookie_jar.fetch(:omglolwut)

actionpack/test/dispatch/request/session_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def test_to_hash
2222
s["foo"] = "bar"
2323
assert_equal "bar", s["foo"]
2424
assert_equal({ "foo" => "bar" }, s.to_hash)
25+
assert_equal({ "foo" => "bar" }, s.to_h)
2526
end
2627

2728
def test_create_merges_old

0 commit comments

Comments
 (0)