sinatra-activerecord 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +36 -48
- data/lib/sinatra/activerecord/rake.rb +1 -1
- data/lib/sinatra/activerecord/tasks.rake +1 -1
- data/lib/sinatra/activerecord.rb +23 -6
- metadata +4 -3
data/README.md
CHANGED
@@ -1,91 +1,86 @@
|
|
1
1
|
# Sinatra ActiveRecord Extension
|
2
2
|
|
3
|
-
## About
|
4
|
-
|
5
3
|
Extends [Sinatra](http://www.sinatrarb.com/) with extension methods and Rake
|
6
4
|
tasks for dealing with an SQL database using the
|
7
5
|
[ActiveRecord ORM](https://github.com/rails/rails/tree/master/activerecord).
|
8
6
|
|
9
|
-
##
|
7
|
+
## Setup
|
10
8
|
|
11
|
-
|
9
|
+
Put it in your `Gemfile`, along with the adapter of your database. For
|
10
|
+
simplicity, let's assume you're using SQLite:
|
12
11
|
|
13
12
|
```ruby
|
14
|
-
gem
|
13
|
+
gem "sinatra-activerecord"
|
14
|
+
gem "sqlite3"
|
15
15
|
```
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- `sqlite3` (SQLite)
|
21
|
-
- `mysql` (MySQL)
|
22
|
-
- `pg` (PostgreSQL)
|
23
|
-
|
24
|
-
Now specify the database in your `app.rb`
|
25
|
-
(let's assume you chose the `sqlite3` adapter):
|
17
|
+
Now require it in your Sinatra application, and establish the database
|
18
|
+
connection:
|
26
19
|
|
27
20
|
```ruby
|
28
|
-
|
29
|
-
require 'sinatra'
|
30
|
-
require 'sinatra/activerecord'
|
21
|
+
require "sinatra/activerecord"
|
31
22
|
|
32
|
-
set :database,
|
23
|
+
set :database, "sqlite3:///foo.sqlite3"
|
33
24
|
```
|
34
25
|
|
35
|
-
|
36
|
-
|
26
|
+
Alternatively, you can set the database with a hash or a YAML file. Take a look at
|
27
|
+
[this wiki](https://github.com/janko-m/sinatra-activerecord/wiki/Alternative-database-setup).
|
37
28
|
|
38
|
-
|
39
|
-
|
29
|
+
Note that in **modular** Sinatra applications you will need to first register
|
30
|
+
the extension:
|
40
31
|
|
41
32
|
```ruby
|
42
|
-
|
33
|
+
class YourApplication < Sinatra::Base
|
34
|
+
register Sinatra::ActiveRecordExtension
|
35
|
+
end
|
43
36
|
```
|
44
37
|
|
45
38
|
Now require the rake tasks and your app in your `Rakefile`:
|
46
39
|
|
47
40
|
```ruby
|
48
|
-
require
|
49
|
-
require
|
41
|
+
require "sinatra/activerecord/rake"
|
42
|
+
require "./app"
|
50
43
|
```
|
51
44
|
|
52
45
|
In the Terminal test that it works:
|
53
46
|
|
54
47
|
```sh
|
55
48
|
$ rake -T
|
56
|
-
rake db:create_migration # create an ActiveRecord migration
|
57
|
-
rake db:migrate # migrate
|
49
|
+
rake db:create_migration # create an ActiveRecord migration
|
50
|
+
rake db:migrate # migrate the database (use version with VERSION=n)
|
51
|
+
rake db:rollback # roll back the migration (use steps with STEP=n)
|
58
52
|
```
|
59
53
|
|
60
|
-
|
54
|
+
And that's it, you're all set :)
|
55
|
+
|
56
|
+
## Usage
|
57
|
+
|
58
|
+
You can create a migration:
|
61
59
|
|
62
60
|
```sh
|
63
61
|
$ rake db:create_migration NAME=create_users
|
64
62
|
```
|
65
63
|
|
66
|
-
This will create a migration file in
|
64
|
+
This will create a migration file in your migrations directory (`./db/migrate`
|
65
|
+
by default), ready for editing.
|
67
66
|
|
68
67
|
```ruby
|
69
68
|
class CreateUsers < ActiveRecord::Migration
|
70
|
-
def
|
69
|
+
def change
|
71
70
|
create_table :users do |t|
|
72
71
|
t.string :name
|
73
72
|
end
|
74
73
|
end
|
75
|
-
|
76
|
-
def down
|
77
|
-
drop_table :users
|
78
|
-
end
|
79
74
|
end
|
80
75
|
```
|
81
76
|
|
82
|
-
|
77
|
+
Now migrate the database:
|
83
78
|
|
84
79
|
```sh
|
85
80
|
$ rake db:migrate
|
86
81
|
```
|
87
82
|
|
88
|
-
You can
|
83
|
+
You can also write models:
|
89
84
|
|
90
85
|
```ruby
|
91
86
|
class User < ActiveRecord::Base
|
@@ -93,10 +88,9 @@ class User < ActiveRecord::Base
|
|
93
88
|
end
|
94
89
|
```
|
95
90
|
|
96
|
-
You can put
|
97
|
-
|
98
|
-
|
99
|
-
file, for example `./db/models.rb`.
|
91
|
+
You can put your models anywhere you want. If you put them in a separate file,
|
92
|
+
just remember to require the file, and to load the models **after** requiring
|
93
|
+
`"sinatra/activerecord"`.
|
100
94
|
|
101
95
|
Now everything just works:
|
102
96
|
|
@@ -113,8 +107,7 @@ end
|
|
113
107
|
```
|
114
108
|
|
115
109
|
A nice thing is that the `ActiveRecord::Base` class is available to
|
116
|
-
you through the `database` variable
|
117
|
-
like this:
|
110
|
+
you through the `database` variable:
|
118
111
|
|
119
112
|
```ruby
|
120
113
|
if database.table_exists?('users')
|
@@ -124,14 +117,9 @@ else
|
|
124
117
|
end
|
125
118
|
```
|
126
119
|
|
127
|
-
## Changelog
|
128
|
-
|
129
|
-
You can see the changelog
|
130
|
-
[here](https://github.com/janko-m/sinatra-activerecord/blob/master/CHANGELOG.md).
|
131
|
-
|
132
120
|
## History
|
133
121
|
|
134
|
-
This gem was made in 2009 by Blake Mizerany,
|
122
|
+
This gem was made in 2009 by Blake Mizerany, creator of Sinatra.
|
135
123
|
|
136
124
|
## Social
|
137
125
|
|
data/lib/sinatra/activerecord.rb
CHANGED
@@ -20,18 +20,35 @@ module Sinatra
|
|
20
20
|
def database
|
21
21
|
@database ||= begin
|
22
22
|
ActiveRecord::Base.logger = activerecord_logger
|
23
|
-
|
24
|
-
ActiveRecord::Base.
|
23
|
+
ActiveRecord::Base.establish_connection(resolve_spec(database_url))
|
24
|
+
ActiveRecord::Base.connection
|
25
25
|
ActiveRecord::Base
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
|
29
|
+
def database_file=(path)
|
30
|
+
require 'pathname'
|
31
|
+
|
32
|
+
return if app_file.nil?
|
33
|
+
path = File.join(app_file, path) if Pathname.new(path).relative?
|
34
|
+
|
35
|
+
if File.exists?(path)
|
36
|
+
require 'yaml'
|
37
|
+
require 'erb'
|
38
|
+
|
39
|
+
database_hash = YAML.load(ERB.new(File.read(path)).result) || {}
|
40
|
+
database_hash = database_hash[environment] if database_hash[environment]
|
41
|
+
set :database, database_hash
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
30
46
|
|
31
47
|
def self.registered(app)
|
32
48
|
app.set :activerecord_logger, Logger.new(STDOUT)
|
33
49
|
app.set :database_url, ENV['DATABASE_URL']
|
34
|
-
app.
|
50
|
+
app.set :database_file, "config/database.yml"
|
51
|
+
app.database if app.database_url
|
35
52
|
app.helpers ActiveRecordHelper
|
36
53
|
|
37
54
|
# re-connect if database connection dropped
|
@@ -39,11 +56,11 @@ module Sinatra
|
|
39
56
|
app.after { ActiveRecord::Base.clear_active_connections! }
|
40
57
|
end
|
41
58
|
|
42
|
-
|
59
|
+
private
|
43
60
|
|
44
61
|
def resolve_spec(database_url)
|
45
62
|
if database_url.is_a?(String)
|
46
|
-
if database_url =~ %r{^sqlite3?://[
|
63
|
+
if database_url =~ %r{^sqlite3?://[^/]+$}
|
47
64
|
warn <<-MESSAGE.strip_heredoc
|
48
65
|
It seems your database URL looks something like this: "sqlite3://<database_name>".
|
49
66
|
This doesn't work anymore, you need to use 3 slashes, like this: "sqlite3:///<database_name>".
|
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
+
- Blake Mizerany
|
8
9
|
- Janko Marohnić
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2012-
|
13
|
+
date: 2012-12-21 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: sinatra
|
@@ -123,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
124
|
version: '0'
|
124
125
|
segments:
|
125
126
|
- 0
|
126
|
-
hash: -
|
127
|
+
hash: -4164762021753065434
|
127
128
|
requirements: []
|
128
129
|
rubyforge_project:
|
129
130
|
rubygems_version: 1.8.23
|