Class: ActionController::CookieJar
- Defined in:
- lib/action_controller/cookies.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns the value of the cookie by
name
, ornil
if no such cookie exists. -
#[]=(key, options) ⇒ Object
Sets the cookie named
name
. -
#delete(key, options = {}) ⇒ Object
Removes the cookie on the client machine by setting the value to an empty string and setting its expiration date into the past.
-
#initialize(controller) ⇒ CookieJar
constructor
A new instance of CookieJar.
-
#permanent ⇒ Object
Returns a jar that’ll automatically set the assigned cookies to have an expiration date 20 years from now.
-
#signed ⇒ Object
Returns a jar that’ll automatically generate a signed representation of cookie value and verify it when reading from the cookie again.
Constructor Details
#initialize(controller) ⇒ CookieJar
Returns a new instance of CookieJar.
62 63 64 65 66 |
# File 'lib/action_controller/cookies.rb', line 62 def initialize(controller) @controller, @cookies = controller, controller.request. super() update(@cookies) end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
60 61 62 |
# File 'lib/action_controller/cookies.rb', line 60 def controller @controller end |
Instance Method Details
#[](name) ⇒ Object
Returns the value of the cookie by name
, or nil
if no such cookie exists.
69 70 71 |
# File 'lib/action_controller/cookies.rb', line 69 def [](name) super(name.to_s) end |
#[]=(key, options) ⇒ Object
Sets the cookie named name
. The second argument may be the very cookie value, or a hash of options as documented above.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/action_controller/cookies.rb', line 75 def []=(key, ) if .is_a?(Hash) .symbolize_keys! else = { :value => } end [:path] = "/" unless .has_key?(:path) super(key.to_s, [:value]) @controller.response.(key, ) end |
#delete(key, options = {}) ⇒ Object
Removes the cookie on the client machine by setting the value to an empty string and setting its expiration date into the past. Like []=
, you can pass in an options hash to delete cookies with extra data such as a :path
.
90 91 92 93 94 95 96 |
# File 'lib/action_controller/cookies.rb', line 90 def delete(key, = {}) .symbolize_keys! [:path] = "/" unless .has_key?(:path) value = super(key.to_s) @controller.response.(key, ) value end |
#permanent ⇒ Object
Returns a jar that’ll automatically set the assigned cookies to have an expiration date 20 years from now. Example:
.permanent[:prefers_open_id] = true
# => Set-Cookie: prefers_open_id=true; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT
This jar is only meant for writing. You’ll read permanent cookies through the regular accessor.
This jar allows chaining with the signed jar as well, so you can set permanent, signed cookies. Examples:
.permanent.signed[:remember_me] = current_user.id
# => Set-Cookie: discount=BAhU--848956038e692d7046deab32b7131856ab20e14e; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT
109 110 111 |
# File 'lib/action_controller/cookies.rb', line 109 def permanent @permanent ||= PermanentCookieJar.new(self) end |
#signed ⇒ Object
Returns a jar that’ll automatically generate a signed representation of cookie value and verify it when reading from the cookie again. This is useful for creating cookies with values that the user is not supposed to change. If a signed cookie was tampered with by the user (or a 3rd party), an ActiveSupport::MessageVerifier::InvalidSignature exception will be raised.
This jar requires that you set a suitable secret for the verification on ActionController::Base.cookie_verifier_secret.
Example:
.signed[:discount] = 45
# => Set-Cookie: discount=BAhpMg==--2c1c6906c90a3bc4fd54a51ffb41dffa4bf6b5f7; path=/
.signed[:discount] # => 45
126 127 128 |
# File 'lib/action_controller/cookies.rb', line 126 def signed @signed ||= SignedCookieJar.new(self) end |