From fdd619e9a7a5b9457f77e6322c920b99c3c09599 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 1 Sep 2011 13:37:14 -0700 Subject: CookieJar is enumerable. fixes #2795 --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_dispatch/middleware/cookies.rb | 5 +++++ actionpack/test/dispatch/cookies_test.rb | 16 ++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index dd27325055..71f38797ae 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -30,6 +30,8 @@ *Rails 3.1.1 (unreleased)* +* CookieJar is now Enumerable. Fixes #2795 + * Fixed AssetNotPrecompiled error raised when rake assets:precompile is compiling certain .erb files. [Guillermo Iguaran] * Manifest is correctly placed in assets path when default assets prefix is changed. [Guillermo Iguaran] diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb index 1c312f2587..8c4615c0c1 100644 --- a/actionpack/lib/action_dispatch/middleware/cookies.rb +++ b/actionpack/lib/action_dispatch/middleware/cookies.rb @@ -85,6 +85,7 @@ module ActionDispatch class CookieOverflow < StandardError; end class CookieJar #:nodoc: + include Enumerable # This regular expression is used to split the levels of a domain. # The top level domain can be any string without a period or @@ -124,6 +125,10 @@ module ActionDispatch alias :closed? :closed def close!; @closed = true end + def each(&block) + @cookies.each(&block) + end + # Returns the value of the cookie by +name+, or +nil+ if no such cookie exists. def [](name) @cookies[name.to_s] diff --git a/actionpack/test/dispatch/cookies_test.rb b/actionpack/test/dispatch/cookies_test.rb index fb67ecb07d..49da448001 100644 --- a/actionpack/test/dispatch/cookies_test.rb +++ b/actionpack/test/dispatch/cookies_test.rb @@ -148,6 +148,22 @@ class CookiesTest < ActionController::TestCase @request.host = "www.nextangle.com" end + def test_each + request.cookie_jar['foo'] = :bar + list = [] + request.cookie_jar.each do |k,v| + list << [k, v] + end + + assert_equal [['foo', :bar]], list + end + + def test_enumerable + request.cookie_jar['foo'] = :bar + actual = request.cookie_jar.map { |k,v| [k.to_s, v.to_s] } + assert_equal [['foo', 'bar']], actual + end + def test_key_methods assert !request.cookie_jar.key?(:foo) assert !request.cookie_jar.has_key?("foo") -- cgit v1.2.3