sinatra-activerecord 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +21 -17
- data/lib/sinatra/activerecord.rb +3 -2
- data/lib/sinatra/activerecord/rake.rb +59 -1
- data/lib/sinatra/activerecord/tasks.rake +18 -0
- metadata +42 -17
- data/lib/sinatra/activerecord/rake_tasks.rake +0 -44
data/README.md
CHANGED
@@ -21,14 +21,32 @@ them manually):
|
|
21
21
|
- `mysql` (MySQL)
|
22
22
|
- `pg` (PostgreSQL)
|
23
23
|
|
24
|
-
|
24
|
+
Now specify the database in your `app.rb`
|
25
|
+
(let's assume you chose the `sqlite3` adapter):
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# app.rb
|
29
|
+
require 'sinatra'
|
30
|
+
require 'sinatra/activerecord'
|
31
|
+
|
32
|
+
set :database, 'sqlite:///foo.db'
|
33
|
+
```
|
34
|
+
|
35
|
+
Note that in **modular** Sinatra applications (ones in which you explicitly
|
36
|
+
subclass `Sinatra::Base`), you will need to manually add the line:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
register Sinatra::ActiveRecordExtension
|
40
|
+
```
|
41
|
+
|
42
|
+
Now require the rake tasks and your app in your `Rakefile`:
|
25
43
|
|
26
44
|
```ruby
|
27
45
|
require 'sinatra/activerecord/rake'
|
28
46
|
require './app'
|
29
47
|
```
|
30
48
|
|
31
|
-
In the
|
49
|
+
In the Terminal test that it works:
|
32
50
|
|
33
51
|
```
|
34
52
|
$ rake -T
|
@@ -73,24 +91,10 @@ end
|
|
73
91
|
```
|
74
92
|
|
75
93
|
You can put the models anywhere. It's probably best to put them in an
|
76
|
-
external file, and require them in your `app.rb
|
94
|
+
external file, and require them in your `app.rb`. Usually
|
77
95
|
models in Sinatra aren't that complex, so you can put them all in one
|
78
96
|
file, for example `./db/models.rb`.
|
79
97
|
|
80
|
-
Now just establish the database connection in your `app.rb`
|
81
|
-
(let's assume you chose the `sqlite3` adapter), and
|
82
|
-
require the models if necessary:
|
83
|
-
|
84
|
-
```ruby
|
85
|
-
# app.rb
|
86
|
-
require 'sinatra'
|
87
|
-
require 'sinatra/activerecord'
|
88
|
-
|
89
|
-
require './db/models'
|
90
|
-
|
91
|
-
set :database, 'sqlite:///foo.db'
|
92
|
-
```
|
93
|
-
|
94
98
|
Now everything just works:
|
95
99
|
|
96
100
|
```ruby
|
data/lib/sinatra/activerecord.rb
CHANGED
@@ -12,14 +12,15 @@ module Sinatra
|
|
12
12
|
module ActiveRecordExtension
|
13
13
|
def database=(url)
|
14
14
|
set :database_url, url
|
15
|
+
@database = nil
|
15
16
|
database
|
16
17
|
end
|
17
18
|
|
18
19
|
def database
|
19
20
|
@database ||= begin
|
20
21
|
ActiveRecord::Base.logger = activerecord_logger
|
21
|
-
|
22
|
-
ActiveRecord::Base.establish_connection(
|
22
|
+
spec = database_url.is_a?(String) ? database_url.sub(/^sqlite:/, "sqlite3:") : database_url
|
23
|
+
ActiveRecord::Base.establish_connection(spec)
|
23
24
|
ActiveRecord::Base
|
24
25
|
end
|
25
26
|
end
|
@@ -1 +1,59 @@
|
|
1
|
-
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support/core_ext/string/strip'
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
module Sinatra
|
6
|
+
module ActiveRecordTasks
|
7
|
+
extend self
|
8
|
+
|
9
|
+
def create_migration(migration_name, version = nil)
|
10
|
+
raise "No NAME specified. Example usage: `rake db:create_migration NAME=create_users`" if migration_name.nil?
|
11
|
+
|
12
|
+
migration_number = version || Time.now.utc.strftime("%Y%m%d%H%M%S")
|
13
|
+
migration_file = File.join(migrations_dir, "#{migration_number}_#{migration_name}.rb")
|
14
|
+
migration_class = migration_name.split("_").map(&:capitalize).join
|
15
|
+
|
16
|
+
FileUtils.mkdir_p(migrations_dir)
|
17
|
+
File.open(migration_file, 'w') do |file|
|
18
|
+
file.write <<-MIGRATION.strip_heredoc
|
19
|
+
class #{migration_class} < ActiveRecord::Migration
|
20
|
+
def up
|
21
|
+
end
|
22
|
+
|
23
|
+
def down
|
24
|
+
end
|
25
|
+
end
|
26
|
+
MIGRATION
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def migrate(version = nil)
|
31
|
+
silence_activerecord do
|
32
|
+
migration_version = version ? version.to_i : version
|
33
|
+
ActiveRecord::Migrator.migrate(migrations_dir, migration_version)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def rollback(step = nil)
|
38
|
+
silence_activerecord do
|
39
|
+
migration_step = step ? step.to_i : 1
|
40
|
+
ActiveRecord::Migrator.rollback(migrations_dir, migration_step)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def migrations_dir
|
47
|
+
"db/migrate"
|
48
|
+
end
|
49
|
+
|
50
|
+
def silence_activerecord(&block)
|
51
|
+
old_logger = ActiveRecord::Base.logger
|
52
|
+
ActiveRecord::Base.logger = nil
|
53
|
+
yield if block_given?
|
54
|
+
ActiveRecord::Base.logger = old_logger
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
load 'sinatra/activerecord/tasks.rake'
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
namespace :db do
|
4
|
+
desc "create an ActiveRecord migration in ./db/migrate"
|
5
|
+
task :create_migration do
|
6
|
+
Sinatra::ActiveRecordTasks.create_migration(ENV["NAME"], ENV["VERSION"])
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "migrate the database (use version with VERSION=n)"
|
10
|
+
task :migrate do
|
11
|
+
Sinatra::ActiveRecordTasks.migrate(ENV["VERSION"])
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "roll back the migration (use steps with STEP=n)"
|
15
|
+
task :rollback do
|
16
|
+
Sinatra::ActiveRecordTasks.rollback(ENV["STEP"])
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sinatra
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '1.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: activerecord
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ~>
|
@@ -32,10 +37,15 @@ dependencies:
|
|
32
37
|
version: '3.0'
|
33
38
|
type: :runtime
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.0'
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: bundler
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ~>
|
@@ -43,10 +53,15 @@ dependencies:
|
|
43
53
|
version: '1.0'
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: rake
|
49
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
50
65
|
none: false
|
51
66
|
requirements:
|
52
67
|
- - ~>
|
@@ -54,10 +69,15 @@ dependencies:
|
|
54
69
|
version: '0.9'
|
55
70
|
type: :development
|
56
71
|
prerelease: false
|
57
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0.9'
|
58
78
|
- !ruby/object:Gem::Dependency
|
59
79
|
name: rspec
|
60
|
-
requirement:
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
61
81
|
none: false
|
62
82
|
requirements:
|
63
83
|
- - ! '>='
|
@@ -68,7 +88,15 @@ dependencies:
|
|
68
88
|
version: '3'
|
69
89
|
type: :development
|
70
90
|
prerelease: false
|
71
|
-
version_requirements:
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.10'
|
97
|
+
- - <
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '3'
|
72
100
|
description: Extends Sinatra with ActiveRecord helpers.
|
73
101
|
email: janko.marohnic@gmail.com
|
74
102
|
executables: []
|
@@ -76,7 +104,7 @@ extensions: []
|
|
76
104
|
extra_rdoc_files: []
|
77
105
|
files:
|
78
106
|
- lib/sinatra/activerecord/rake.rb
|
79
|
-
- lib/sinatra/activerecord/
|
107
|
+
- lib/sinatra/activerecord/tasks.rake
|
80
108
|
- lib/sinatra/activerecord.rb
|
81
109
|
- README.md
|
82
110
|
- LICENSE
|
@@ -99,12 +127,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
127
|
- - ! '>='
|
100
128
|
- !ruby/object:Gem::Version
|
101
129
|
version: '0'
|
102
|
-
segments:
|
103
|
-
- 0
|
104
|
-
hash: 2210364565815214899
|
105
130
|
requirements: []
|
106
131
|
rubyforge_project:
|
107
|
-
rubygems_version: 1.8.
|
132
|
+
rubygems_version: 1.8.23
|
108
133
|
signing_key:
|
109
134
|
specification_version: 3
|
110
135
|
summary: Extends Sinatra with ActiveRecord helpers.
|
@@ -1,44 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
require 'active_support/core_ext/string/strip'
|
3
|
-
require 'fileutils'
|
4
|
-
|
5
|
-
namespace :db do
|
6
|
-
desc "create an ActiveRecord migration in ./db/migrate"
|
7
|
-
task :create_migration do
|
8
|
-
name = ENV['NAME']
|
9
|
-
if name.nil?
|
10
|
-
raise "No NAME specified. Example usage: `rake db:create_migration NAME=create_users`"
|
11
|
-
end
|
12
|
-
|
13
|
-
migrations_dir = File.join("db", "migrate")
|
14
|
-
version = ENV["VERSION"] || Time.now.utc.strftime("%Y%m%d%H%M%S")
|
15
|
-
filename = "#{version}_#{name}.rb"
|
16
|
-
migration_class = name.split("_").map(&:capitalize).join
|
17
|
-
|
18
|
-
FileUtils.mkdir_p(migrations_dir)
|
19
|
-
|
20
|
-
File.open(File.join(migrations_dir, filename), 'w') do |file|
|
21
|
-
file.write <<-MIGRATION.strip_heredoc
|
22
|
-
class #{migration_class} < ActiveRecord::Migration
|
23
|
-
def up
|
24
|
-
end
|
25
|
-
|
26
|
-
def down
|
27
|
-
end
|
28
|
-
end
|
29
|
-
MIGRATION
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
desc "migrate the database (use version with VERSION=n)"
|
34
|
-
task :migrate do
|
35
|
-
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
36
|
-
ActiveRecord::Migrator.migrate('db/migrate', version)
|
37
|
-
end
|
38
|
-
|
39
|
-
desc "rolls back the migration (use steps with STEP=n)"
|
40
|
-
task :rollback do
|
41
|
-
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
|
42
|
-
ActiveRecord::Migrator.rollback('db/migrate', step)
|
43
|
-
end
|
44
|
-
end
|