Class: ActionController::CookieJar

Inherits:
Hash
  • Object
show all
Defined in:
lib/action_controller/cookies.rb

Overview

:nodoc:

Direct Known Subclasses

PermanentCookieJar, SignedCookieJar

Instance Attribute Summary collapse

Instance Method Summary collapse

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, @secure = controller, controller.request.cookies, controller.request.ssl?
  super()
  update(@cookies)
end

Instance Attribute Details

#controllerObject (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, options)
  if options.is_a?(Hash)
    options.symbolize_keys!
  else
    options = { :value => options }
  end

  options[:path] = "/" unless options.has_key?(:path)
  super(key.to_s, options[:value])
  @controller.response.set_cookie(key, options) if write_cookie?(options)
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, options = {})
  options.symbolize_keys!
  options[:path] = "/" unless options.has_key?(:path)
  value = super(key.to_s)
  @controller.response.delete_cookie(key, options)
  value
end

#permanentObject

Returns a jar that’ll automatically set the assigned cookies to have an expiration date 20 years from now. Example:

cookies.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:

cookies.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

#signedObject

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:

cookies.signed[:discount] = 45
# => Set-Cookie: discount=BAhpMg==--2c1c6906c90a3bc4fd54a51ffb41dffa4bf6b5f7; path=/

cookies.signed[:discount] # => 45


126
127
128
# File 'lib/action_controller/cookies.rb', line 126

def signed
  @signed ||= SignedCookieJar.new(self)
end