grape-activerecord 0.0.8 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d2c6900e3c4121e02d0f6e4396c121b436de4130
4
- data.tar.gz: be25edd2db43eededd72b925c5aa8ea4655a57f7
3
+ metadata.gz: e2c64bed54671c981f10740dd8c9aff825139ecd
4
+ data.tar.gz: 4767e1ec312989eca3fdf8402581953c34293ecb
5
5
  SHA512:
6
- metadata.gz: c70989c0e2e7fc2be5113a9a6851e46af63d9f8dfb725b0a7e7ecf643532525b3ec7e6c416d6bf16a408d0a0f89b299c1d3ce8e4e3f24ddeac7cbaca6cdfd470
7
- data.tar.gz: 861261075ed7ef9d3b417fb0678b36a9460df1a2c4f030ec579291e4f1685ac36939959a78a2937bcab6d13f1c2bd0d29f417b60326922a21562bdb6071e52cf
6
+ metadata.gz: 6f4408ff1a8c8a2509b8e3e9a4118be9c456d2b5b3ce7b2e625a25970054ad6218332b6e29a6c8223caeb41d7262cf982b06290460941e8384b21e502efe4782
7
+ data.tar.gz: 5c749482c3f5a820ae9f93b5f9cd614293bbe3ef3b9a8a052e6f12fd362b039b78f1d29eead8df8c25f84a50fb1f13ea35e72fc3ba2cec984e8854bcb773818d
@@ -0,0 +1,56 @@
1
+ # grape-activerecord
2
+
3
+ A simple way to use ActiveRecord with your Grape apps.
4
+
5
+ ## How to use
6
+
7
+ ### 1. Add it to your Gemfile
8
+
9
+ gem "grape-activerecord"
10
+
11
+ ### 2. Configure your database connection
12
+
13
+ grape-activerecord looks for your database configuration in:
14
+
15
+ * `config/database.yml` (see /examples for a sample file)
16
+ * The `DATABASE_URL` environment variable (e.g. `postgres://user:pass@host/db`)
17
+
18
+ But if your app has more particular needs, we've got you covered:
19
+
20
+ Grape::ActiveRecord.database_file = "db.yml"
21
+ Grape::ActiveRecord.database_url = "postgres://user:pass@host/db"
22
+ Grape::ActiveRecord.database = {adapter: "postgresql", host: "localhost", database: "db", username: "user", password: "pass", encoding: "utf8", pool: 10, timeout: 5000}
23
+
24
+ ### 3. Enable ActiveRecord connection management
25
+
26
+ This ActiveRecord middleware cleans up your database connections after each request. Add it to your config.ru file:
27
+
28
+ use ActiveRecord::ConnectionAdapters::ConnectionManagement
29
+
30
+ ### 4. Import ActiveRecord tasks into your Rakefile
31
+
32
+ This will give you most of the standard `db:` tasks you get in Rails. Run `bundle exec rake -T` to get a full list.
33
+
34
+ require "bundler/setup"
35
+ require "grape/activerecord/rake"
36
+
37
+ namespace :db do
38
+ # Some db tasks require your app code to be loaded
39
+ task :environment do
40
+ require_relative "app"
41
+ end
42
+ end
43
+
44
+ Unlike in Rails, creating a new migration is also a rake task:
45
+
46
+ bundle exec rake db:create_migration NAME=create_widgets
47
+
48
+ ## Examples
49
+
50
+ Look under /examples for some example apps.
51
+
52
+ ## License
53
+
54
+ Licensed under the MIT License
55
+
56
+ Copyright 2014 Jordan Hollinger
@@ -1,5 +1,6 @@
1
1
  require 'erb'
2
+ require 'active_record'
2
3
  require 'hashie-forbidden_attributes'
3
4
  require 'grape/activerecord/version'
4
5
  require 'grape/activerecord/activerecord'
5
- require 'grape/activerecord/extension'
6
+ require 'grape/activerecord/defaults'
@@ -2,41 +2,27 @@
2
2
  module Grape
3
3
  # Grape and ActiveRecord integration
4
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
5
  # The current Rack environment
17
6
  RACK_ENV = (ENV['RACK_ENV'] || 'development').to_sym
18
7
 
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}
8
+ # Connect to database with a Hash. Example:
9
+ # {adapter: 'postgresql', host: 'localhost', database: 'db', username: 'user', password: 'pass', encoding: 'utf8', pool: 10, timeout: 5000}
10
+ def self.database=(spec)
11
+ ::ActiveRecord::Base.configurations = {RACK_ENV => spec}.stringify_keys
12
+ ::ActiveRecord::Base.establish_connection(RACK_ENV)
13
+ end
31
14
 
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)
15
+ # Connect to database with a DB URL. Example: "postgres://user:pass@localhost/db"
16
+ def self.database_url=(url)
17
+ ::ActiveRecord::Base.establish_connection(url)
18
+ ::ActiveRecord::Base.configurations = {RACK_ENV.to_s => ::ActiveRecord::Base.connection.pool.spec.config}
19
+ end
36
20
 
37
- else
38
- raise RuntimeError, 'You must configure a database connection!'
39
- end
21
+ # Connect to database with a yml file. Example: "config/database.yml"
22
+ def self.database_file=(path)
23
+ raise "#{path} does not exist!" unless File.file? path
24
+ ::ActiveRecord::Base.configurations = YAML.load(ERB.new(File.read(path)).result) || {}
25
+ ::ActiveRecord::Base.establish_connection(RACK_ENV)
40
26
  end
41
27
  end
42
28
  end
@@ -0,0 +1,8 @@
1
+ ::ActiveRecord::Base.default_timezone = :utc
2
+ ::ActiveRecord::Base.logger = Logger.new(STDOUT)
3
+
4
+ if ENV['DATABASE_URL']
5
+ Grape::ActiveRecord.database_url = ENV['DATABASE_URL']
6
+ elsif File.file? 'config/database.yml'
7
+ Grape::ActiveRecord.database_file = 'config/database.yml'
8
+ end
@@ -1,7 +1,6 @@
1
1
  Rake::Task['db:load_config'].clear
2
2
  Rake::Task.define_task('db:load_config') do
3
3
  require 'grape/activerecord'
4
- Grape::ActiveRecord.setup!
5
4
  ActiveRecord::Base.logger = nil
6
5
 
7
6
  ActiveRecord::Tasks::DatabaseTasks.tap do |config|
@@ -1,6 +1,6 @@
1
1
  module Grape
2
2
  module ActiveRecord
3
3
  # Gem version
4
- VERSION = '0.0.8'
4
+ VERSION = '1.0.0'
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Hollinger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-11 00:00:00.000000000 Z
11
+ date: 2015-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
@@ -59,10 +59,10 @@ extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
61
  - LICENSE
62
- - README.rdoc
62
+ - README.md
63
63
  - lib/grape/activerecord.rb
64
64
  - lib/grape/activerecord/activerecord.rb
65
- - lib/grape/activerecord/extension.rb
65
+ - lib/grape/activerecord/defaults.rb
66
66
  - lib/grape/activerecord/rake.rb
67
67
  - lib/grape/activerecord/rake/activerecord_4.rb
68
68
  - lib/grape/activerecord/tasks.rake
@@ -87,9 +87,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  version: '0'
88
88
  requirements: []
89
89
  rubyforge_project:
90
- rubygems_version: 2.2.2
90
+ rubygems_version: 2.4.5
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: ActiveRecord integration for Grape
94
94
  test_files: []
95
- has_rdoc:
@@ -1,60 +0,0 @@
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 (usually) 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
- ==== Important
34
-
35
- It's recommended that you manually include ActiveRecord::ConnectionAdapters::ConnectionManagement yourself BEFORE mounting your Grape app (https://github.com/intridea/grape/issues/517#issuecomment-60969722). The top of your config.ru file is a good place:
36
-
37
- # config.ru
38
- use ActiveRecord::ConnectionAdapters::ConnectionManagement
39
- run MyGrapeApi
40
-
41
- === Example Rakefile with ActiveRecord tasks
42
-
43
- require 'bundler/setup'
44
- require 'grape/activerecord/rake'
45
-
46
- namespace :db do
47
- # Some db tasks require your app code to be loaded
48
- task :environment do
49
- require_relative 'app'
50
- end
51
- end
52
-
53
- This will give you most of the standard "db:" tasks you get in Rails. Run "bundle exec rake -T" to get a full list.
54
- Note that unlike in Rails, creating a new migration is also a rake task:
55
-
56
- bundle exec rake db:create_migration NAME=create_widgets
57
-
58
- === Example app
59
-
60
- Look under /examples for some example apps.
@@ -1,12 +0,0 @@
1
- module Grape
2
- module ActiveRecord
3
- # Including this module in your Grape app establishes the database connection and handles connection management
4
- module Extension
5
- # Establishes db connection and management
6
- def self.included(app)
7
- Grape::ActiveRecord.setup!
8
- app.use ::ActiveRecord::ConnectionAdapters::ConnectionManagement
9
- end
10
- end
11
- end
12
- end