Skip to content
Browse files

refactoring class methods to a module

  • Loading branch information...
1 parent b378b19 commit da37716c35ec5007c028c533d64cea0dc80f574b @tenderlove tenderlove committed Jul 22, 2010
View
42 activerecord/lib/active_record/session_store.rb
@@ -49,8 +49,24 @@ module ActiveRecord
# The example SqlBypass class is a generic SQL session store. You may
# use it as a basis for high-performance database-specific stores.
class SessionStore < ActionDispatch::Session::AbstractStore
+ module ClassMethods # :nodoc:
+ def marshal(data)
+ ActiveSupport::Base64.encode64(Marshal.dump(data)) if data
+ end
+
+ def unmarshal(data)
+ Marshal.load(ActiveSupport::Base64.decode64(data)) if data
+ end
+
+ def drop_table!
+ connection.execute "DROP TABLE #{table_name}"
+ end
+ end
+
# The default Active Record class.
class Session < ActiveRecord::Base
+ extend ClassMethods
+
##
# :singleton-method:
# Customizable data column name. Defaults to 'data'.
@@ -71,14 +87,6 @@ def find_by_session_id(session_id)
find_by_session_id(session_id)
end
- def marshal(data)
- ActiveSupport::Base64.encode64(Marshal.dump(data)) if data
- end
-
- def unmarshal(data)
- Marshal.load(ActiveSupport::Base64.decode64(data)) if data
- end
-
def create_table!
connection.execute <<-end_sql
CREATE TABLE #{table_name} (
@@ -89,10 +97,6 @@ def create_table!
end_sql
end
- def drop_table!
- connection.execute "DROP TABLE #{table_name}"
- end
-
private
def session_id_column
'session_id'
@@ -173,6 +177,8 @@ def raise_on_session_data_overflow!
# binary session data in a +text+ column. For higher performance,
# store in a +blob+ column instead and forgo the Base64 encoding.
class SqlBypass
+ extend ClassMethods
+
##
# :singleton-method:
# Use the ActiveRecord::Base.connection by default.
@@ -208,14 +214,6 @@ def find_by_session_id(session_id)
end
end
- def marshal(data)
- ActiveSupport::Base64.encode64(Marshal.dump(data)) if data
- end
-
- def unmarshal(data)
- Marshal.load(ActiveSupport::Base64.decode64(data)) if data
- end
-
def create_table!
connection.execute <<-end_sql
CREATE TABLE #{table_name} (
@@ -225,10 +223,6 @@ def create_table!
)
end_sql
end
-
- def drop_table!
- connection.execute "DROP TABLE #{table_name}"
- end
end
attr_reader :session_id, :new_record
View
7 activerecord/test/cases/session_store/session_test.rb
@@ -49,8 +49,11 @@ def self.session_id_column
def test_find_by_session_id
Session.create_table!
- s = Session.create!(:data => 'world', :session_id => "10")
- assert_equal s, Session.find_by_session_id("10")
+ session_id = "10"
+ s = Session.create!(:data => 'world', :session_id => session_id)
+ t = Session.find_by_session_id(session_id)
+ assert_equal s, t
+ assert_equal s.data, t.data
Session.drop_table!
end

0 comments on commit da37716

Please sign in to comment.
Something went wrong with that request. Please try again.