1 module ActionController
#:nodoc:
2 # Cookies are read and written through ActionController#cookies.
4 # The cookies being read are the ones received along with the request, the cookies
5 # being written will be sent out with the response. Reading a cookie does not get
6 # the cookie object itself back, just the value it holds.
8 # Examples for writing:
10 # # Sets a simple session cookie.
11 # cookies[:user_name] = "david"
13 # # Sets a cookie that expires in 1 hour.
14 # cookies[:login] = { :value => "XJ-122", :expires => 1.hour.from_now }
16 # Examples for reading:
18 # cookies[:user_name] # => "david"
21 # Example for deleting:
23 # cookies.delete :user_name
25 # Please note that if you specify a :domain when setting a cookie, you must also specify the domain when deleting the cookie:
28 # :value => 'a yummy cookie',
29 # :expires => 1.year.from_now,
30 # :domain => 'domain.com'
33 # cookies.delete(:key, :domain => 'domain.com')
35 # The option symbols for setting cookies are:
37 # * <tt>:value</tt> - The cookie's value or list of values (as an array).
38 # * <tt>:path</tt> - The path for which this cookie applies. Defaults to the root
40 # * <tt>:domain</tt> - The domain for which this cookie applies.
41 # * <tt>:expires</tt> - The time at which this cookie expires, as a Time object.
42 # * <tt>:secure</tt> - Whether this cookie is a only transmitted to HTTPS servers.
44 # * <tt>:httponly</tt> - Whether this cookie is accessible via scripting or
45 # only HTTP. Defaults to +false+.
47 def self.included(base
)
48 base
.helper_method
:cookies
52 # Returns the cookie container, which operates as described above.
58 class CookieJar
< Hash
#:nodoc:
59 def initialize(controller
)
60 @controller, @cookies = controller
, controller
.request
.cookies
65 # Returns the value of the cookie by +name+, or +nil+ if no such cookie exists.
70 # Sets the cookie named +name+. The second argument may be the very cookie
71 # value, or a hash of options as documented above.
73 if options
.is_a
?(Hash
)
74 options
.symbolize_keys
!
76 options
= { :value => options
}
79 options
[:path] = "/" unless options
.has_key
?(:path)
80 super(key
.to_s
, options
[:value])
81 @controller.response
.set_cookie(key
, options
)
84 # Removes the cookie on the client machine by setting the value to an empty string
85 # and setting its expiration date into the past. Like <tt>[]=</tt>, you can pass in
86 # an options hash to delete cookies with extra data such as a <tt>:path</tt>.
87 def delete(key
, options
= {})
88 options
.symbolize_keys
!
89 options
[:path] = "/" unless options
.has_key
?(:path)
91 @controller.response
.delete_cookie(key
, options
)