aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/metal/cookies.rb
diff options
context:
space:
mode:
authorYehuda Katz <wycats@gmail.com>2009-10-26 18:01:09 -0700
committerYehuda Katz <wycats@gmail.com>2009-10-26 18:01:09 -0700
commit000d5936216f363a5b11013f664959019b7ebac2 (patch)
tree456fcd6383cf224fc890f1737d744e9ec74f5cac /actionpack/lib/action_controller/metal/cookies.rb
parente1786ee6ebee9fab10d6756be1eeacbbe6b65b48 (diff)
downloadrails-000d5936216f363a5b11013f664959019b7ebac2.tar.gz
rails-000d5936216f363a5b11013f664959019b7ebac2.tar.bz2
rails-000d5936216f363a5b11013f664959019b7ebac2.zip
Clean up and update cookies
Diffstat (limited to 'actionpack/lib/action_controller/metal/cookies.rb')
-rw-r--r--actionpack/lib/action_controller/metal/cookies.rb44
1 files changed, 27 insertions, 17 deletions
diff --git a/actionpack/lib/action_controller/metal/cookies.rb b/actionpack/lib/action_controller/metal/cookies.rb
index c328db8beb..6855ca1478 100644
--- a/actionpack/lib/action_controller/metal/cookies.rb
+++ b/actionpack/lib/action_controller/metal/cookies.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,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