grape-activerecord 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +19 -0
- data/README.rdoc +54 -0
- data/lib/grape/activerecord/activerecord.rb +42 -0
- data/lib/grape/activerecord/extension.rb +13 -0
- data/lib/grape/activerecord/rake/activerecord_4.rb +18 -0
- data/lib/grape/activerecord/rake.rb +7 -0
- data/lib/grape/activerecord/tasks.rake +39 -0
- data/lib/grape/activerecord/version.rb +6 -0
- data/lib/grape/activerecord.rb +4 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 40f258710a91aed710575edce515cbaf12b17c00
|
4
|
+
data.tar.gz: c91ac2913e6761842ffc60434c1f59894ce60ea3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9d8f767fb5471c2cefedb1539105a1e970033b7aa8866484e37dfe1726ba469ab82b56ddb8614cca775e31208391564cd90f848e6f49cebfb0a7f8f24f1b7306
|
7
|
+
data.tar.gz: 28ce37f584d292cd9f01ad73b84079e28be9a4f633154d22d0c50809c82359f841e351a1cb7aea0212d5bed9de271851cee1bb63c3d202702735dd952bf963da
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2014 Jordan Hollinger
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
== grape-activerecord
|
2
|
+
|
3
|
+
Simple integration of ActiveRecord connections and Rake tasks with Grape. Supports ActiveRecord 4.x.
|
4
|
+
Inspired by sinatra-activerecord (https://github.com/janko-m/sinatra-activerecord).
|
5
|
+
|
6
|
+
=== Add to your Gemfile
|
7
|
+
|
8
|
+
gem 'grape-activerecord'
|
9
|
+
|
10
|
+
=== Set database config
|
11
|
+
|
12
|
+
# Use a Hash
|
13
|
+
Grape::ActiveRecord.database = {adapter: 'postgresql', host: 'localhost', database: 'db', username: 'user', password: 'pass', encoding: 'utf8', pool: 10, timeout: 5000}
|
14
|
+
|
15
|
+
# or a URL
|
16
|
+
Grape::ActiveRecord.database_url = 'postgres://user:pass@localhost/db'
|
17
|
+
|
18
|
+
# or a YAML file
|
19
|
+
Grape::ActiveRecord.database_file = 'config/database.yml'
|
20
|
+
|
21
|
+
If you don't specify any configuration, grape-activerecord will first look in DATABASE_URL, then in config/database.yml.
|
22
|
+
|
23
|
+
=== Include Grape::ActiveRecord::Extension
|
24
|
+
|
25
|
+
class MyApi < Grape::API
|
26
|
+
include Grape::ActiveRecord::Extension
|
27
|
+
...
|
28
|
+
end
|
29
|
+
|
30
|
+
This will establish the database connections and correctly return used connections to the pool after each request.
|
31
|
+
NOTE If you're mounting sub Grape apps, you need only include grape-activerecord in the root app.
|
32
|
+
|
33
|
+
=== Add ActiveRecord tasks to your Rakefile
|
34
|
+
|
35
|
+
# Add at the bottom of your Rakefile
|
36
|
+
require 'grape/activerecord/rake'
|
37
|
+
|
38
|
+
This will give you most of the standard "db:" tasks you get in Rails. Run "bundle exec rake -T" to get a full list.
|
39
|
+
Note that unlike in Rails, creating a new migration is also a rake task:
|
40
|
+
|
41
|
+
bundle exec rake db:create_migration NAME=create_widgets
|
42
|
+
|
43
|
+
=== Customize ActiveRecord rake tasks config
|
44
|
+
|
45
|
+
# The defaults are the same as Rails'
|
46
|
+
ActiveRecord::Tasks::DatabaseTasks.tap do |config|
|
47
|
+
config.db_dir = 'db'
|
48
|
+
config.migrations_paths = ['db/migrate']
|
49
|
+
config.fixtures_path = 'test/fixtures'
|
50
|
+
end
|
51
|
+
|
52
|
+
=== Example app
|
53
|
+
|
54
|
+
Look under /examples for some example apps.
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# Grape and ActiveRecord integration
|
2
|
+
module Grape
|
3
|
+
# Grape and ActiveRecord integration
|
4
|
+
module ActiveRecord
|
5
|
+
class << self
|
6
|
+
# A Hash representing the database connection
|
7
|
+
attr_accessor :database
|
8
|
+
# A database connection URL (default ENV['DATABASE_URL'])
|
9
|
+
attr_accessor :database_url
|
10
|
+
# YAML file where all database configurations are stored (default config/database.yml)
|
11
|
+
attr_accessor :database_file
|
12
|
+
end
|
13
|
+
self.database_url = ENV['DATABASE_URL']
|
14
|
+
self.database_file = 'config/database.yml'
|
15
|
+
|
16
|
+
# The current Rack environment
|
17
|
+
RACK_ENV = (ENV['RACK_ENV'] || 'development').to_sym
|
18
|
+
|
19
|
+
# Read configuration and connect to the database
|
20
|
+
def self.setup!
|
21
|
+
::ActiveRecord::Base.default_timezone = :utc
|
22
|
+
::ActiveRecord::Base.logger = Logger.new(STDOUT)
|
23
|
+
|
24
|
+
if spec = Grape::ActiveRecord.database
|
25
|
+
::ActiveRecord::Base.configurations = {RACK_ENV => spec}.stringify_keys
|
26
|
+
::ActiveRecord::Base.establish_connection(RACK_ENV)
|
27
|
+
|
28
|
+
elsif spec = Grape::ActiveRecord.database_url
|
29
|
+
::ActiveRecord::Base.establish_connection(spec)
|
30
|
+
::ActiveRecord::Base.configurations = {RACK_ENV.to_s => ::ActiveRecord::Base.connection.pool.spec.config}
|
31
|
+
|
32
|
+
elsif path = Grape::ActiveRecord::database_file
|
33
|
+
raise "#{path} does not exist!" unless File.file? path
|
34
|
+
::ActiveRecord::Base.configurations = YAML.load(ERB.new(File.read(path)).result) || {}
|
35
|
+
::ActiveRecord::Base.establish_connection(RACK_ENV)
|
36
|
+
|
37
|
+
else
|
38
|
+
raise RuntimeError, 'You must configure a database connection!'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Grape
|
2
|
+
module ActiveRecord
|
3
|
+
# Including this module in your Grape app establishes the database connection and connection management
|
4
|
+
module Extension
|
5
|
+
# Establishes db connection and management
|
6
|
+
def self.included(app)
|
7
|
+
Grape::ActiveRecord.setup!
|
8
|
+
app.before { ::ActiveRecord::Base.verify_active_connections! if ::ActiveRecord::Base.respond_to?(:verify_active_connections!) }
|
9
|
+
app.after { ::ActiveRecord::Base.clear_active_connections! }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
ActiveRecord::Tasks::DatabaseTasks.tap do |config|
|
2
|
+
config.root = Rake.application.original_dir
|
3
|
+
config.env = Grape::ActiveRecord::RACK_ENV.to_s
|
4
|
+
config.db_dir = 'db'
|
5
|
+
config.migrations_paths = ['db/migrate']
|
6
|
+
config.fixtures_path = 'test/fixtures'
|
7
|
+
config.database_configuration = ActiveRecord::Base.configurations
|
8
|
+
config.seed_loader = Object.new
|
9
|
+
config.seed_loader.instance_eval do
|
10
|
+
def load_seed
|
11
|
+
load "#{ActiveRecord::Tasks::DatabaseTasks.db_dir}/seeds.rb"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
Rake::Task['db:load_config'].clear
|
17
|
+
Rake::Task.define_task('db:environment')
|
18
|
+
Rake::Task['db:test:deprecated'].clear if Rake::Task.task_defined?('db:test:deprecated')
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "pathname"
|
2
|
+
require "fileutils"
|
3
|
+
require "active_support/core_ext/string/strip"
|
4
|
+
|
5
|
+
namespace :db do
|
6
|
+
desc "Create a migration (parameters: NAME, VERSION)"
|
7
|
+
task :create_migration do
|
8
|
+
unless ENV["NAME"]
|
9
|
+
puts "No NAME specified. Example usage: `rake db:create_migration NAME=create_widgets`"
|
10
|
+
exit 1
|
11
|
+
end
|
12
|
+
|
13
|
+
name = ENV["NAME"]
|
14
|
+
version = ENV["VERSION"] || Time.now.utc.strftime("%Y%m%d%H%M%S")
|
15
|
+
|
16
|
+
ActiveRecord::Migrator.migrations_paths.each do |directory|
|
17
|
+
next unless File.exists?(directory)
|
18
|
+
migration_files = Pathname(directory).children
|
19
|
+
if duplicate = migration_files.find { |path| path.basename.to_s.include?(name) }
|
20
|
+
puts "Another migration is already named \"#{name}\": #{duplicate}."
|
21
|
+
exit 1
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
filename = "#{version}_#{name}.rb"
|
26
|
+
dirname = ActiveRecord::Migrator.migrations_path
|
27
|
+
path = File.join(dirname, filename)
|
28
|
+
|
29
|
+
FileUtils.mkdir_p(dirname)
|
30
|
+
File.write path, <<-MIGRATION.strip_heredoc
|
31
|
+
class #{name.camelize} < ActiveRecord::Migration
|
32
|
+
def change
|
33
|
+
end
|
34
|
+
end
|
35
|
+
MIGRATION
|
36
|
+
|
37
|
+
puts path
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grape-activerecord
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jordan Hollinger
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: grape
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.0'
|
41
|
+
description: Extends Grape with simple ActiveRecord integration
|
42
|
+
email: jordan.hollinger@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- lib/grape/activerecord.rb
|
50
|
+
- lib/grape/activerecord/activerecord.rb
|
51
|
+
- lib/grape/activerecord/extension.rb
|
52
|
+
- lib/grape/activerecord/rake.rb
|
53
|
+
- lib/grape/activerecord/rake/activerecord_4.rb
|
54
|
+
- lib/grape/activerecord/tasks.rake
|
55
|
+
- lib/grape/activerecord/version.rb
|
56
|
+
homepage: https://github.com/jhollinger/grape-activerecord
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.9.2
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.2.2
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: ActiveRecord integration for Grape
|
80
|
+
test_files: []
|