sinatra-activerecord 1.1.2 → 1.2.0

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/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
- ## Instructions
7
+ ## Setup
10
8
 
11
- First, put the gem into your `Gemfile` (or install it manually):
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 'sinatra-activerecord'
13
+ gem "sinatra-activerecord"
14
+ gem "sqlite3"
15
15
  ```
16
16
 
17
- Also put one of the database adapters into your `Gemfile` (or install
18
- them manually):
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
- # app.rb
29
- require 'sinatra'
30
- require 'sinatra/activerecord'
21
+ require "sinatra/activerecord"
31
22
 
32
- set :database, 'sqlite3:///foo.db'
23
+ set :database, "sqlite3:///foo.sqlite3"
33
24
  ```
34
25
 
35
- Note that the database URL here has **3** slashes. This is the difference from
36
- <= 1.0.0 versions, where it was typed with 2 slashes.
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
- Also note that in **modular** Sinatra applications (ones in which you explicitly
39
- subclass `Sinatra::Base`), you will need to manually add the line:
29
+ Note that in **modular** Sinatra applications you will need to first register
30
+ the extension:
40
31
 
41
32
  ```ruby
42
- register Sinatra::ActiveRecordExtension
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 'sinatra/activerecord/rake'
49
- require './app'
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 in ./db/migrate
57
- rake db:migrate # migrate your database
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
- Now you can create a migration:
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 the `./db/migrate` folder, ready for editing.
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 up
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
- After you've written the migration, migrate the database:
77
+ Now migrate the database:
83
78
 
84
79
  ```sh
85
80
  $ rake db:migrate
86
81
  ```
87
82
 
88
- You can then also write the model:
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 the models anywhere. It's probably best to put them in an
97
- external file, and require them in your `app.rb`. Usually
98
- models in Sinatra aren't that complex, so you can put them all in one
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. This means that you can write something
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, one of the authors of Sinatra.
122
+ This gem was made in 2009 by Blake Mizerany, creator of Sinatra.
135
123
 
136
124
  ## Social
137
125
 
@@ -44,7 +44,7 @@ module Sinatra
44
44
  private
45
45
 
46
46
  def migrations_dir
47
- "db/migrate"
47
+ ActiveRecord::Migrator.migrations_paths.first
48
48
  end
49
49
 
50
50
  def silence_activerecord(&block)
@@ -1,7 +1,7 @@
1
1
  require 'rake'
2
2
 
3
3
  namespace :db do
4
- desc "create an ActiveRecord migration in ./db/migrate"
4
+ desc "create an ActiveRecord migration"
5
5
  task :create_migration do
6
6
  Sinatra::ActiveRecordTasks.create_migration(ENV["NAME"], ENV["VERSION"])
7
7
  end
@@ -20,18 +20,35 @@ module Sinatra
20
20
  def database
21
21
  @database ||= begin
22
22
  ActiveRecord::Base.logger = activerecord_logger
23
- spec = resolve_spec(database_url)
24
- ActiveRecord::Base.establish_connection(spec)
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
- protected
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.database if ENV['DATABASE_URL'] # Force connection if DATABASE_URL is set
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
- private
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?://[A-Za-z_-]+\.(db|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.1.2
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-11-02 00:00:00.000000000 Z
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: -2180039102864061955
127
+ hash: -4164762021753065434
127
128
  requirements: []
128
129
  rubyforge_project:
129
130
  rubygems_version: 1.8.23