mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
refactoring class methods to a module
This commit is contained in:
@@ -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 @@ module ActiveRecord
|
||||
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 @@ module ActiveRecord
|
||||
end_sql
|
||||
end
|
||||
|
||||
def drop_table!
|
||||
connection.execute "DROP TABLE #{table_name}"
|
||||
end
|
||||
|
||||
private
|
||||
def session_id_column
|
||||
'session_id'
|
||||
@@ -173,6 +177,8 @@ module ActiveRecord
|
||||
# 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 @@ module ActiveRecord
|
||||
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 @@ module ActiveRecord
|
||||
)
|
||||
end_sql
|
||||
end
|
||||
|
||||
def drop_table!
|
||||
connection.execute "DROP TABLE #{table_name}"
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :session_id, :new_record
|
||||
|
||||
@@ -49,8 +49,11 @@ module ActiveRecord
|
||||
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user