Permalink
Browse files

Clean up and update cookies

  • Loading branch information...
1 parent e1786ee commit 000d5936216f363a5b11013f664959019b7ebac2 @wycats wycats committed Oct 27, 2009
Showing with 32 additions and 23 deletions.
  1. +27 −17 actionpack/lib/action_controller/metal/cookies.rb
  2. +5 −6 actionpack/test/controller/cookie_test.rb
@@ -44,24 +44,31 @@ module ActionController #:nodoc:
# * <tt>:httponly</tt> - Whether this cookie is accessible via scripting or
# only HTTP. Defaults to +false+.
module Cookies
- def self.included(base)
- base.helper_method :cookies
+ extend ActiveSupport::Concern
+
+ include RackConvenience
+
+ included do
+ helper_method :cookies
end
- protected
- # Returns the cookie container, which operates as described above.
- def cookies
- @cookies ||= CookieJar.new(self)
- end
+ protected
+ # Returns the cookie container, which operates as described above.
+ def cookies
+ @cookies ||= CookieJar.build(request, response)
+ end
end
class CookieJar < Hash #:nodoc:
- def initialize(controller)
- @controller, @cookies = controller, controller.request.cookies
- super()
- update(@cookies)
+ def self.build(request, response)
+ new.tap do |hash|
+ hash.update(request.cookies)
+ hash.response = response
+ end
end
+ attr_accessor :response
+
# Returns the value of the cookie by +name+, or +nil+ if no such cookie exists.
def [](name)
super(name.to_s)
@@ -72,23 +79,26 @@ def [](name)
def []=(key, options)
if options.is_a?(Hash)
options.symbolize_keys!
+ value = options[:value]
else
- options = { :value => options }
+ value = options
+ options = { :value => value }
end
- options[:path] = "/" unless options.has_key?(:path)
- super(key.to_s, options[:value])
- @controller.response.set_cookie(key, options)
+ super(key.to_s, value)
+
+ options[:path] ||= "/"
+ response.set_cookie(key, options)
end
# Removes the cookie on the client machine by setting the value to an empty string
# and setting its expiration date into the past. Like <tt>[]=</tt>, you can pass in
# an options hash to delete cookies with extra data such as a <tt>:path</tt>.
def delete(key, options = {})
options.symbolize_keys!
- options[:path] = "/" unless options.has_key?(:path)
+ options[:path] ||= "/"
value = super(key.to_s)
- @controller.response.delete_cookie(key, options)
+ response.delete_cookie(key, options)
value
end
end
@@ -106,22 +106,22 @@ def test_expiring_cookie
def test_cookiejar_accessor
@request.cookies["user_name"] = "david"
@controller.request = @request
- jar = ActionController::CookieJar.new(@controller)
+ jar = ActionController::CookieJar.build(@controller.request, @controller.response)
assert_equal "david", jar["user_name"]
assert_equal nil, jar["something_else"]
end
def test_cookiejar_accessor_with_array_value
@request.cookies["pages"] = %w{1 2 3}
@controller.request = @request
- jar = ActionController::CookieJar.new(@controller)
+ jar = ActionController::CookieJar.build(@controller.request, @controller.response)
assert_equal %w{1 2 3}, jar["pages"]
end
def test_cookiejar_delete_removes_item_and_returns_its_value
@request.cookies["user_name"] = "david"
@controller.response = @response
- jar = ActionController::CookieJar.new(@controller)
+ jar = ActionController::CookieJar.build(@controller.request, @controller.response)
assert_equal "david", jar.delete("user_name")
end
@@ -131,9 +131,8 @@ def test_delete_cookie_with_path
end
def test_cookies_persist_throughout_request
- get :authenticate
- cookies = @controller.send(:cookies)
- assert_equal 'david', cookies['user_name']
+ response = get :authenticate
+ assert response.headers["Set-Cookie"] =~ /user_name=david/
end
private

0 comments on commit 000d593

Please sign in to comment.