sinatra-activerecord 0.1.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Janko Marohnić
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ 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
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,98 +1,131 @@
1
- Sinatra ActiveRecord Extension
2
- ========================
1
+ # Sinatra ActiveRecord Extension
3
2
 
4
- Extends [Sinatra](http://www.sinatrarb.com/) with a extension methods and Rake
5
- tasks for dealing with a SQL database using the [ActiveRecord ORM](http://api.rubyonrails.org/).
3
+ ## About
6
4
 
7
- Install the `sinatra-activerecord` gem along with one of the database adapters:
5
+ Extends [Sinatra](http://www.sinatrarb.com/) with extension methods and Rake
6
+ tasks for dealing with an SQL database using the
7
+ [ActiveRecord ORM](https://github.com/rails/rails/tree/master/activerecord).
8
8
 
9
- sudo gem install activerecord
10
- sudo gem install sinatra-activerecord -s http://gemcutter.org
11
- sudo gem install sqlite3
12
- sudo gem install mysql
13
- sudo gem install postgres
9
+ ## Instructions
14
10
 
15
- adding this to your `Rakefile`
11
+ First, put the gem into your `Gemfile` (or install it manually):
16
12
 
17
- # require your app file first
18
- require 'sinatra-ar-exmple-app'
19
- require 'sinatra/activerecord/rake'
13
+ ```ruby
14
+ gem 'sinatra-activerecord'
15
+ ```
20
16
 
21
- $ rake -T
22
- rake db:create_migration # create an ActiveRecord migration in ./db/migrate
23
- rake db:migrate # migrate your database
17
+ Also put one of the database adapters into your `Gemfile` (or install
18
+ them manually):
24
19
 
25
- create a migration
20
+ - `sqlite3` (SQLite)
21
+ - `mysql` (MySQL)
22
+ - `pg` (PostgreSQL)
26
23
 
27
- $ rake db:create_migration NAME=create_foos
28
- $ vim db/migrate/20090922043513_create_foos.rb
24
+ Then require the rake tasks and your app in your `Rakefile`:
29
25
 
30
- class CreateFoos < ActiveRecord::Migration
31
- def self.up
32
- create_table :foos do |t|
33
- t.string :name
34
- end
35
- end
26
+ ```ruby
27
+ require 'sinatra/activerecord/rake'
28
+ require './app'
29
+ ```
36
30
 
37
- def self.down
38
- end
31
+ In the terminal, test that it works:
32
+
33
+ ```
34
+ $ rake -T
35
+ rake db:create_migration # create an ActiveRecord migration in ./db/migrate
36
+ rake db:migrate # migrate your database
37
+ ```
38
+
39
+ Now you can create a migration:
40
+
41
+ ```
42
+ $ rake db:create_migration NAME=create_users
43
+ ```
44
+
45
+ This will create a migration file in the `./db/migrate` folder, ready for editing.
46
+
47
+ ```ruby
48
+ class CreateUsers < ActiveRecord::Migration
49
+ def up
50
+ create_table :users do |t|
51
+ t.string :name
39
52
  end
53
+ end
40
54
 
41
- run the migration
55
+ def down
56
+ drop_table :users
57
+ end
58
+ end
59
+ ```
42
60
 
43
- $ rake db:migrate
61
+ After you've written the migration, migrate the database:
44
62
 
45
- I like to split models out into a separate `database.rb` file and then
46
- require it from the main app file, but you can plop
47
- the following code in about anywhere and it'll work just fine:
63
+ ```
64
+ $ rake db:migrate
65
+ ```
48
66
 
49
- require 'sinatra'
50
- require 'sinatra/activerecord'
67
+ You can then also write the model:
51
68
 
52
- # Establish the database connection; or, omit this and use the DATABASE_URL
53
- # environment variable or the default sqlite://<environment>.db as the connection string:
54
- set :database, 'sqlite://foo.db'
69
+ ```ruby
70
+ class User < ActiveRecord::Base
71
+ validates_presence_of :name
72
+ end
73
+ ```
55
74
 
56
- # At this point, you can access the ActiveRecord::Base class using the
57
- # "database" object:
58
- puts "the foos table doesn't exist" if !database.table_exists?('foos')
75
+ 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` aftewards. Usually
77
+ models in Sinatra aren't that complex, so you can put them all in one
78
+ file, for example `./db/models.rb`.
59
79
 
60
- # models just work ...
61
- class Foo < ActiveRecord::Base
62
- end
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:
63
83
 
64
- # see:
65
- Foo.all
84
+ ```ruby
85
+ # app.rb
86
+ require 'sinatra'
87
+ require 'sinatra/activerecord'
66
88
 
67
- # access the models within the context of an HTTP request
68
- get '/foos/:id' do
69
- @foo = Foo.find(params[:id])
70
- erb :foos
71
- end
89
+ require './db/models'
90
+
91
+ set :database, 'sqlite://foo.db'
92
+ ```
93
+
94
+ Now everything just works:
95
+
96
+ ```ruby
97
+ get '/users' do
98
+ @users = User.all
99
+ erb :index
100
+ end
101
+
102
+ get '/users/:id' do
103
+ @user = User.find(params[:id])
104
+ erb :show
105
+ end
106
+ ```
107
+
108
+ A nice thing is that the `ActiveRecord::Base` class is available to
109
+ you through the `database` variable. This means that you can write something
110
+ like this:
111
+
112
+ ```ruby
113
+ if database.table_exists?('users')
114
+ # Do stuff
115
+ else
116
+ raise "The table 'users' doesn't exist."
117
+ end
118
+ ```
72
119
 
73
- ### NOTE about the rip-off
120
+ ## Changelog
74
121
 
75
- This Code and README.md is a heavy adaption of [rtomayko's sinatra-sequel](http://github.com/rtomayko/sinatra-sequel/)
122
+ You can see the changelog
123
+ [here](https://github.com/janko-m/sinatra-activerecord/blob/master/CHANGELOG.md).
76
124
 
77
- Copyright (c) 2009 Blake Mizerany
125
+ ## History
78
126
 
79
- Permission is hereby granted, free of charge, to any person
80
- obtaining a copy of this software and associated documentation
81
- files (the "Software"), to deal in the Software without
82
- restriction, including without limitation the rights to use,
83
- copy, modify, merge, publish, distribute, sublicense, and/or sell
84
- copies of the Software, and to permit persons to whom the
85
- Software is furnished to do so, subject to the following
86
- conditions:
127
+ This gem was made in 2009 by Blake Mizerany, one of the authors of Sinatra.
87
128
 
88
- The above copyright notice and this permission notice shall be
89
- included in all copies or substantial portions of the Software.
129
+ ## License
90
130
 
91
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
92
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
93
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
94
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
95
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
96
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
97
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
98
- OTHER DEALINGS IN THE SOFTWARE.
131
+ [MIT](https://github.com/janko-m/sinatra-activerecord/blob/master/LICENSE)
@@ -1,5 +1,3 @@
1
- require 'uri'
2
- require 'time'
3
1
  require 'sinatra/base'
4
2
  require 'active_record'
5
3
  require 'logger'
@@ -13,48 +11,27 @@ module Sinatra
13
11
 
14
12
  module ActiveRecordExtension
15
13
  def database=(url)
16
- @database = nil
17
14
  set :database_url, url
18
15
  database
19
16
  end
20
17
 
21
18
  def database
22
- @database ||= (
23
- url = URI(database_url)
19
+ @database ||= begin
24
20
  ActiveRecord::Base.logger = activerecord_logger
25
- ActiveRecord::Base.establish_connection(database_options)
21
+ ActiveRecord::Base.establish_connection(database_url.sub(/^sqlite/, "sqlite3"))
26
22
  ActiveRecord::Base
27
- )
23
+ end
28
24
  end
29
25
 
30
26
  protected
31
27
 
32
- def database_options
33
- url = URI(database_url)
34
- options = {
35
- :adapter => url.scheme,
36
- :host => url.host,
37
- :port => url.port,
38
- :database => url.path[1..-1],
39
- :username => url.user,
40
- :password => url.password
41
- }
42
- case url.scheme
43
- when "sqlite"
44
- options[:adapter] = "sqlite3"
45
- options[:database] = url.host
46
- when "postgres"
47
- options[:adapter] = "postgresql"
48
- end
49
- options.merge(database_extras)
50
- end
51
-
52
28
  def self.registered(app)
53
- app.set :database_url, lambda { ENV['DATABASE_URL'] || "sqlite://#{environment}.db" }
54
- app.set :database_extras, Hash.new
55
29
  app.set :activerecord_logger, Logger.new(STDOUT)
56
- app.database # force connection
57
30
  app.helpers ActiveRecordHelper
31
+
32
+ # re-connect if database connection dropped
33
+ app.before { ActiveRecord::Base.verify_active_connections! }
34
+ app.after { ActiveRecord::Base.clear_active_connections! }
58
35
  end
59
36
  end
60
37
 
@@ -1,37 +1 @@
1
- require 'active_record'
2
- require 'fileutils'
3
-
4
- namespace :db do
5
- desc "migrate your database"
6
- task :migrate do
7
- ActiveRecord::Migrator.migrate(
8
- 'db/migrate',
9
- ENV["VERSION"] ? ENV["VERSION"].to_i : nil
10
- )
11
- end
12
-
13
- desc "create an ActiveRecord migration in ./db/migrate"
14
- task :create_migration do
15
- name = ENV['NAME']
16
- abort("no NAME specified. use `rake db:create_migration NAME=create_users`") if !name
17
-
18
- migrations_dir = File.join("db", "migrate")
19
- version = ENV["VERSION"] || Time.now.utc.strftime("%Y%m%d%H%M%S")
20
- filename = "#{version}_#{name}.rb"
21
- migration_name = name.gsub(/_(.)/) { $1.upcase }.gsub(/^(.)/) { $1.upcase }
22
-
23
- FileUtils.mkdir_p(migrations_dir)
24
-
25
- open(File.join(migrations_dir, filename), 'w') do |f|
26
- f << (<<-EOS).gsub(" ", "")
27
- class #{migration_name} < ActiveRecord::Migration
28
- def self.up
29
- end
30
-
31
- def self.down
32
- end
33
- end
34
- EOS
35
- end
36
- end
37
- end
1
+ load 'sinatra/activerecord/rake_tasks.rake'
@@ -0,0 +1,44 @@
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
metadata CHANGED
@@ -1,88 +1,111 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sinatra-activerecord
3
- version: !ruby/object:Gem::Version
4
- hash: 29
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 3
10
- version: 0.1.3
11
6
  platform: ruby
12
- authors:
13
- - Blake Mizerany
7
+ authors:
8
+ - Janko Marohnić
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2009-09-21 00:00:00 -07:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-05-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: sinatra
16
+ requirement: &70294476610880 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
23
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
24
+ version_requirements: *70294476610880
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &70294476607980 !ruby/object:Gem::Requirement
25
28
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 51
30
- segments:
31
- - 0
32
- - 9
33
- - 4
34
- version: 0.9.4
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
35
33
  type: :runtime
36
- version_requirements: *id001
37
- description: Extends Sinatra with activerecord helpers for instant activerecord use
38
- email: blake.mizerany@gmail.com
34
+ prerelease: false
35
+ version_requirements: *70294476607980
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &70294476606740 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '1.0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70294476606740
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &70294476621660 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70294476621660
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec
60
+ requirement: &70294476618440 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '2.10'
66
+ - - <
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: *70294476618440
72
+ description: Extends Sinatra with ActiveRecord helpers.
73
+ email: janko.marohnic@gmail.com
39
74
  executables: []
40
-
41
75
  extensions: []
42
-
43
- extra_rdoc_files:
44
- - README.md
45
- files:
46
- - README.md
47
- - lib/sinatra/activerecord.rb
76
+ extra_rdoc_files: []
77
+ files:
48
78
  - lib/sinatra/activerecord/rake.rb
49
- - sinatra-activerecord.gemspec
50
- has_rdoc: true
51
- homepage: http://github.com/rtomayko/sinatra-activerecord
52
- licenses: []
53
-
79
+ - lib/sinatra/activerecord/rake_tasks.rake
80
+ - lib/sinatra/activerecord.rb
81
+ - README.md
82
+ - LICENSE
83
+ homepage: http://github.com/janko-m/sinatra-activerecord
84
+ licenses:
85
+ - MIT
54
86
  post_install_message:
55
- rdoc_options:
56
- - --line-numbers
57
- - --inline-source
58
- - --title
59
- - Sinatra::ActiveRecord
60
- require_paths:
87
+ rdoc_options: []
88
+ require_paths:
61
89
  - lib
62
- required_ruby_version: !ruby/object:Gem::Requirement
90
+ required_ruby_version: !ruby/object:Gem::Requirement
63
91
  none: false
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- hash: 3
68
- segments:
69
- - 0
70
- version: "0"
71
- required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: 1.9.2
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
97
  none: false
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- hash: 3
77
- segments:
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ segments:
78
103
  - 0
79
- version: "0"
104
+ hash: 4034299485721175520
80
105
  requirements: []
81
-
82
- rubyforge_project: bmizerany
83
- rubygems_version: 1.5.2
106
+ rubyforge_project:
107
+ rubygems_version: 1.8.11
84
108
  signing_key:
85
- specification_version: 2
86
- summary: Extends Sinatra with activerecord helpers for instant activerecord use
109
+ specification_version: 3
110
+ summary: Extends Sinatra with ActiveRecord helpers.
87
111
  test_files: []
88
-
@@ -1,33 +0,0 @@
1
- Gem::Specification.new do |s|
2
- s.specification_version = 2 if s.respond_to? :specification_version=
3
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
-
5
- s.name = 'sinatra-activerecord'
6
- s.version = '0.1.3'
7
- s.date = '2009-09-21'
8
-
9
- s.description = "Extends Sinatra with activerecord helpers for instant activerecord use"
10
- s.summary = s.description
11
-
12
- s.authors = ["Blake Mizerany"]
13
- s.email = "blake.mizerany@gmail.com"
14
-
15
- # = MANIFEST =
16
- s.files = %w[
17
- README.md
18
- lib/sinatra/activerecord.rb
19
- lib/sinatra/activerecord/rake.rb
20
- sinatra-activerecord.gemspec
21
- ]
22
- # = MANIFEST =
23
-
24
- s.extra_rdoc_files = %w[README.md]
25
- s.add_dependency 'sinatra', '>= 0.9.4'
26
-
27
- s.has_rdoc = true
28
- s.homepage = "http://github.com/rtomayko/sinatra-activerecord"
29
- s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Sinatra::ActiveRecord"]
30
- s.require_paths = %w[lib]
31
- s.rubyforge_project = 'bmizerany'
32
- s.rubygems_version = '1.1.1'
33
- end