pk-merb_sequel 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Generators +4 -0
- data/LICENSE +21 -0
- data/README.rdoc +85 -0
- data/Rakefile +75 -0
- data/TODO +2 -0
- data/lib/generators/migration.rb +4 -0
- data/lib/generators/model.rb +8 -0
- data/lib/generators/resource_controller.rb +12 -0
- data/lib/generators/session_migration.rb +4 -0
- data/lib/generators/templates/migration/schema/migrations/%file_name%.rb +24 -0
- data/lib/generators/templates/model/app/models/%file_name%.rb +4 -0
- data/lib/generators/templates/resource_controller/app/controllers/%file_name%.rb +66 -0
- data/lib/generators/templates/resource_controller/app/views/%file_name%/edit.html.erb +3 -0
- data/lib/generators/templates/resource_controller/app/views/%file_name%/index.html.erb +3 -0
- data/lib/generators/templates/resource_controller/app/views/%file_name%/new.html.erb +3 -0
- data/lib/generators/templates/resource_controller/app/views/%file_name%/show.html.erb +3 -0
- data/lib/generators/templates/session_migration/schema/migrations/%version%_sessions.rb +16 -0
- data/lib/merb/orms/sequel/connection.rb +80 -0
- data/lib/merb/orms/sequel/database.yml.sample +18 -0
- data/lib/merb/session/sequel_session.rb +131 -0
- data/lib/merb_sequel.rb +50 -0
- data/lib/merb_sequel/merbtasks.rb +83 -0
- data/lib/sequel_ext/model.rb +7 -0
- metadata +118 -0
data/Generators
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2007-2008,
|
2
|
+
Duane Johnson, Wayne E. Seguin, Lance Carlson, Michael S. Klishin.
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
= merb_sequel
|
2
|
+
|
3
|
+
A plug-in for the Merb framework that provides support for Sequel models.
|
4
|
+
|
5
|
+
This is fork of the code in the official merb-plugins
|
6
|
+
(http://github.com/wycats/merb-plugins) repository. I did separate repository
|
7
|
+
and Gem to allow faster and more independent release cycle to keep up with the
|
8
|
+
Sequel monthly releases.
|
9
|
+
|
10
|
+
Plug-in should be compatible with Merb 0.9.9 and higher and Sequel 1.4.0 and
|
11
|
+
higher including 2.x and incoming <b>3.x</b>.
|
12
|
+
|
13
|
+
<b>Any issues please report to http://github.com/pk/merb_sequel/issues</b>.
|
14
|
+
|
15
|
+
|
16
|
+
== Install
|
17
|
+
|
18
|
+
gem install pk-merb_sequel --source http://gems.github.com
|
19
|
+
|
20
|
+
In Merb, add it as a dependency to your config/dependencies.rb:
|
21
|
+
|
22
|
+
dependency 'pk-merb_sequel', :require_as => 'merb_sequel'
|
23
|
+
|
24
|
+
== Compatibility
|
25
|
+
|
26
|
+
===Ruby 1.8.7:
|
27
|
+
Sequel 2.11.0:: All pass
|
28
|
+
Sequel 2.12.0:: All pass
|
29
|
+
Sequel 3.:: All pass
|
30
|
+
|
31
|
+
===Ruby 1.9.1:
|
32
|
+
Sequel 2.11.0:: All pass except session spec failing due to Marshall issues.
|
33
|
+
Sequel 2.12.0:: All pass except session spec failing due to Marshall issues.
|
34
|
+
Sequel 3.:: All pass except session spec failing due to Marshall issues.
|
35
|
+
|
36
|
+
|
37
|
+
== Connection options
|
38
|
+
|
39
|
+
Merb Sequel plug-in uses config/database.yml for connection configuration.
|
40
|
+
|
41
|
+
Options are:
|
42
|
+
|
43
|
+
* adapter. :sqlite is assumed by default.
|
44
|
+
* database, default is "hey_dude_configure_your_database". This should be
|
45
|
+
either :memory: or file path for SQLite.
|
46
|
+
* db_type: default is nil. Use "mssql" to connect to MSSQL using ODBC.
|
47
|
+
* encoding or charset, default is utf8.
|
48
|
+
* host. localhost is assumed by default.
|
49
|
+
* logger default is Merb.logger
|
50
|
+
* password. WARNING: default password is an empty string.
|
51
|
+
* socket Use socket to connect to DB.
|
52
|
+
* username or user, default is an empty string
|
53
|
+
|
54
|
+
|
55
|
+
== Generators
|
56
|
+
|
57
|
+
After you install the plug-in, merb-gen can generate Sequel models for you:
|
58
|
+
|
59
|
+
merb-gen model --orm=sequel Article
|
60
|
+
|
61
|
+
same with the resources
|
62
|
+
|
63
|
+
merb-gen resource --orm=sequel Article
|
64
|
+
|
65
|
+
|
66
|
+
<b>Note that if you have specified that you use Sequel in init.rb or environment
|
67
|
+
specific init file (for instance, environments/development.rb)
|
68
|
+
via use_orm :sequel, you don't need to specify --orm option explicitly when
|
69
|
+
using merb-gen</b>.
|
70
|
+
|
71
|
+
|
72
|
+
== Contributors
|
73
|
+
|
74
|
+
Originally written by Duane Johnson (canadaduane at gmail.com).
|
75
|
+
|
76
|
+
Contributions by:
|
77
|
+
* Wayne E. Seguin
|
78
|
+
* Lance Carlson
|
79
|
+
* Jacob Dunphy
|
80
|
+
* Lori Holden
|
81
|
+
* Pavel Kunc
|
82
|
+
* e-mac
|
83
|
+
* Piotr Usewicz
|
84
|
+
|
85
|
+
Maintained by Pavel Kunc (pavel.kunc at gmail.com)
|
data/Rakefile
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require "rake/rdoctask"
|
4
|
+
require 'merb-core/tasks/merb_rake_helper'
|
5
|
+
require "spec/rake/spectask"
|
6
|
+
|
7
|
+
##############################################################################
|
8
|
+
# Package && release
|
9
|
+
##############################################################################
|
10
|
+
RUBY_FORGE_PROJECT = "merb"
|
11
|
+
PROJECT_URL = "http://merbivore.com"
|
12
|
+
PROJECT_SUMMARY = "Merb plugin that provides support for Sequel and Sequel::Model"
|
13
|
+
PROJECT_DESCRIPTION = PROJECT_SUMMARY
|
14
|
+
|
15
|
+
GEM_AUTHOR = "Wayne E. Seguin, Lance Carlson, Lori Holden"
|
16
|
+
GEM_EMAIL = "wayneeseguin@gmail.com, lancecarlson@gmail.com, email@loriholden.com"
|
17
|
+
|
18
|
+
GEM_NAME = "merb_sequel"
|
19
|
+
PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
|
20
|
+
GEM_VERSION = (Merb::MORE_VERSION rescue "1.0.1") + PKG_BUILD
|
21
|
+
|
22
|
+
RELEASE_NAME = "REL #{GEM_VERSION}"
|
23
|
+
|
24
|
+
spec = Gem::Specification.new do |s|
|
25
|
+
s.rubyforge_project = RUBY_FORGE_PROJECT
|
26
|
+
s.name = GEM_NAME
|
27
|
+
s.version = GEM_VERSION
|
28
|
+
s.platform = Gem::Platform::RUBY
|
29
|
+
s.has_rdoc = true
|
30
|
+
s.extra_rdoc_files = ["README.rdoc", "LICENSE", 'TODO']
|
31
|
+
s.summary = PROJECT_SUMMARY
|
32
|
+
s.description = PROJECT_DESCRIPTION
|
33
|
+
s.author = GEM_AUTHOR
|
34
|
+
s.email = GEM_EMAIL
|
35
|
+
s.homepage = PROJECT_URL
|
36
|
+
s.add_dependency("merb-core", ">= 0.9.9")
|
37
|
+
s.add_dependency("sequel", ">= 1.4.0")
|
38
|
+
s.files = %w(LICENSE README.rdoc Rakefile TODO Generators) + Dir.glob("{lib}/**/*")
|
39
|
+
end
|
40
|
+
|
41
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
42
|
+
pkg.gem_spec = spec
|
43
|
+
end
|
44
|
+
|
45
|
+
desc "Install the gem"
|
46
|
+
task :install do
|
47
|
+
Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
|
48
|
+
end
|
49
|
+
|
50
|
+
desc "Uninstall the gem"
|
51
|
+
task :uninstall do
|
52
|
+
Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
|
53
|
+
end
|
54
|
+
|
55
|
+
desc "Create a gemspec file"
|
56
|
+
task :gemspec do
|
57
|
+
File.open("#{GEM_NAME}.gemspec", "w") do |file|
|
58
|
+
file.puts spec.to_ruby
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
desc "Run all examples (or a specific spec with TASK=xxxx)"
|
63
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
64
|
+
t.spec_opts = ["-cfs"]
|
65
|
+
t.spec_files = begin
|
66
|
+
if ENV["TASK"]
|
67
|
+
ENV["TASK"].split(',').map { |task| "spec/**/#{task}_spec.rb" }
|
68
|
+
else
|
69
|
+
FileList['spec/**/*_spec.rb']
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
desc 'Default: run spec examples'
|
75
|
+
task :default => 'spec'
|
data/TODO
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
Merb::Generators::ModelGenerator.template :model_sequel, :orm => :sequel do |t|
|
2
|
+
t.source = File.dirname(__FILE__) / "templates/model/app/models/%file_name%.rb"
|
3
|
+
t.destination = "app/models" / base_path / "#{file_name}.rb"
|
4
|
+
end
|
5
|
+
|
6
|
+
Merb::Generators::ModelGenerator.invoke :migration, :orm => :sequel do |generator|
|
7
|
+
generator.new(destination_root, options.merge(:model => true), file_name, attributes)
|
8
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
Merb::Generators::ResourceControllerGenerator.template :controller_sequel, :orm => :sequel do |t|
|
2
|
+
t.source = File.dirname(__FILE__) / "templates/resource_controller/app/controllers/%file_name%.rb"
|
3
|
+
t.destination = "app/controllers" / base_path / "#{file_name}.rb"
|
4
|
+
end
|
5
|
+
|
6
|
+
[:index, :show, :edit, :new].each do |view|
|
7
|
+
Merb::Generators::ResourceControllerGenerator.template "view_#{view}_sequel".to_sym,
|
8
|
+
:orm => :sequel, :template_engine => :erb do |t|
|
9
|
+
t.source = File.dirname(__FILE__) / "templates/resource_controller/app/views/%file_name%/#{view}.html.erb"
|
10
|
+
t.destination = "app/views" / base_path / "#{file_name}/#{view}.html.erb"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,4 @@
|
|
1
|
+
Merb::Generators::SessionMigrationGenerator.template :session_migration_sequel, :orm => :sequel do |t|
|
2
|
+
t.source = File.dirname(__FILE__) / 'templates/session_migration/schema/migrations/%version%_sessions.rb'
|
3
|
+
t.destination = "schema/migrations/#{version}_sessions.rb"
|
4
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# For details on Sequel migrations see
|
2
|
+
# http://sequel.rubyforge.org/
|
3
|
+
# http://sequel.rubyforge.org/rdoc/classes/Sequel/Database.html#M000607
|
4
|
+
|
5
|
+
class <%= class_name %> < Sequel::Migration
|
6
|
+
|
7
|
+
def up
|
8
|
+
<% if model -%>
|
9
|
+
create_table :<%= table_name -%> do
|
10
|
+
primary_key :id
|
11
|
+
<% attributes.each do |name, type| -%>
|
12
|
+
<%= type %> :<%= name %>
|
13
|
+
<% end -%>
|
14
|
+
end
|
15
|
+
<% end -%>
|
16
|
+
end
|
17
|
+
|
18
|
+
def down
|
19
|
+
<% if model -%>
|
20
|
+
drop_table :<%= table_name %>
|
21
|
+
<% end -%>
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
<% with_modules(modules) do -%>
|
2
|
+
class <%= class_name %> < Application
|
3
|
+
# provides :xml, :yaml, :js
|
4
|
+
|
5
|
+
# GET /<%= resource_path %>
|
6
|
+
def index
|
7
|
+
@<%= plural_model %> = <%= model_class_name %>.all
|
8
|
+
display @<%= plural_model %>
|
9
|
+
end
|
10
|
+
|
11
|
+
# GET /<%= resource_path %>/:id
|
12
|
+
def show
|
13
|
+
@<%= singular_model %> = <%= model_class_name %>[params[:id]]
|
14
|
+
raise NotFound unless @<%= singular_model %>
|
15
|
+
display @<%= singular_model %>
|
16
|
+
end
|
17
|
+
|
18
|
+
# GET /<%= resource_path %>/new
|
19
|
+
def new
|
20
|
+
only_provides :html
|
21
|
+
@<%= singular_model %> = <%= model_class_name %>.new(params[:<%= singular_model %>])
|
22
|
+
render
|
23
|
+
end
|
24
|
+
|
25
|
+
# POST /<%= resource_path %>
|
26
|
+
def create
|
27
|
+
@<%= singular_model %> = <%= model_class_name %>.new(params[:<%= singular_model %>])
|
28
|
+
if @<%= singular_model %>.save
|
29
|
+
redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>, @<%= singular_model %>)
|
30
|
+
else
|
31
|
+
render :new
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# GET /<%= resource_path %>/:id/edit
|
36
|
+
def edit
|
37
|
+
only_provides :html
|
38
|
+
@<%= singular_model %> = <%= model_class_name %>[params[:id]]
|
39
|
+
raise NotFound unless @<%= singular_model %>
|
40
|
+
render
|
41
|
+
end
|
42
|
+
|
43
|
+
# PUT /<%= resource_path %>/:id
|
44
|
+
def update
|
45
|
+
@<%= singular_model %> = <%= model_class_name %>[params[:id]]
|
46
|
+
raise NotFound unless @<%= singular_model %>
|
47
|
+
if @<%= singular_model %>.update(params[:<%= singular_model %>])
|
48
|
+
redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>, @<%= singular_model %>)
|
49
|
+
else
|
50
|
+
raise BadRequest
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# DELETE /<%= resource_path %>/:id
|
55
|
+
def destroy
|
56
|
+
@<%= singular_model %> = <%= model_class_name %>[params[:id]]
|
57
|
+
raise NotFound unless @<%= singular_model %>
|
58
|
+
if @<%= singular_model %>.destroy
|
59
|
+
redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>s)
|
60
|
+
else
|
61
|
+
raise BadRequest
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
<% end -%>
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class AddSessionsTable < Sequel::Migration
|
2
|
+
|
3
|
+
def up
|
4
|
+
create_table :sessions do
|
5
|
+
primary_key :id
|
6
|
+
varchar :session_id, :size => 64, :unique => true
|
7
|
+
timestamp :created_at
|
8
|
+
text :data
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def down
|
13
|
+
drop_table :sessions
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require "fileutils"
|
2
|
+
require "sequel"
|
3
|
+
|
4
|
+
module Merb
|
5
|
+
module Orms
|
6
|
+
module Sequel
|
7
|
+
|
8
|
+
class << self
|
9
|
+
|
10
|
+
def config_file() Merb.dir_for(:config) / "database.yml" end
|
11
|
+
def sample_dest() Merb.dir_for(:config) / "database.yml.sample" end
|
12
|
+
def sample_source() File.dirname(__FILE__) / "database.yml.sample" end
|
13
|
+
|
14
|
+
def copy_sample_config
|
15
|
+
FileUtils.cp sample_source, sample_dest unless File.exists?(sample_dest)
|
16
|
+
end
|
17
|
+
|
18
|
+
def config
|
19
|
+
@config ||= begin
|
20
|
+
# Convert string keys to symbols
|
21
|
+
full_config = Erubis.load_yaml_file(config_file)
|
22
|
+
config = (Merb::Plugins.config[:merb_sequel] = {})
|
23
|
+
(full_config[Merb.environment.to_sym] || full_config[Merb.environment] || full_config[:development]).each do |key, value|
|
24
|
+
config[key.to_sym] = value
|
25
|
+
end
|
26
|
+
config
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Database connects as soon as the gem is loaded
|
31
|
+
def connect
|
32
|
+
if File.exists?(config_file)
|
33
|
+
Merb.logger.info!("Connecting to the '#{config[:database]}' database on '#{config[:host]}' using '#{config[:adapter]}' ...")
|
34
|
+
connection = ::Sequel.connect(config_options(config))
|
35
|
+
begin
|
36
|
+
connection.test_connection
|
37
|
+
rescue => e
|
38
|
+
Merb.logger.error!("Connection Error: #{e}")
|
39
|
+
exit(1)
|
40
|
+
end
|
41
|
+
connection
|
42
|
+
else
|
43
|
+
copy_sample_config
|
44
|
+
Merb.logger.set_log(STDERR)
|
45
|
+
Merb.logger.error! "No database.yml file found at #{config_file}."
|
46
|
+
Merb.logger.error! "A sample file was created called #{sample_dest} for you to copy and edit."
|
47
|
+
exit(1)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def config_options(config = {})
|
52
|
+
options = {}
|
53
|
+
|
54
|
+
# Use SQLite by default
|
55
|
+
options[:adapter] = (config[:adapter] || "sqlite")
|
56
|
+
# Use localhost as default host
|
57
|
+
options[:host] = (config[:host] || "localhost")
|
58
|
+
# Default user is an empty string. Both username and user keys are supported.
|
59
|
+
options[:user] = (config[:username] || config[:user] || "")
|
60
|
+
|
61
|
+
options[:password] = config[:password] || ""
|
62
|
+
|
63
|
+
# Both encoding and charset options are supported, default is utf8
|
64
|
+
options[:encoding] = (config[:encoding] || config[:charset] || "utf8")
|
65
|
+
# Default database is hey_dude_configure_your_database
|
66
|
+
options[:database] = config[:database] || "hey_dude_configure_your_database"
|
67
|
+
# MSSQL support
|
68
|
+
options[:db_type] = config[:db_type] if config[:db_type]
|
69
|
+
options[:socket] = config[:socket] if config[:socket]
|
70
|
+
options[:logger] = Merb.logger
|
71
|
+
options
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
---
|
2
|
+
# This is a sample database file for the Sequel ORM
|
3
|
+
:development: &defaults
|
4
|
+
:adapter: mysql
|
5
|
+
:database: sample_development
|
6
|
+
:username: the_user
|
7
|
+
:password: secrets
|
8
|
+
:host: localhost
|
9
|
+
:socket: /tmp/mysql.sock
|
10
|
+
:encoding: utf8
|
11
|
+
|
12
|
+
:test:
|
13
|
+
<<: *defaults
|
14
|
+
:database: sample_test
|
15
|
+
|
16
|
+
:production:
|
17
|
+
<<: *defaults
|
18
|
+
:database: sample_production
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
# Load extensions if we use new versions of Sequel
|
3
|
+
require 'sequel/extensions/migration' if /^(2.12|3)/ =~ Sequel.version
|
4
|
+
require 'merb-core/dispatch/session'
|
5
|
+
require 'base64'
|
6
|
+
|
7
|
+
module Merb
|
8
|
+
|
9
|
+
Merb::Plugins.config[:merb_sequel][:session_table_name] ||= "sessions"
|
10
|
+
|
11
|
+
# Default session migration run if a sessions table does not yet exist.
|
12
|
+
#
|
13
|
+
# Will create a table with a name of 'sessions' by default, or as
|
14
|
+
# set by Merb::Plugins.config[:merb_sequel][:session_table_name]
|
15
|
+
|
16
|
+
class CreateSessionMigration < Sequel::Migration
|
17
|
+
def up
|
18
|
+
table_name = Merb::Plugins.config[:merb_sequel][:session_table_name].to_sym
|
19
|
+
unless table_exists?(table_name)
|
20
|
+
puts "Warning: The database did not contain a '#{table_name}' table for sessions."
|
21
|
+
|
22
|
+
create_table table_name do
|
23
|
+
primary_key :id
|
24
|
+
varchar :session_id
|
25
|
+
text :data
|
26
|
+
timestamp :created_at
|
27
|
+
end
|
28
|
+
|
29
|
+
puts "Created '#{table_name}' session table."
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
CreateSessionMigration.apply(Sequel::Model.db, :up)
|
35
|
+
|
36
|
+
# Sessions stored in Sequel model.
|
37
|
+
#
|
38
|
+
# To use Sequel based sessions add the following to config/init.rb:
|
39
|
+
#
|
40
|
+
# Merb::Config[:session_store] = 'sequel'
|
41
|
+
|
42
|
+
class SequelSessionStore < Sequel::Model(Merb::Plugins.config[:merb_sequel][:session_table_name].to_sym)
|
43
|
+
|
44
|
+
class << self
|
45
|
+
|
46
|
+
# ==== Parameters
|
47
|
+
# session_id<String>:: ID of the session to retrieve.
|
48
|
+
#
|
49
|
+
# ==== Returns
|
50
|
+
# ContainerSession:: The session corresponding to the ID.
|
51
|
+
def retrieve_session(session_id)
|
52
|
+
if item = find(:session_id => session_id)
|
53
|
+
item.data
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# ==== Parameters
|
58
|
+
# session_id<String>:: ID of the session to set.
|
59
|
+
# data<ContainerSession>:: The session to set.
|
60
|
+
def store_session(session_id, data)
|
61
|
+
if item = find(:session_id => session_id)
|
62
|
+
item.update(:data => data)
|
63
|
+
else
|
64
|
+
create(:session_id => session_id, :data => data, :created_at => Time.now)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# ==== Parameters
|
69
|
+
# session_id<String>:: ID of the session to delete.
|
70
|
+
def delete_session(session_id)
|
71
|
+
if item = find(:session_id => session_id)
|
72
|
+
item.delete
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# ==== Returns
|
77
|
+
# Integer:: The maximum length of the 'data' column.
|
78
|
+
def data_column_size_limit
|
79
|
+
512 # TODO - figure out how much space we actually have
|
80
|
+
end
|
81
|
+
|
82
|
+
alias :create_table! :create_table
|
83
|
+
alias :drop_table! :drop_table
|
84
|
+
end
|
85
|
+
|
86
|
+
# Lazy-unserialize session state.
|
87
|
+
def data
|
88
|
+
@data ||= (@values[:data] ? Marshal.load(@values[:data]) : {})
|
89
|
+
end
|
90
|
+
|
91
|
+
# Virtual attribute writer - override.
|
92
|
+
def data=(hsh)
|
93
|
+
@data = hsh if hsh.is_a?(Hash)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Has the session been loaded yet?
|
97
|
+
def loaded?
|
98
|
+
!!@data
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def prepare_data_to_save
|
104
|
+
@values[:data] = Marshal.dump(self.data)
|
105
|
+
if @values[:data].size > self.class.data_column_size_limit
|
106
|
+
raise Merb::SessionMixin::SessionOverflow
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
if /^(2.12|3)/ =~ Sequel.version
|
111
|
+
def before_save
|
112
|
+
super
|
113
|
+
prepare_data_to_save
|
114
|
+
end
|
115
|
+
else
|
116
|
+
before_save :prepare_data_to_save
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
class SequelSession < SessionStoreContainer
|
122
|
+
|
123
|
+
# The session store type
|
124
|
+
self.session_store_type = :sequel
|
125
|
+
|
126
|
+
# The store object is the model class itself
|
127
|
+
self.store = SequelSessionStore
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
data/lib/merb_sequel.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
if defined?(Merb::Plugins)
|
2
|
+
Merb::Plugins.config[:merb_sequel] = {}
|
3
|
+
require File.join(File.dirname(__FILE__) / "sequel_ext" / "model")
|
4
|
+
require File.join(File.dirname(__FILE__) / "merb" / "orms" / "sequel" / "connection")
|
5
|
+
Merb::Plugins.add_rakefiles "merb_sequel" / "merbtasks"
|
6
|
+
|
7
|
+
# Connects to the database and handles session
|
8
|
+
#
|
9
|
+
# Connects to the database and loads sequel sessions if we use them.
|
10
|
+
# Sets router to identify models using Model.pk.
|
11
|
+
class Merb::Orms::Sequel::Connect < Merb::BootLoader
|
12
|
+
after BeforeAppLoads
|
13
|
+
|
14
|
+
def self.run
|
15
|
+
Merb::Orms::Sequel.connect
|
16
|
+
if Merb::Config.session_stores.include?(:sequel)
|
17
|
+
Merb.logger.debug "Using Sequel sessions"
|
18
|
+
require File.join(File.dirname(__FILE__) / "merb" / "session" / "sequel_session")
|
19
|
+
end
|
20
|
+
|
21
|
+
Merb::Router.root_behavior = Merb::Router.root_behavior.identify(Sequel::Model => :pk)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
# Disconnects from DB before starting reloading classes
|
27
|
+
#
|
28
|
+
# There is a problem with the pg gem driver wich causes infinite loop
|
29
|
+
# duing reloading process.
|
30
|
+
#
|
31
|
+
# Disconnect only when fork_for_class_relaod is set and we're not in
|
32
|
+
# testing mode.
|
33
|
+
class Merb::BootLoader::DisconnectBeforeStartTransaction < Merb::BootLoader
|
34
|
+
before LoadClasses
|
35
|
+
|
36
|
+
def self.run
|
37
|
+
if Merb::Config[:fork_for_class_load] && !Merb.testing?
|
38
|
+
Merb.logger.debug "Disconnecting database connection before starting transaction."
|
39
|
+
::Sequel::DATABASES.each { |db| db.disconnect }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Load generators
|
45
|
+
generators = File.join(File.dirname(__FILE__), 'generators')
|
46
|
+
Merb.add_generators generators / :migration
|
47
|
+
Merb.add_generators generators / :model
|
48
|
+
Merb.add_generators generators / :resource_controller
|
49
|
+
Merb.add_generators generators / :session_migration
|
50
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require "fileutils"
|
2
|
+
|
3
|
+
namespace :sequel do
|
4
|
+
|
5
|
+
desc "Minimalistic Sequel environment"
|
6
|
+
task :sequel_env do
|
7
|
+
Merb::Orms::Sequel.connect
|
8
|
+
end
|
9
|
+
|
10
|
+
namespace :db do
|
11
|
+
|
12
|
+
desc "Perform migration using migrations in schema/migrations"
|
13
|
+
task :migrate => :sequel_env do
|
14
|
+
require 'sequel/extensions/migration' if /^(2.12|3)/ =~ Sequel.version
|
15
|
+
Sequel::Migrator.apply(Sequel::Model.db, "schema/migrations", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "Drop all tables"
|
19
|
+
task :drop_tables => :sequel_env do
|
20
|
+
Sequel::Model.db.drop_table *Sequel::Model.db.tables
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Drop all tables and perform migrations"
|
24
|
+
task :reset => [:sequel_env, :drop_tables, :migrate]
|
25
|
+
|
26
|
+
desc "Truncate all tables in database"
|
27
|
+
task :truncate => :sequel_env do
|
28
|
+
Sequel::Model.db << "TRUNCATE #{db.tables.join(', ')} CASCADE;"
|
29
|
+
end
|
30
|
+
|
31
|
+
desc "Create the database according to the config from the database.yaml. Use [username,password] if you need another user to connect to DB than in config."
|
32
|
+
task :create, :username, :password do |t,args|
|
33
|
+
config = Merb::Orms::Sequel.config
|
34
|
+
puts "Creating database '#{config[:database]}'"
|
35
|
+
case config[:adapter]
|
36
|
+
when 'postgres'
|
37
|
+
if args.username.nil?
|
38
|
+
`createdb -U #{config[:username]} #{config[:database]}`
|
39
|
+
else
|
40
|
+
`createdb -U #{args.username} -O #{config[:username]} #{config[:database]}`
|
41
|
+
end
|
42
|
+
when 'mysql'
|
43
|
+
`mysqladmin -u #{config[:username]} #{config[:password] ? "-p'#{config[:password]}'" : ''} create #{config[:database]}`
|
44
|
+
else
|
45
|
+
raise "Adapter #{config[:adapter]} not supported for creating databases yet."
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
desc "Drop the database for enviroment from database.yaml (postgres only). Use [username,password] if you need another user to connect to DB than in config."
|
50
|
+
task :drop, :username, :password do |t,args|
|
51
|
+
config = Merb::Orms::Sequel.config
|
52
|
+
user = args.username.nil? ? config[:username]: args.username
|
53
|
+
puts "Droping database '#{config[:database]}'"
|
54
|
+
case config[:adapter]
|
55
|
+
when 'postgres'
|
56
|
+
`dropdb -U #{user} #{config[:database]}`
|
57
|
+
else
|
58
|
+
raise "Adapter #{config[:adapter]} not supported for dropping databases yet."
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
namespace :sessions do
|
64
|
+
|
65
|
+
desc "Creates session migration"
|
66
|
+
task :create => :sequel_env do
|
67
|
+
migration_exists = Dir[File.join(Merb.root,"schema", "migrations", "*.rb")].detect{ |f| f =~ /database_sessions\.rb/ }
|
68
|
+
if migration_exists
|
69
|
+
puts "\nThe Session Migration File already exists\n\n"
|
70
|
+
else
|
71
|
+
sh %{merb-gen session_migration}
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
desc "Clears sessions"
|
76
|
+
task :clear => :sequel_env do
|
77
|
+
table_name = ((Merb::Plugins.config[:sequel] || {})[:session_table_name] || "sessions")
|
78
|
+
Sequel::Model.db.connect.execute("DELETE FROM #{table_name}")
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pk-merb_sequel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wayne E. Seguin, Lance Carlson, Lori Holden
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-04 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: merb-core
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.9.9
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sequel
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.4.0
|
34
|
+
version:
|
35
|
+
description: Merb plugin that provides support for Sequel and Sequel::Model
|
36
|
+
email: wayneeseguin@gmail.com, lancecarlson@gmail.com, email@loriholden.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README.rdoc
|
43
|
+
- LICENSE
|
44
|
+
- TODO
|
45
|
+
files:
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- Rakefile
|
49
|
+
- TODO
|
50
|
+
- Generators
|
51
|
+
- lib/generators
|
52
|
+
- lib/generators/migration.rb
|
53
|
+
- lib/generators/model.rb
|
54
|
+
- lib/generators/resource_controller.rb
|
55
|
+
- lib/generators/session_migration.rb
|
56
|
+
- lib/generators/templates
|
57
|
+
- lib/generators/templates/migration
|
58
|
+
- lib/generators/templates/migration/schema
|
59
|
+
- lib/generators/templates/migration/schema/migrations
|
60
|
+
- lib/generators/templates/migration/schema/migrations/%file_name%.rb
|
61
|
+
- lib/generators/templates/model
|
62
|
+
- lib/generators/templates/model/app
|
63
|
+
- lib/generators/templates/model/app/models
|
64
|
+
- lib/generators/templates/model/app/models/%file_name%.rb
|
65
|
+
- lib/generators/templates/resource_controller
|
66
|
+
- lib/generators/templates/resource_controller/app
|
67
|
+
- lib/generators/templates/resource_controller/app/controllers
|
68
|
+
- lib/generators/templates/resource_controller/app/controllers/%file_name%.rb
|
69
|
+
- lib/generators/templates/resource_controller/app/views
|
70
|
+
- lib/generators/templates/resource_controller/app/views/%file_name%
|
71
|
+
- lib/generators/templates/resource_controller/app/views/%file_name%/edit.html.erb
|
72
|
+
- lib/generators/templates/resource_controller/app/views/%file_name%/index.html.erb
|
73
|
+
- lib/generators/templates/resource_controller/app/views/%file_name%/new.html.erb
|
74
|
+
- lib/generators/templates/resource_controller/app/views/%file_name%/show.html.erb
|
75
|
+
- lib/generators/templates/session_migration
|
76
|
+
- lib/generators/templates/session_migration/schema
|
77
|
+
- lib/generators/templates/session_migration/schema/migrations
|
78
|
+
- lib/generators/templates/session_migration/schema/migrations/%version%_sessions.rb
|
79
|
+
- lib/merb
|
80
|
+
- lib/merb/orms
|
81
|
+
- lib/merb/orms/sequel
|
82
|
+
- lib/merb/orms/sequel/connection.rb
|
83
|
+
- lib/merb/orms/sequel/database.yml.sample
|
84
|
+
- lib/merb/session
|
85
|
+
- lib/merb/session/sequel_session.rb
|
86
|
+
- lib/merb_sequel
|
87
|
+
- lib/merb_sequel/merbtasks.rb
|
88
|
+
- lib/merb_sequel.rb
|
89
|
+
- lib/sequel_ext
|
90
|
+
- lib/sequel_ext/model.rb
|
91
|
+
has_rdoc: true
|
92
|
+
homepage: http://merbivore.com
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: "0"
|
103
|
+
version:
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: "0"
|
109
|
+
version:
|
110
|
+
requirements: []
|
111
|
+
|
112
|
+
rubyforge_project: merb
|
113
|
+
rubygems_version: 1.2.0
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: Merb plugin that provides support for Sequel and Sequel::Model
|
117
|
+
test_files: []
|
118
|
+
|