lib/merb/session/merb_ar_session.rb in merb-0.0.8 vs lib/merb/session/merb_ar_session.rb in merb-0.0.9
- old
+ new
@@ -4,53 +4,52 @@
module SessionMixin
def setup_session
MERB_LOGGER.info("Setting up session")
- @session = Merb::Session.persist(cookies)
- @fingerprint_before = Marshal.dump(@session).hash
+ before = cookies[session_id_key]
+ @session, cookies[session_id_key] = Merb::Session.persist(cookies[session_id_key])
+ @_fingerprint_before = Marshal.dump(@session).hash
+ @_new_cookie = cookies[session_id_key] != before
end
def finalize_session
MERB_LOGGER.info("Finalize session")
- unless Marshal.dump(@session).hash == @fingerprint_before
+ unless Marshal.dump(@session).hash == @_fingerprint_before
@session.save
end
- @headers['Set-Cookie'] = @cookies.map { |k,v| "#{k}=#{escape(v)}; path=/" if v != @cookies[k] } - [nil]
+ set_cookie(session_id_key, cookies[session_id_key], Time.now+Merb::Const::WEEK*2) if @_new_cookie
end
end
class Session < ::ActiveRecord::Base
# Customizable data column name. Defaults to 'data'.
cattr_accessor :data_column_name
self.data_column_name = 'data'
-
before_save :marshal_data!
before_save :raise_on_session_data_overflow!
RAND_CHARS = [*'A'..'Z'] + [*'0'..'9'] + [*'a'..'z']
class << self
# Generates a new session ID and creates a row for the new session in the database.
- def generate(cookies)
+ def generate
rand_max = RAND_CHARS.size
sid = (0...32).inject("") { |ret,_| ret << RAND_CHARS[rand(rand_max)] }
- sess = create(:session_id => sid, :data => {})
- cookies[:session_id] = sess.session_id
- sess
+ create(:session_id => sid, :data => {})
end
# Gets the existing session based on the <tt>session_id</tt> available in cookies.
# If none is found, generates a new session.
- def persist(cookies)
- if cookies[:session_id]
- session = find_by_session_id(cookies[:session_id])
+ def persist(session_id)
+ if session_id
+ session = find_by_session_id(session_id)
end
unless session
- session = generate(cookies)
+ session = generate
end
- session
+ [session, session.session_id]
end
# Don't try to reload ARStore::Session in dev mode.
def reloadable? #:nodoc:
false
\ No newline at end of file