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

Changeset 866

Show
Ignore:
Timestamp:
03/06/05 23:34:03 (3 years ago)
Author:
david
Message:

Changed ActiveRecordStore to use Marshal instead of YAML as the latter proved troublesome in persisting circular dependencies. Updating existing applications MUST clear their existing session table from data to start using this updated store #739 [Jamis Buck]

Files:

Legend:

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

    r862 r866  
    11*SVN* 
     2 
     3* Changed ActiveRecordStore to use Marshal instead of YAML as the latter proved troublesome in persisting circular dependencies. Updating existing applications MUST clear their existing session table from data to start using this updated store #739 [Jamis Buck] 
    24 
    35* Added shortcut :id assignment to render_component and friends (before you had to go through :params) #784 [Lucas Carlson] 
  • trunk/actionpack/lib/action_controller/session/active_record_store.rb

    r639 r866  
    44require 'cgi' 
    55require 'cgi/session' 
     6require 'base64' 
    67 
    78# Contributed by Tim Bates 
    89class CGI 
    910  class Session 
    10     # ActiveRecord database based session storage class. 
     11    # Active Record database-based session storage class. 
    1112    # 
    1213    # Implements session storage in a database using the ActiveRecord ORM library. Assumes that the database 
    1314    # has a table called +sessions+ with columns +id+ (numeric, primary key), +sessid+ and +data+ (text). 
    14     # The session data is stored in the +data+ column in YAML format; the user is responsible for ensuring that 
    15     # only data that can be YAMLized is stored in the session. 
     15    # The session data is stored in the +data+ column in the binary Marshal format; the user is responsible for ensuring that 
     16    # only data that can be Marshaled is stored in the session. 
     17    # 
     18    # Adding +created_at+ or +updated_at+ datetime columns to the sessions table will enable stamping of the data, which can 
     19    # be used to clear out old sessions. 
     20    # 
     21    # It's highly recommended to have an index on the sessid column to improve performance. 
    1622    class ActiveRecordStore 
    1723      # The ActiveRecord class which corresponds to the database table. 
    1824      class Session < ActiveRecord::Base 
    19         serialize :data 
    20         # Isn't this class definition beautiful? 
    2125      end 
    2226 
     
    3135      def initialize(session, option=nil) 
    3236        ActiveRecord::Base.silence do 
    33           @session = Session.find_by_sessid(session.session_id) || Session.new("sessid" => session.session_id, "data" => {}
    34           @data    = @session.data 
     37          @session = Session.find_by_sessid(session.session_id) || Session.new("sessid" => session.session_id, "data" => marshalize({})
     38          @data    = unmarshalize(@session.data) 
    3539        end 
    3640      end 
     
    5357      def restore 
    5458        return unless @session 
    55         @data = @session.data 
     59        @data = unmarshalize(@session.data) 
    5660      end 
    5761 
     
    5963      def update 
    6064        return unless @session 
    61         ActiveRecord::Base.silence { @session.update_attribute "data", @data
     65        ActiveRecord::Base.silence { @session.update_attribute "data", marshalize(@data)
    6266      end 
     67 
     68      private 
     69        def unmarshalize(data) 
     70          Marshal.load(Base64.decode64(data)) 
     71        end 
     72 
     73        def marshalize(data) 
     74          Base64.encode64(Marshal.dump(data)) 
     75        end 
    6376    end #ActiveRecordStore 
    6477  end #Session