class ActionDispatch::Cookies::CookieJar
:nodoc:
def self.build(request)
def self.build(request) secret = request.env[TOKEN_KEY] host = request.host secure = request.ssl? new(secret, host, secure).tap do |hash| hash.update(request.cookies) end end
def [](name)
def [](name) super(name.to_s) end
def []=(key, options)
Sets the cookie named +name+. The second argument may be the very cookie
def []=(key, options) if options.is_a?(Hash) options.symbolize_keys! value = options[:value] else value = options options = { :value => value } end value = super(key.to_s, value) handle_options(options) @set_cookies[key] = options @delete_cookies.delete(key) value end
def delete(key, options = {})
and setting its expiration date into the past. Like []=, you can pass in
Removes the cookie on the client machine by setting the value to an empty string
def delete(key, options = {}) options.symbolize_keys! handle_options(options) value = super(key.to_s) @delete_cookies[key] = options value end
def handle_options(options) #:nodoc:
def handle_options(options) #:nodoc: options[:path] ||= "/" if options[:domain] == :all @host =~ DOMAIN_REGEXP options[:domain] = ".#{$1}.#{$2}" end end
def initialize(secret = nil, host = nil, secure = false)
def initialize(secret = nil, host = nil, secure = false) @secret = secret @set_cookies = {} @delete_cookies = {} @host = host @secure = secure super() end
def permanent
cookies.permanent.signed[:remember_me] = current_user.id
This jar allows chaining with the signed jar as well, so you can set permanent, signed cookies. Examples:
This jar is only meant for writing. You'll read permanent cookies through the regular accessor.
# => Set-Cookie: prefers_open_id=true; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT
cookies.permanent[:prefers_open_id] = true
Returns a jar that'll automatically set the assigned cookies to have an expiration date 20 years from now. Example:
def permanent @permanent ||= PermanentCookieJar.new(self, @secret) end
def signed
# => Set-Cookie: discount=BAhpMg==--2c1c6906c90a3bc4fd54a51ffb41dffa4bf6b5f7; path=/
cookies.signed[:discount] = 45
Example:
This jar requires that you set a suitable secret for the verification on your app's config.secret_token.
be raised.
cookie was tampered with by the user (or a 3rd party), an ActiveSupport::MessageVerifier::InvalidSignature exception will
the cookie again. This is useful for creating cookies with values that the user is not supposed to change. If a signed
Returns a jar that'll automatically generate a signed representation of cookie value and verify it when reading from
def signed @signed ||= SignedCookieJar.new(self, @secret) end
def write(headers)
def write(headers) @set_cookies.each { |k, v| ::Rack::Utils.set_cookie_header!(headers, k, v) if write_cookie?(v) } @delete_cookies.each { |k, v| ::Rack::Utils.delete_cookie_header!(headers, k, v) } end
def write_cookie?(cookie)
def write_cookie?(cookie) @secure || !cookie[:secure] || defined?(Rails.env) && Rails.env.development? end