Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

fixed session_store.rb and added rake task. #8

Open
wants to merge 7 commits into from
Commits on Jan 12, 2011
  1. @authorNari
  2. @authorNari

    fixed README

    authorNari authored
Commits on Jan 17, 2011
  1. @authorNari
Commits on Jan 18, 2011
  1. @authorNari
Commits on Jan 20, 2011
  1. @authorNari

    add attr_writer :data.

    authorNari authored
Commits on Mar 18, 2011
  1. @authorNari
  2. @authorNari
This page is out of date. Refresh to see the latest.
View
8 README.rdoc
@@ -11,17 +11,19 @@ Using sequel with rails3 requires a couple minor changes.
First, add the following to your Gemfile:
gem 'sequel-rails'
-
+
... be sure to run "bundle install" if needed!
Secondly, you'll need to require "sequel-rails/railtie" in your config/application.rb file, and not require activerecord. The top of your config/application.rb will probably look something like:
-
+
# require 'rails/all'
-
+
# Instead of 'rails/all', require these:
require "action_controller/railtie"
require "sequel-rails/railtie"
require "action_mailer/railtie"
+ require "active_resource/railtie"
+ require "rails/test_unit/railtie"
After those changes, you should be good to go!
View
44 lib/sequel-rails/railties/database.rake
@@ -10,12 +10,12 @@ namespace :db do
end
Rake::Task["db:schema:dump"].reenable
end
-
+
desc "Load a schema.rb file into the database"
task :load, :needs => :environment do
require 'sequel-rails/storage'
Rails::Sequel::Storage.new(Rails.env).create
-
+
file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
if File.exists?(file)
load(file)
@@ -36,15 +36,15 @@ namespace :db do
desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?"
task :create, :env, :needs => :environment do |t, args|
args.with_defaults(:env => Rails.env)
-
+
require 'sequel-rails/storage'
Rails::Sequel::Storage.new(args.env).create
-
+
if Rails.env.development? && Rails.configuration.database_configuration['test']
Rails::Sequel::Storage.new('test').create
end
end
-
+
namespace :drop do
desc 'Drops all the local databases defined in config/database.yml'
task :all, :needs => :environment do
@@ -52,14 +52,14 @@ namespace :db do
Rails::Sequel::Storage.drop_all
end
end
-
+
desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?"
task :drop, :env, :needs => :environment do |t, args|
args.with_defaults(:env => Rails.env)
-
+
require 'sequel-rails/storage'
Rails::Sequel::Storage.new(args.env).drop
-
+
if Rails.env.development? && Rails.configuration.database_configuration['test']
Rails::Sequel::Storage.new('test').drop
end
@@ -101,7 +101,7 @@ namespace :db do
Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
end
end
-
+
desc 'Migrate the database to the latest version'
task :migrate => :'migrate:load' do
Rails::Sequel::Migrations.migrate_up!(ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
@@ -121,19 +121,19 @@ namespace :db do
Sequel::Migrator.forward('db/migrate/', step)
Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
end
-
+
desc 'Load the seed data from db/seeds.rb'
task :seed => :environment do
seed_file = File.join(Rails.root, 'db', 'seeds.rb')
load(seed_file) if File.exist?(seed_file)
end
-
+
desc 'Create the database, load the schema, and initialize with the seed data'
task :setup => [ 'db:create', 'db:migrate', 'db:seed' ]
-
+
desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
task :reset => [ 'db:drop', 'db:setup' ]
-
+
namespace :test do
task :prepare do
Rails.env = 'test'
@@ -143,6 +143,24 @@ namespace :db do
end
end
end
+
+ namespace :sessions do
+ desc "Creates the sessions table for SequelStore"
+ task :create => :environment do
+ require 'sequel-rails/session_store'
+ Rails::Sequel::SessionStore::Session.auto_migrate!
+ database = Rails::Sequel.configuration.environments[Rails.env]['database']
+ puts "Created '#{database}.sessions'"
+ end
+
+ desc "Clear the sessions table for SequelStore"
+ task :clear => :environment do
+ require 'sequel-rails/session_store'
+ Rails::Sequel::SessionStore::Session.delete
+ database = Rails::Sequel.configuration.environments[Rails.env]['database']
+ puts "Deleted entries from '#{database}.sessions'"
+ end
+ end
end
task 'test:prepare' => 'db:test:prepare'
View
55 lib/sequel-rails/session_store.rb
@@ -15,30 +15,55 @@ class Session < ::Sequel::Model
# property :updated_at, DateTime, :required => false, :index => true
class << self
-
+
def auto_migrate!
self.db.create_table :sessions do
primary_key :id
- column :session_id, String,
- :null => false,
- :unique => true,
+ column :session_id, String,
+ :null => false,
+ :unique => true,
:index => true
- column :data, :text,
- :null => false
-
- column :updated_at, DateTime,
- :null => true,
+ column :data, :text,
+ :null => false
+
+ column :updated_at, DateTime,
+ :null => true,
:index => true
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
end
def self.name
'session'
end
+ def data
+ @data ||= self.class.unmarshal(self[:data]) || {}
+ end
+ attr_writer :data
+
+ def marshal_data!
+ return false unless @data
+ self[:data] = self.class.marshal(data)
+ end
+
+ def before_save
+ marshal_data!
+ super
+ end
+
+ def loaded?
+ @data
+ end
end
SESSION_RECORD_KEY = 'rack.session.record'.freeze
@@ -58,8 +83,8 @@ def get_session(env, sid)
def set_session(env, sid, session_data)
session = get_session_resource(env, sid)
session.data = session_data
- session.updated_at = Time.now if session.dirty?
- session.save
+ session.updated_at = Time.now if session.modified?
+ session.save ? sid : false
end
def get_session_resource(env, sid)
@@ -72,10 +97,14 @@ def get_session_resource(env, sid)
def find_session(sid)
klass = self.class.session_class
-
+
klass.where(:session_id => sid).first || klass.new(:session_id => sid)
end
+ def destroy(env)
+ find_session(current_session_id(env)).destroy
+ end
+
end
end
View
2  lib/sequel-rails/setup.rb
@@ -12,7 +12,7 @@ module Sequel
def self.setup(environment)
puts "[sequel] Setting up the #{environment.inspect} environment:"
- ::Sequel.connect({:logger => configuration.logger}.merge(::Rails::Sequel.configuration.environment_for(environment.to_s)))
+ ::Sequel.connect({:logger => configuration.logger, :sql_log_level => :debug}.merge(::Rails::Sequel.configuration.environment_for(environment.to_s)))
end
end
Something went wrong with that request. Please try again.