Changeset 668

Show
Ignore:
Timestamp:
09/21/07 13:29:10 (1 year ago)
Author:
duane.johns..@gmail.com
Message:

Fixed merb_sequel plugin (thanks James) [closes #177]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/merb_sequel/lib/merb/session/sequel_session.rb

    r659 r668  
    11module Merb 
    2   module Sessions 
    3     module SessionMixin 
    4       def setup_session 
    5         MERB_LOGGER.info("Setting up session") 
    6         before = cookies[_session_id_key] 
    7         @_session, cookies[_session_id_key] = Merb::SequelSession.persist(cookies[_session_id_key]) 
    8         @_fingerprint = Marshal.dump(@_session.data).hash 
    9         @_new_cookie = cookies[_session_id_key] != before 
     2  module SessionMixin 
     3    def setup_session 
     4      MERB_LOGGER.info("Setting up session") 
     5      before = cookies[_session_id_key] 
     6      @_session, cookies[_session_id_key] = Merb::SequelSession.persist(cookies[_session_id_key]) 
     7      @_fingerprint = Marshal.dump(@_session.data).hash 
     8      @_new_cookie = cookies[_session_id_key] != before 
     9    end 
     10   
     11    def finalize_session 
     12      MERB_LOGGER.info("Finalize session") 
     13      @_session.save if @_fingerprint != Marshal.dump(@_session.data).hash 
     14      set_cookie(_session_id_key, @_session.values[:session_id], _session_expiry) if (@_new_cookie || @_session.needs_new_cookie) 
     15    end 
     16  end 
     17 
     18  table_name = (Merb::Plugins.config[:sequel][:session_table_name] || "sessions") 
     19 
     20  class SequelSession < Sequel::Model(table_name.to_sym) 
     21    set_schema do 
     22      primary_key :id 
     23      varchar :session_id 
     24      varchar :data 
     25    end 
     26   
     27    attr_accessor :needs_new_cookie 
     28   
     29    class << self 
     30      # Generates a new session ID and creates a row for the new session in the database. 
     31      def generate 
     32        create(:session_id => Merb::SessionMixin::rand_uuid, :data => marshal({})) 
    1033      end 
    1134     
    12       def finalize_session 
    13         MERB_LOGGER.info("Finalize session") 
    14         @_session.save if @_fingerprint != Marshal.dump(@_session.data).hash 
    15         set_cookie(_session_id_key, @_session.values[:session_id], _session_expiry) if (@_new_cookie || @_session.needs_new_cookie) 
     35      # Gets the existing session based on the <tt>session_id</tt> available in cookies. 
     36      # If none is found, generates a new session. 
     37      def persist(session_id) 
     38        if session_id 
     39          session = find_by_session_id(session_id) 
     40        end 
     41        unless session 
     42          session = generate 
     43        end 
     44        [session, session.values[:session_id]] 
    1645      end 
     46     
     47      # Don't try to reload ARStore::Session in dev mode. 
     48      def reloadable? #:nodoc: 
     49        false 
     50      end 
     51     
     52      def data_column_size_limit 
     53        255 
     54      end 
     55     
     56      def marshal(data)   Base64.encode64(Marshal.dump(data)) if data end 
     57      def unmarshal(data) 
     58        Marshal.load(Base64.decode64(data)) if data 
     59      end 
     60       
     61      alias :create_table! :create_table 
     62      alias :drop_table! :drop_table 
    1763    end 
    1864   
     65    # Regenerate the Session ID 
     66    def regenerate 
     67      update_attributes(:session_id => Merb::SessionMixin::rand_uuid) 
     68      self.needs_new_cookie = true 
     69    end  
    1970   
    20     table_name = (Merb::Plugins.config[:sequel][:session_table_name] || "sessions") 
     71    # Recreates the cookie with the default expiration time  
     72    # Useful during log in for pushing back the expiration date  
     73    def refresh_expiration 
     74      self.needs_new_cookie = true 
     75    end 
    2176   
    22     class SequelSession < Sequel::Model(table_name.to_sym) 
    23       set_schema do 
    24         primary_key :id 
    25         varchar :session_id 
    26         varchar :data 
    27       end 
    28      
    29       attr_accessor :needs_new_cookie 
    30      
    31       class << self 
    32         # Generates a new session ID and creates a row for the new session in the database. 
    33         def generate 
    34           create(:session_id => Merb::SessionMixin::rand_uuid, :data => marshal({})) 
    35         end 
    36        
    37         # Gets the existing session based on the <tt>session_id</tt> available in cookies. 
    38         # If none is found, generates a new session. 
    39         def persist(session_id) 
    40           if session_id 
    41             session = find_by_session_id(session_id) 
    42           end 
    43           unless session 
    44             session = generate 
    45           end 
    46           [session, session.values[:session_id]] 
    47         end 
    48        
    49         # Don't try to reload ARStore::Session in dev mode. 
    50         def reloadable? #:nodoc: 
    51           false 
    52         end 
    53        
    54         def data_column_size_limit 
    55           255 
    56         end 
    57        
    58         def marshal(data)   Base64.encode64(Marshal.dump(data)) if data end 
    59         def unmarshal(data) 
    60           Marshal.load(Base64.decode64(data)) if data 
    61         end 
    62          
    63         alias :create_table! :create_table 
    64         alias :drop_table! :drop_table 
    65       end 
    66      
    67       # Regenerate the Session ID 
    68       def regenerate 
    69         update_attributes(:session_id => Merb::SessionMixin::rand_uuid) 
    70         self.needs_new_cookie = true 
    71       end  
    72      
    73       # Recreates the cookie with the default expiration time  
    74       # Useful during log in for pushing back the expiration date  
    75       def refresh_expiration 
    76         self.needs_new_cookie = true 
    77       end 
    78      
    79       # Lazy-delete of session data  
    80       def delete 
    81         self.data = {} 
    82       end 
    83      
    84       def [](key) 
    85         data[key] 
    86       end 
    87      
    88       def []=(key, val) 
    89         data[key] = val 
    90       end 
    91      
    92       # Lazy-unmarshal session state. 
    93       def data 
    94         @data ||= self.class.unmarshal(@values[:data]) || {} 
    95       end 
    96      
    97       # Has the session been loaded yet? 
    98       def loaded? 
    99         !! @data 
    100       end 
    101      
    102     private 
    103       attr_writer :data 
    104      
    105       before_save do # marshal_data! 
    106         # return false if !loaded? 
    107         @values[:data] = self.class.marshal(self.data) 
    108       end 
    109      
    110       # Ensures that the data about to be stored in the database is not 
    111       # larger than the data storage column. Raises 
    112       # ActionController::SessionOverflowError. 
    113       # before_save do # raise_on_session_data_overflow! 
    114         # return false if !loaded? 
    115         # limit = self.class.data_column_size_limit 
    116         # if loaded? and limit and read_attribute(@@data_column_name).size > limit 
    117           # raise MerbController::SessionOverflowError 
    118         # end 
     77    # Lazy-delete of session data  
     78    def delete 
     79      self.data = {} 
     80    end 
     81   
     82    def [](key) 
     83      data[key] 
     84    end 
     85   
     86    def []=(key, val) 
     87      data[key] = val 
     88    end 
     89   
     90    # Lazy-unmarshal session state. 
     91    def data 
     92      @data ||= self.class.unmarshal(@values[:data]) || {} 
     93    end 
     94   
     95    # Has the session been loaded yet? 
     96    def loaded? 
     97      !! @data 
     98    end 
     99   
     100  private 
     101    attr_writer :data 
     102   
     103    before_save do # marshal_data! 
     104      # return false if !loaded? 
     105      @values[:data] = self.class.marshal(self.data) 
     106    end 
     107   
     108    # Ensures that the data about to be stored in the database is not 
     109    # larger than the data storage column. Raises 
     110    # ActionController::SessionOverflowError. 
     111    # before_save do # raise_on_session_data_overflow! 
     112      # return false if !loaded? 
     113      # limit = self.class.data_column_size_limit 
     114      # if loaded? and limit and read_attribute(@@data_column_name).size > limit 
     115        # raise MerbController::SessionOverflowError 
    119116      # end 
     117    # end 
     118  end 
     119 
     120  unless Sequel::Model.db.table_exists?(table_name.to_sym) 
     121    puts "Warning: The database did not contain a '#{table_name}' table for sessions." 
     122 
     123    SequelSession.class_eval do 
     124      create_table unless table_exists? 
    120125    end 
    121126 
    122     unless Sequel::Model.db.table_exists?(table_name.to_sym) 
    123       puts "Warning: The database did not contain a '#{table_name}' table for sessions." 
    124  
    125       SequelSession.class_eval do 
    126         create_table unless table_exists? 
    127       end 
    128  
    129       puts "Created sessions table." 
    130     end 
     127    puts "Created sessions table." 
    131128  end 
    132129end