Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 20

Show
Ignore:
Timestamp:
11/26/04 02:04:35 (4 years ago)
Author:
david
Message:

Added a new container for cookies that makes them more intuative to use. The old methods of cookie and @cookies have been deprecated.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r19 r20  
    11*SVN* 
    22 
    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! 
    718 
    819* 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  
    4242 action_controller/cgi_ext/cgi_methods.rb 
    4343 action_controller/cgi_process.rb 
     44 action_controller/cookies.rb 
    4445 action_controller/filters.rb 
    4546 action_controller/flash.rb 
  • trunk/actionpack/lib/action_controller.rb

    r4 r20  
    3535require 'action_controller/helpers' 
    3636require 'action_controller/dependencies' 
     37require 'action_controller/cookies' 
    3738require 'action_controller/cgi_process' 
    3839 
     
    4647  include ActionController::Helpers 
    4748  include ActionController::Dependencies 
     49  include ActionController::Cookies 
    4850end 
    4951 
  • trunk/actionpack/lib/action_controller/base.rb

    r19 r20  
    540540      end 
    541541 
    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',   # optional 
    550       #          'domain'  => 'domain', # optional 
    551       #          'expires' => Time.now, # optional 
    552       #          'secure'  => true      # optional 
    553       #   ) 
    554       def cookie(*options) #:doc: 
    555         @response.headers["cookie"] << CGI::Cookie.new(*options) 
    556       end 
    557  
    558       # Alias for cookie "name", "value" 
    559       def cookies[]=(name, value) 
    560         cookie(name, value) 
    561       end 
    562  
    563       # Returns the value of the cookie by +name+ -- or nil if no such cookie exist. You set new cookies using either the cookie method 
    564       # or cookies[]= (for simple name/value cookies without options). 
    565       def cookies[](name) 
    566         @cookies[name].value if @cookies[name] 
    567       end 
    568        
    569542      # Resets the session by clearsing out all the objects stored within and initializing a new session object. 
    570543      def reset_session #:doc: 
     
    574547      end 
    575548     
     549      # Deprecated cookie writer method 
     550      def cookie(*options) 
     551        @response.headers["cookie"] << CGI::Cookie.new(*options) 
     552      end 
     553 
    576554    private 
    577555      def initialize_template_class(response) 
  • trunk/actionpack/test/controller/cookie_test.rb

    r4 r20  
    33class CookieTest < Test::Unit::TestCase 
    44  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 
    510    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" 
    723      render_text "hello world" 
    824    end 
     
    2238  end 
    2339 
     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 
    2445  def test_setting_cookie 
    2546    @request.action = "authenticate" 
     
    2748  end 
    2849 
    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 
    3061    @request.action = "access_frozen_cookies" 
    3162    assert_raises(TypeError) { process_request }