nulogy-sequel-rails 0.3.8
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +29 -0
- data/Gemfile +3 -0
- data/History.md +77 -0
- data/LICENSE +20 -0
- data/README.rdoc +102 -0
- data/Rakefile +9 -0
- data/lib/rails/generators/sequel/active_model.rb +39 -0
- data/lib/rails/generators/sequel/base.rb +26 -0
- data/lib/rails/generators/sequel/generated_attribute_ext.rb +9 -0
- data/lib/rails/generators/sequel/migration/migration_generator.rb +43 -0
- data/lib/rails/generators/sequel/migration/templates/migration.rb +48 -0
- data/lib/rails/generators/sequel/model/model_generator.rb +28 -0
- data/lib/rails/generators/sequel/model/templates/migration.rb +14 -0
- data/lib/rails/generators/sequel/model/templates/model.rb +6 -0
- data/lib/rails/generators/sequel/observer/observer_generator.rb +16 -0
- data/lib/rails/generators/sequel/observer/templates/observer.rb +4 -0
- data/lib/rails/generators/sequel.rb +9 -0
- data/lib/sequel/plugins/rails_extensions.rb +25 -0
- data/lib/sequel/rails/configuration.rb +56 -0
- data/lib/sequel/rails/core.rb +50 -0
- data/lib/sequel/rails/ext.rb +25 -0
- data/lib/sequel/rails/log_subscriber.rb +71 -0
- data/lib/sequel/rails/migrations.rb +22 -0
- data/lib/sequel/rails/railtie.rb +76 -0
- data/lib/sequel/rails/railties/controller_runtime.rb +48 -0
- data/lib/sequel/rails/railties/database.rake +185 -0
- data/lib/sequel/rails/railties/i18n_support.rb +9 -0
- data/lib/sequel/rails/session_store.rb +69 -0
- data/lib/sequel/rails/setup.rb +14 -0
- data/lib/sequel/rails/storage.rb +225 -0
- data/lib/sequel/rails/version.rb +6 -0
- data/lib/sequel/rails.rb +26 -0
- data/lib/sequel-rails.rb +2 -0
- data/nulogy-sequel-rails.gemspec +19 -0
- metadata +101 -0
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
## MAC OS
|
2
|
+
.DS_Store
|
3
|
+
|
4
|
+
## TEXTMATE
|
5
|
+
*.tmproj
|
6
|
+
tmtags
|
7
|
+
|
8
|
+
## EMACS
|
9
|
+
*~
|
10
|
+
\#*
|
11
|
+
.\#*
|
12
|
+
|
13
|
+
## VIM
|
14
|
+
*.swp
|
15
|
+
|
16
|
+
## PROJECT::GENERAL
|
17
|
+
coverage
|
18
|
+
rdoc
|
19
|
+
doc
|
20
|
+
pkg
|
21
|
+
tmp
|
22
|
+
log
|
23
|
+
.yardoc
|
24
|
+
|
25
|
+
## PROJECT::SPECIFIC
|
26
|
+
vendor
|
27
|
+
.bundle
|
28
|
+
.rvmrc
|
29
|
+
*.gem
|
data/Gemfile
ADDED
data/History.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
0.3.5 - dev
|
2
|
+
===========
|
3
|
+
|
4
|
+
0.3.4
|
5
|
+
=====
|
6
|
+
|
7
|
+
* Make `rake db:schema:dump` generate a schema file which contains foreign_keys
|
8
|
+
and uses db types instead of ruby equivalents. This ensure loading the schema
|
9
|
+
file will result in a correct db
|
10
|
+
|
11
|
+
* Map some Sequel specific exceptions to `ActiveRecord` equivalents, in
|
12
|
+
`config.action_dispatch.rescue_responses`. This allows controllers to behave
|
13
|
+
more like `ActiveRecord` when Sequel raises exceptions. (Joshua Hansen)
|
14
|
+
|
15
|
+
* New Sequel plugin added to all `Sequel::Model` which allows to use
|
16
|
+
`Sequel::Model#find!` which will raise an exception if record does not exists.
|
17
|
+
This method is an alias to `Sequel::Model#[]` method. (Joshua Hansen)
|
18
|
+
|
19
|
+
0.3.3
|
20
|
+
=====
|
21
|
+
|
22
|
+
* Fix generators and use better model and migration template (Joshua Hansen)
|
23
|
+
|
24
|
+
0.3.2
|
25
|
+
=====
|
26
|
+
* Ignore environments without `database` key (like ActiveRecord do it), to allow
|
27
|
+
shared configurations in `database.yml`.
|
28
|
+
* Fix db creation commands to let the `system` method escape the arguments
|
29
|
+
* Fix error when using `mysql2` gem
|
30
|
+
|
31
|
+
0.3.1
|
32
|
+
=====
|
33
|
+
* Make `db:schema:dump` Rake task depends on Rails `environment` task (Gabor Ratky)
|
34
|
+
|
35
|
+
0.3.0
|
36
|
+
=====
|
37
|
+
* Update dependency to Rails (~> 3.2.0)
|
38
|
+
|
39
|
+
0.2.3
|
40
|
+
=====
|
41
|
+
* Set `PGPASSWORD` environment variable before trying to create DB using `createdb`
|
42
|
+
|
43
|
+
0.2.2
|
44
|
+
=====
|
45
|
+
* Ensure Sequel is disconnected before trying to drop a db
|
46
|
+
|
47
|
+
0.2.1
|
48
|
+
=====
|
49
|
+
* Make dependency on Sequel more open (~> 3.28)
|
50
|
+
|
51
|
+
0.2.0
|
52
|
+
=====
|
53
|
+
* Fix deprecation warning for config.generators
|
54
|
+
* Update dependency to Rails 3.1.1
|
55
|
+
* Update dependency to Sequel 3.28.0
|
56
|
+
* Update dependency to RSpec 2.7.0
|
57
|
+
|
58
|
+
0.1.4
|
59
|
+
=====
|
60
|
+
* Merged in changes to rake tasks and timestamp migrations
|
61
|
+
|
62
|
+
0.1.3
|
63
|
+
=====
|
64
|
+
* update sequel dependency, configuration change
|
65
|
+
|
66
|
+
0.1.2
|
67
|
+
=====
|
68
|
+
* fixed log_subscriber bug that 0.1.1 was -supposed- to fix.
|
69
|
+
* fixed controller_runtime bug
|
70
|
+
|
71
|
+
0.1.1
|
72
|
+
=====
|
73
|
+
* bug fixes, no additional functionality
|
74
|
+
|
75
|
+
0.1.0
|
76
|
+
=====
|
77
|
+
* initial release
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009-2010 The sequel-rails team
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
= sequel-rails
|
2
|
+
|
3
|
+
This gem provides the railtie that allows {sequel}[http://github.com/jeremyevans/sequel] to hook into {rails3}[http://github.com/rails/rails] and thus behave like a rails framework component. Just like activerecord does in rails, {sequel-rails}[http://github.com/talentbox/sequel-rails] uses the railtie API to hook into rails. The two are actually hooked into rails almost identically.
|
4
|
+
|
5
|
+
The code for this gem was initially taken from the excellent {dm-rails}[http://github.com/datamapper/dm-rails] project.
|
6
|
+
|
7
|
+
This is a fork of {brasten}[https://github.com/brasten]'s {sequel-rails}[https://github.com/brasten/sequel-rails] that has been updated to support newer versions of rails.
|
8
|
+
|
9
|
+
== Using sequel-rails
|
10
|
+
|
11
|
+
Using sequel with rails3 requires a couple minor changes.
|
12
|
+
|
13
|
+
First, add the following to your Gemfile:
|
14
|
+
|
15
|
+
gem 'talentbox-sequel-rails'
|
16
|
+
|
17
|
+
... be sure to run "bundle install" if needed!
|
18
|
+
|
19
|
+
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:
|
20
|
+
|
21
|
+
# require 'rails/all'
|
22
|
+
|
23
|
+
# Instead of 'rails/all', require these:
|
24
|
+
require "action_controller/railtie"
|
25
|
+
require "sequel-rails/railtie"
|
26
|
+
require "action_mailer/railtie"
|
27
|
+
|
28
|
+
|
29
|
+
After those changes, you should be good to go!
|
30
|
+
|
31
|
+
|
32
|
+
== Available sequel specific rake tasks
|
33
|
+
|
34
|
+
To get a list of all available rake tasks in your rails3 app, issue the usual
|
35
|
+
|
36
|
+
vendor/bin/rake -T
|
37
|
+
|
38
|
+
Once you do that, you will see the following rake tasks among others. These are the ones that sequel-rails added for us.
|
39
|
+
|
40
|
+
...
|
41
|
+
vendor/bin/rake db:create # Create the database(s) defined in config/database.yml for the current Rails.env - also creates the test database(s) if Rails.env.development?
|
42
|
+
vendor/bin/rake db:create:all # Create all the local databases defined in config/database.yml
|
43
|
+
vendor/bin/rake db:drop # Drops the database(s) for the current Rails.env - also drops the test database(s) if Rails.env.development?
|
44
|
+
vendor/bin/rake db:drop:all # Drop all the local databases defined in config/database.yml
|
45
|
+
vendor/bin/rake db:migrate # Migrate the database to the latest version
|
46
|
+
vendor/bin/rake db:migrate:down[version] # Migrate down using migrations
|
47
|
+
vendor/bin/rake db:migrate:up[version] # Migrate up using migrations
|
48
|
+
vendor/bin/rake db:seed # Load the seed data from db/seeds.rb
|
49
|
+
vendor/bin/rake db:sessions:clear # Clear the sessions table for SequelStore
|
50
|
+
vendor/bin/rake db:sessions:create # Creates the sessions table for SequelStore
|
51
|
+
vendor/bin/rake db:setup # Create the database, load the schema, and initialize with the seed data
|
52
|
+
...
|
53
|
+
|
54
|
+
|
55
|
+
== Current Issues
|
56
|
+
|
57
|
+
* There are bound to be a lot, but I'm not yet sure what they are
|
58
|
+
|
59
|
+
== TODO (not necessarily in that order)
|
60
|
+
|
61
|
+
* SPECS
|
62
|
+
* README changes
|
63
|
+
* Publish SQL issued by sequel to rails subscribers
|
64
|
+
|
65
|
+
== Credits
|
66
|
+
|
67
|
+
The {dm-rails}[http://github.com/datamapper/dm-rails] team wrote most of this code, I just sequel-ized it.
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
== Note on Patches/Pull Requests
|
72
|
+
|
73
|
+
* Fork the project.
|
74
|
+
* Make your feature addition or bug fix.
|
75
|
+
* Add tests for it. This is important so I don't break it in a
|
76
|
+
future version unintentionally.
|
77
|
+
* Commit, do not mess with rakefile, version, or history.
|
78
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
79
|
+
* Send me a pull request. Bonus points for topic branches.
|
80
|
+
|
81
|
+
|
82
|
+
== The sequel-rails team
|
83
|
+
|
84
|
+
{Original project}[https://github.com/brasten/sequel-rails]:
|
85
|
+
|
86
|
+
* Brasten Sager (brasten)
|
87
|
+
|
88
|
+
This fork:
|
89
|
+
|
90
|
+
* Jonathan Tron (JonathanTron)
|
91
|
+
|
92
|
+
== Contributors
|
93
|
+
|
94
|
+
Improvements has been made by those awesome contributors:
|
95
|
+
|
96
|
+
* Benjamin Atkin (benatkin)
|
97
|
+
* Gabor Ratky (rgabo)
|
98
|
+
* Joshua Hansen (binarypaladin)
|
99
|
+
|
100
|
+
== Copyright
|
101
|
+
|
102
|
+
Copyright (c) 2010 The sequel-rails team. See {LICENSE}[http://github.com/brasten/sequel-rails/blob/master/LICENSE] for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
require 'rails/generators/active_model'
|
3
|
+
|
4
|
+
module Sequel::Rails::Generators
|
5
|
+
class ActiveModel < ::Rails::Generators::ActiveModel #:nodoc:
|
6
|
+
def self.all(klass)
|
7
|
+
"#{klass}.all"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.find(klass, params=nil)
|
11
|
+
"#{klass}.find!(#{params})"
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.build(klass, params=nil)
|
15
|
+
if params
|
16
|
+
"#{klass}.new(#{params})"
|
17
|
+
else
|
18
|
+
"#{klass}.new"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def save
|
23
|
+
"#{name}.save"
|
24
|
+
end
|
25
|
+
|
26
|
+
def update_attributes(params=nil)
|
27
|
+
"#{name}.update(#{params})"
|
28
|
+
end
|
29
|
+
|
30
|
+
def errors
|
31
|
+
"#{name}.errors"
|
32
|
+
end
|
33
|
+
|
34
|
+
def destroy
|
35
|
+
"#{name}.destroy"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
# This is basically adapted straight from ActiveRecord
|
3
|
+
|
4
|
+
require 'rails/generators/named_base'
|
5
|
+
require 'rails/generators/migration'
|
6
|
+
|
7
|
+
module Sequel::Generators
|
8
|
+
class Base < ::Rails::Generators::NamedBase
|
9
|
+
include ::Rails::Generators::Migration
|
10
|
+
|
11
|
+
# Implement the required interface for Rails::Generators::Migration.
|
12
|
+
#
|
13
|
+
def self.next_migration_number(dirname) #:nodoc:
|
14
|
+
next_migration_number = current_migration_number(dirname) + 1
|
15
|
+
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
# Sequel does not care if migrations have the same name as long as
|
20
|
+
# they have different ids.
|
21
|
+
#
|
22
|
+
def migration_exists?(dirname, file_name) #:nodoc:
|
23
|
+
false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
require File.expand_path('../../../sequel', __FILE__)
|
3
|
+
|
4
|
+
module Sequel::Generators
|
5
|
+
class MigrationGenerator < Base
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
9
|
+
class_option :id, :type => :numeric, :desc => "The id to be used in this migration"
|
10
|
+
|
11
|
+
def create_migration_file
|
12
|
+
set_local_assigns!
|
13
|
+
migration_template "migration.rb", "db/migrate/#{file_name}.rb"
|
14
|
+
end
|
15
|
+
|
16
|
+
attr_reader :migration_action, :table_action, :column_action, :use_change
|
17
|
+
|
18
|
+
protected
|
19
|
+
def set_local_assigns!
|
20
|
+
if file_name =~ /^(create|drop)_(.*)$/
|
21
|
+
@table_action = $1
|
22
|
+
@table_name = $2.pluralize
|
23
|
+
@column_action = 'add'
|
24
|
+
@use_change = @table_action == 'create' ? true : false
|
25
|
+
elsif file_name =~ /^(add|drop|remove)_.*_(?:to|from)_(.*)/
|
26
|
+
@table_action = 'alter'
|
27
|
+
@table_name = $2.pluralize
|
28
|
+
@column_action = $1 == 'add' ? 'add' : 'drop'
|
29
|
+
@use_change = @column_action == 'add' ? true : false
|
30
|
+
else
|
31
|
+
@table_action = 'alter'
|
32
|
+
if file_name =~ /^(alter)_(.*)/
|
33
|
+
@table_name = $2.pluralize
|
34
|
+
else
|
35
|
+
@table_name = file_name.pluralize
|
36
|
+
end
|
37
|
+
@use_change = false
|
38
|
+
@column_action = 'add'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
Sequel.migration do
|
2
|
+
<%- if use_change -%>
|
3
|
+
change do
|
4
|
+
<%= table_action %>_table :<%= table_name %> do
|
5
|
+
<%- if table_action == 'create' -%>
|
6
|
+
primary_key :id
|
7
|
+
<%- end -%>
|
8
|
+
<%- attributes.each do |attribute| -%>
|
9
|
+
<%- if table_action == 'create' -%>
|
10
|
+
<%= attribute.type_class %> :<%= attribute.name %>
|
11
|
+
<%- else -%>
|
12
|
+
<%= column_action %>_column :<%= attribute.name %><% if column_action == 'add' %>, <%= attribute.type_class %><% end %>
|
13
|
+
<%- end -%>
|
14
|
+
<%- end -%>
|
15
|
+
end
|
16
|
+
end
|
17
|
+
<%- else -%>
|
18
|
+
up do
|
19
|
+
<%- if table_action == 'drop' -%>
|
20
|
+
drop_table :<%= table_name %>
|
21
|
+
<%- else -%>
|
22
|
+
<%= table_action %>_table :<%= table_name %> do
|
23
|
+
<%- attributes.each do |attribute| -%>
|
24
|
+
<%- if table_action == 'create' -%>
|
25
|
+
<%= attribute.type_class %> :<%= attribute.name %>
|
26
|
+
<%- else -%>
|
27
|
+
<%= column_action %>_column :<%= attribute.name %><% if column_action == 'add' %>, <%= attribute.type_class %><% end %>
|
28
|
+
<%- end -%>
|
29
|
+
<%- end -%>
|
30
|
+
end
|
31
|
+
<%- end -%>
|
32
|
+
end
|
33
|
+
|
34
|
+
down do
|
35
|
+
<%- alter_table_action = (table_action == 'drop') ? 'create' : table_action -%>
|
36
|
+
<%- alter_column_action = (column_action == 'add') ? 'drop' : 'add' -%>
|
37
|
+
<%= alter_table_action %>_table :<%= table_name %> do
|
38
|
+
<%- attributes.each do |attribute| -%>
|
39
|
+
<%- if alter_table_action == 'create' -%>
|
40
|
+
<%= attribute.type_class %> :<%= attribute.name %>
|
41
|
+
<%- else -%>
|
42
|
+
<%= alter_column_action %>_column :<%= attribute.name %><% if alter_column_action == 'add' %>, <%= attribute.type_class %><% end %>
|
43
|
+
<%- end -%>
|
44
|
+
<%- end -%>
|
45
|
+
end
|
46
|
+
end
|
47
|
+
<%- end -%>
|
48
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
require File.expand_path('../../../sequel', __FILE__)
|
3
|
+
|
4
|
+
module Sequel::Generators
|
5
|
+
class ModelGenerator < Base
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
9
|
+
|
10
|
+
check_class_collision
|
11
|
+
|
12
|
+
class_option :migration, :type => :boolean
|
13
|
+
class_option :timestamps, :type => :boolean
|
14
|
+
class_option :parent, :type => :string, :desc => "The parent class for the generated model"
|
15
|
+
|
16
|
+
def create_migration_file
|
17
|
+
return unless options[:migration]
|
18
|
+
migration_template "migration.rb", "db/migrate/create_#{table_name}.rb"
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_model_file
|
22
|
+
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
23
|
+
end
|
24
|
+
|
25
|
+
hook_for :test_framework
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Sequel.migration do
|
2
|
+
change do
|
3
|
+
create_table :<%= table_name %> do
|
4
|
+
primary_key :id
|
5
|
+
<%- if options[:timestamps] -%>
|
6
|
+
DateTime :created_at
|
7
|
+
DateTime :updated_at
|
8
|
+
<%- end -%>
|
9
|
+
<%- attributes.each do |attribute| -%>
|
10
|
+
<%= attribute.type_class %> :<%= attribute.name %>
|
11
|
+
<%- end -%>
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
require File.expand_path('../../../sequel', __FILE__)
|
3
|
+
|
4
|
+
module Sequel::Generators
|
5
|
+
class ObserverGenerator < Base
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
check_class_collision :suffix => "Observer"
|
9
|
+
|
10
|
+
def create_observer_file
|
11
|
+
template 'observer.rb', File.join('app/models', class_path, "#{file_name}_observer.rb")
|
12
|
+
end
|
13
|
+
|
14
|
+
hook_for :test_framework
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
|
2
|
+
module Sequel
|
3
|
+
module Plugins
|
4
|
+
# The RailsExtensions plugin adds a single class method to Sequel::Model in
|
5
|
+
# order to emulate the behavior of ActiveRecord's `.find` method, where an
|
6
|
+
# exception is raised if a record cannot be found by the given id. Here, we
|
7
|
+
# raise a ModelNotFoundError, which is rescued in controllers like so:
|
8
|
+
#
|
9
|
+
# config.action_dispatch.rescue_responses.merge!(
|
10
|
+
# 'Sequel::Plugins::RailsExtensions::ModelNotFound' => :not_found
|
11
|
+
# )
|
12
|
+
#
|
13
|
+
module RailsExtensions
|
14
|
+
class ModelNotFound < Sequel::Error; end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def find!(args)
|
18
|
+
record = self[args]
|
19
|
+
raise ModelNotFound, "Couldn't find #{self} matching #{args}." unless record
|
20
|
+
return record
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
|
2
|
+
require 'active_support/core_ext/hash/keys'
|
3
|
+
|
4
|
+
module Sequel
|
5
|
+
module Rails
|
6
|
+
module Configuration
|
7
|
+
class << self
|
8
|
+
attr_accessor :logger, :db_environments
|
9
|
+
|
10
|
+
def truncate_sql_to=(len)
|
11
|
+
@sql_truncate_length = len
|
12
|
+
end
|
13
|
+
attr_reader :sql_truncate_length
|
14
|
+
|
15
|
+
def init_database(db_config)
|
16
|
+
@db_config = db_config
|
17
|
+
@db_environments = db_config.inject({}) { |hash, (name, config)|
|
18
|
+
hash[name.to_sym] = normalize_repository_config(config)
|
19
|
+
hash
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def db_config_for(name)
|
24
|
+
@db_environments[name.to_sym].merge(:logger => logger)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def normalize_repository_config(hash)
|
29
|
+
hash = hash.stringify_keys
|
30
|
+
|
31
|
+
port = hash.delete('port')
|
32
|
+
adapter = hash.delete('adapter')
|
33
|
+
database = hash.delete('database')
|
34
|
+
|
35
|
+
config = {}
|
36
|
+
config['port'] = port.try(:to_i)
|
37
|
+
config['adapter'] = case adapter
|
38
|
+
when 'sqlite3' then 'sqlite'
|
39
|
+
when 'postgresql' then 'postgres'
|
40
|
+
else adapter
|
41
|
+
end
|
42
|
+
config['database'] =
|
43
|
+
if adapter =~ /^sqlite3?/ and database != ':memory:'
|
44
|
+
File.expand_path(database, ::Rails.root)
|
45
|
+
else
|
46
|
+
database
|
47
|
+
end
|
48
|
+
|
49
|
+
config.merge!(hash)
|
50
|
+
config
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
|
2
|
+
# Prevent this file from being loaded twice
|
3
|
+
unless defined?(SEQUEL_RAILS)
|
4
|
+
|
5
|
+
SEQUEL_RAILS = 1
|
6
|
+
|
7
|
+
require 'sequel'
|
8
|
+
|
9
|
+
module Sequel
|
10
|
+
module Rails
|
11
|
+
|
12
|
+
PATH = ::File.expand_path('../../../..', __FILE__)
|
13
|
+
LIBPATH = ::File.join(PATH, 'lib')
|
14
|
+
|
15
|
+
# Get the root path of the project. If any arguments are given, they are
|
16
|
+
# concatenated to the path using `File.join`.
|
17
|
+
#
|
18
|
+
def self.path(*args)
|
19
|
+
rv = ::File.join(PATH, args.flatten)
|
20
|
+
if block_given?
|
21
|
+
begin
|
22
|
+
$LOAD_PATH.unshift(PATH)
|
23
|
+
rv = yield
|
24
|
+
ensure
|
25
|
+
$LOAD_PATH.shift
|
26
|
+
end
|
27
|
+
end
|
28
|
+
return rv
|
29
|
+
end
|
30
|
+
|
31
|
+
# Get the library path of the project. If any arguments are given, they are
|
32
|
+
# concatenated to the path using `File.join`.
|
33
|
+
#
|
34
|
+
def self.libpath(*args)
|
35
|
+
rv = ::File.join(LIBPATH, args.flatten)
|
36
|
+
if block_given?
|
37
|
+
begin
|
38
|
+
$LOAD_PATH.unshift(LIBPATH)
|
39
|
+
rv = yield
|
40
|
+
ensure
|
41
|
+
$LOAD_PATH.shift
|
42
|
+
end
|
43
|
+
end
|
44
|
+
return rv
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
|
2
|
+
Sequel::Database.class_eval do
|
3
|
+
# Override #log_yield so that if `config.sequel.truncate_sql_to` was specified
|
4
|
+
# in config/application.rb, when the SQL is query is logged, truncate it to
|
5
|
+
# the length. Also, if the query contains any line breaks, convert them to
|
6
|
+
# "\n".
|
7
|
+
#
|
8
|
+
def log_yield(sql, args=nil)
|
9
|
+
return yield if @loggers.empty?
|
10
|
+
sql = sql.gsub(/\n/, "\\n")
|
11
|
+
if len = Sequel::Rails.configuration.sql_truncate_length and sql.length > len
|
12
|
+
sql = sql[0...len] + '...'
|
13
|
+
end
|
14
|
+
sql = "#{sql}; #{args.inspect}" if args
|
15
|
+
start = Time.now
|
16
|
+
begin
|
17
|
+
yield
|
18
|
+
rescue => e
|
19
|
+
log_each(:error, "#{e.class}: #{e.message.strip}: #{sql}")
|
20
|
+
raise
|
21
|
+
ensure
|
22
|
+
log_duration(Time.now - start, sql) unless e
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|