Skip to content
This repository
Browse code

Refactored duplication into a separate method. Dropped class variable.

  • Loading branch information...
commit b602ce61919afd0797057942f74bdeb9eae65d6b 1 parent 6148b2d
Rizwan Reza authored June 11, 2010 josevalim committed June 11, 2010
32  actionpack/lib/action_dispatch/middleware/cookies.rb
@@ -78,16 +78,18 @@ class CookieJar < Hash #:nodoc:
78 78
 
79 79
       def self.build(request)
80 80
         secret = request.env[TOKEN_KEY]
81  
-        @@host = request.env["HTTP_HOST"]
82  
-        new(secret).tap do |hash|
  81
+        host = request.env["HTTP_HOST"]
  82
+
  83
+        new(secret, host).tap do |hash|
83 84
           hash.update(request.cookies)
84 85
         end
85 86
       end
86 87
 
87  
-      def initialize(secret=nil)
  88
+      def initialize(secret = nil, host = nil)
88 89
         @secret = secret
89 90
         @set_cookies = {}
90 91
         @delete_cookies = {}
  92
+        @host = host
91 93
 
92 94
         super()
93 95
       end
@@ -97,6 +99,15 @@ def [](name)
97 99
         super(name.to_s)
98 100
       end
99 101
 
  102
+      def handle_options(options) #:nodoc:
  103
+        options[:path] ||= "/"
  104
+        
  105
+        if options[:domain] == :all
  106
+          @host =~ DOMAIN_REGEXP
  107
+          options[:domain] = ".#{$2}.#{$3}"
  108
+        end
  109
+      end
  110
+
100 111
       # Sets the cookie named +name+. The second argument may be the very cookie
101 112
       # value, or a hash of options as documented above.
102 113
       def []=(key, options)
@@ -110,13 +121,8 @@ def []=(key, options)
110 121
 
111 122
         value = super(key.to_s, value)
112 123
 
113  
-        options[:path] ||= "/"
114  
-
115  
-        if options[:domain] == :all
116  
-          @@host =~ DOMAIN_REGEXP
117  
-          options[:domain] = ".#{$2}.#{$3}"
118  
-        end
119  
-
  124
+        handle_options(options)
  125
+        
120 126
         @set_cookies[key] = options
121 127
         @delete_cookies.delete(key)
122 128
         value
@@ -127,12 +133,8 @@ def []=(key, options)
127 133
       # an options hash to delete cookies with extra data such as a <tt>:path</tt>.
128 134
       def delete(key, options = {})
129 135
         options.symbolize_keys!
130  
-        options[:path] ||= "/"
131 136
 
132  
-        if options[:domain] == :all
133  
-          @@host =~ DOMAIN_REGEXP
134  
-          options[:domain] = ".#{$2}.#{$3}"
135  
-        end
  137
+        handle_options(options)
136 138
 
137 139
         value = super(key.to_s)
138 140
         @delete_cookies[key] = options

0 notes on commit b602ce6

Please sign in to comment.
Something went wrong with that request. Please try again.