Skip to content

Commit

Permalink
Refactored duplication into a separate method. Dropped class variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
rizwanreza authored and josevalim committed Jun 11, 2010
1 parent 6148b2d commit b602ce6
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions actionpack/lib/action_dispatch/middleware/cookies.rb
Expand Up @@ -78,16 +78,18 @@ class CookieJar < Hash #:nodoc:

def self.build(request)
secret = request.env[TOKEN_KEY]
@@host = request.env["HTTP_HOST"]
new(secret).tap do |hash|
host = request.env["HTTP_HOST"]

new(secret, host).tap do |hash|
hash.update(request.cookies)
end
end

def initialize(secret=nil)
def initialize(secret = nil, host = nil)
@secret = secret
@set_cookies = {}
@delete_cookies = {}
@host = host

super()
end
Expand All @@ -97,6 +99,15 @@ def [](name)
super(name.to_s)
end

def handle_options(options) #:nodoc:
options[:path] ||= "/"

if options[:domain] == :all
@host =~ DOMAIN_REGEXP
options[:domain] = ".#{$2}.#{$3}"
end
end

# Sets the cookie named +name+. The second argument may be the very cookie
# value, or a hash of options as documented above.
def []=(key, options)
Expand All @@ -110,13 +121,8 @@ def []=(key, options)

value = super(key.to_s, value)

options[:path] ||= "/"

if options[:domain] == :all
@@host =~ DOMAIN_REGEXP
options[:domain] = ".#{$2}.#{$3}"
end

handle_options(options)

@set_cookies[key] = options
@delete_cookies.delete(key)
value
Expand All @@ -127,12 +133,8 @@ def []=(key, options)
# an options hash to delete cookies with extra data such as a <tt>:path</tt>.
def delete(key, options = {})
options.symbolize_keys!
options[:path] ||= "/"

if options[:domain] == :all
@@host =~ DOMAIN_REGEXP
options[:domain] = ".#{$2}.#{$3}"
end
handle_options(options)

value = super(key.to_s)
@delete_cookies[key] = options
Expand Down

0 comments on commit b602ce6

Please sign in to comment.