Created
April 24, 2016 00:31
-
-
Save reu/11b03095ee1bfd62bdd80d80ab4325cf to your computer and use it in GitHub Desktop.
Rails session file store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SessionFileStore < ActionDispatch::Session::AbstractStore | |
def get_session(env, sid) | |
sid ||= generate_sid | |
session = Marshal.load(IO.binread(tmp_file(sid))) rescue nil | |
[sid, session.presence || {}] | |
end | |
def set_session(env, sid, session, options) | |
Dir.mkdir(sessions_path) unless Dir.exist? sessions_path | |
IO.binwrite(tmp_file(sid), Marshal.dump(session)) | |
sid | |
end | |
def destroy_session(env, sid, options) | |
File.unlink(tmp_file(sid)) if File.exists? tmp_file(sid) | |
generate_sid | |
end | |
private | |
def tmp_file(sid) | |
File.join(sessions_path, sid) | |
end | |
def sessions_path | |
File.join(Rails.root, "tmp", "sessions") | |
end | |
end | |
Rails.application.config.session_store :session_file_store |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment