Changeset 20
- Timestamp:
- 11/26/04 02:04:35 (4 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/install.rb (modified) (1 diff)
- trunk/actionpack/lib/action_controller.rb (modified) (2 diffs)
- trunk/actionpack/lib/action_controller/base.rb (modified) (2 diffs)
- trunk/actionpack/lib/action_controller/cookies.rb (added)
- trunk/actionpack/test/controller/cookie_test.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r19 r20 1 1 *SVN* 2 2 3 * Added ActionController#cookies[]= as an alias for cookie "name", "value" -- you'll still need to use the latter if you have options to set. 4 5 * Added ActionController#cookies[] as a reader for @cookies that'll return the value of the cookie instead of the cookie object itself. 6 NOTE: If you were using the old accessor, this could potentially break your code -- if you expect a full cookie object! 3 * Added a new container for cookies that makes them more intuative to use. The old methods of cookie and @cookies have been deprecated. 4 5 Examples for writing: 6 7 cookies["user_name"] = "david" # => Will set a simple session cookie 8 cookies["login"] = { "value" => "XJ-122", "expires" => Time.now + 360} # => Will set a cookie that expires in 1 hour 9 10 Examples for reading: 11 12 cookies["user_name"] # => "david" 13 cookies.size # => 2 14 15 Read more in ActionController::Cookies 16 17 NOTE: If you were using the old accessor (cookies instead of @cookies), this could potentially break your code -- if you expect a full cookie object! 7 18 8 19 * Added the opportunity to defined method_missing on a controller which will handle all requests for actions not otherwise defined #223 [timb] trunk/actionpack/install.rb
r4 r20 42 42 action_controller/cgi_ext/cgi_methods.rb 43 43 action_controller/cgi_process.rb 44 action_controller/cookies.rb 44 45 action_controller/filters.rb 45 46 action_controller/flash.rb trunk/actionpack/lib/action_controller.rb
r4 r20 35 35 require 'action_controller/helpers' 36 36 require 'action_controller/dependencies' 37 require 'action_controller/cookies' 37 38 require 'action_controller/cgi_process' 38 39 … … 46 47 include ActionController::Helpers 47 48 include ActionController::Dependencies 49 include ActionController::Cookies 48 50 end 49 51 trunk/actionpack/lib/action_controller/base.rb
r19 r20 540 540 end 541 541 542 # Creates a new cookie that is sent along-side the next render or redirect command. API is the same as for CGI::Cookie.543 # Examples:544 #545 # cookie("name", "value1", "value2", ...)546 # cookie("name" => "name", "value" => "value")547 # cookie('name' => 'name',548 # 'value' => ['value1', 'value2', ...],549 # 'path' => 'path', # optional550 # 'domain' => 'domain', # optional551 # 'expires' => Time.now, # optional552 # 'secure' => true # optional553 # )554 def cookie(*options) #:doc:555 @response.headers["cookie"] << CGI::Cookie.new(*options)556 end557 558 # Alias for cookie "name", "value"559 def cookies[]=(name, value)560 cookie(name, value)561 end562 563 # Returns the value of the cookie by +name+ -- or nil if no such cookie exist. You set new cookies using either the cookie method564 # or cookies[]= (for simple name/value cookies without options).565 def cookies[](name)566 @cookies[name].value if @cookies[name]567 end568 569 542 # Resets the session by clearsing out all the objects stored within and initializing a new session object. 570 543 def reset_session #:doc: … … 574 547 end 575 548 549 # Deprecated cookie writer method 550 def cookie(*options) 551 @response.headers["cookie"] << CGI::Cookie.new(*options) 552 end 553 576 554 private 577 555 def initialize_template_class(response) trunk/actionpack/test/controller/cookie_test.rb
r4 r20 3 3 class CookieTest < Test::Unit::TestCase 4 4 class TestController < ActionController::Base 5 def authenticate_with_deprecated_writer 6 cookie "name" => "user_name", "value" => "david" 7 render_text "hello world" 8 end 9 5 10 def authenticate 6 cookie "name" => "user_name", "value" => "david" 11 cookies["user_name"] = "david" 12 render_text "hello world" 13 end 14 15 def authenticate_for_fourten_days 16 cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) } 17 render_text "hello world" 18 end 19 20 def set_multiple_cookies 21 cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) } 22 cookies["login"] = "XJ-122" 7 23 render_text "hello world" 8 24 end … … 22 38 end 23 39 40 def test_setting_cookie_with_deprecated_writer 41 @request.action = "authenticate_with_deprecated_writer" 42 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david") ], process_request.headers["cookie"] 43 end 44 24 45 def test_setting_cookie 25 46 @request.action = "authenticate" … … 27 48 end 28 49 29 def test_setting_cookie 50 def test_setting_cookie_for_fourteen_days 51 @request.action = "authenticate_for_fourten_days" 52 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "expires" => Time.local(2005, 10, 10)) ], process_request.headers["cookie"] 53 end 54 55 def test_multiple_cookies 56 @request.action = "set_multiple_cookies" 57 assert_equal 2, process_request.headers["cookie"].size 58 end 59 60 def test_setting_cookie_on_frozen_instance_variable 30 61 @request.action = "access_frozen_cookies" 31 62 assert_raises(TypeError) { process_request }