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

Clean up and update cookies

This commit is contained in:
Yehuda Katz 2009-10-26 18:01:09 -07:00
parent e1786ee6eb
commit 000d593621
2 changed files with 32 additions and 23 deletions

View file

@ -44,23 +44,30 @@ 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)
@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)
@ -72,13 +79,16 @@ module ActionController #:nodoc:
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
@ -86,9 +96,9 @@ module ActionController #:nodoc:
# 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

View file

@ -106,7 +106,7 @@ class CookieTest < ActionController::TestCase
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
@ -114,14 +114,14 @@ class CookieTest < ActionController::TestCase
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 @@ class CookieTest < ActionController::TestCase
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