grape-activerecord 2.0.0 → 2.1.0.pre.rc1
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.
- checksums.yaml +4 -4
- data/README.md +8 -2
- data/lib/grape/activerecord/activerecord.rb +4 -0
- data/lib/grape/activerecord/compatibility_4.rb +30 -0
- data/lib/grape/activerecord/compatibility_5.rb +30 -0
- data/lib/grape/activerecord/defaults.rb +4 -3
- data/lib/grape/activerecord/rake.rb +2 -4
- data/lib/grape/activerecord/version.rb +1 -1
- data/lib/grape/activerecord.rb +2 -1
- data/lib/tasks/grape-activerecord.rake +91 -0
- metadata +18 -11
- data/lib/grape/activerecord/rake/activerecord_4.rb +0 -35
- data/lib/grape/activerecord/tasks.rake +0 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52fae3b0b15766403ba80d6a74c9bff1bac315b9
|
4
|
+
data.tar.gz: 0f6f09e48b8bf41bb6b68adc37e04879bb6b528a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05b77a6c52a9cf15497d2f2e591475c0d54d1b03260d74d68d264970fa968cf00bfa4f58d2b23984a83c6d8031f134a7a985c8c6bc51c98746a9f3bd15713f00
|
7
|
+
data.tar.gz: 5c89319bc465ba39fab414d1cae69fcf30398057eb4b4a6717953cb4805d00d5838d18cf5fbc2856885c16837aa4ecb11c81e6442877dc6548eaa66affabbac5
|
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# grape-activerecord
|
2
2
|
|
3
|
-
A simple way to use ActiveRecord with your Grape apps. The defaults are all very Railsy (`config/database.yml`, `db/seeds.rb`, `db/migrate`, etc.), but you can easily change them.
|
3
|
+
A simple way to use ActiveRecord with your Grape apps. The defaults are all very Railsy (`config/database.yml`, `db/seeds.rb`, `db/migrate`, etc.), but you can easily change them. Supports:
|
4
|
+
|
5
|
+
* ActiveRecord 4
|
6
|
+
* ActiveRecord 5 (new, possibly buggy; please submit a PR if you find something!)
|
4
7
|
|
5
8
|
## How to use
|
6
9
|
|
@@ -16,6 +19,9 @@ After loading your gems, tell `Grape::ActiveRecord` about your database config u
|
|
16
19
|
Grape::ActiveRecord.configure_from_url! ENV['DATABASE_URL'] # e.g. postgres://user:pass@host/db
|
17
20
|
Grape::ActiveRecord.configure_from_hash!(adapter: "postgresql", host: "localhost", database: "db", username: "user", password: "pass", encoding: "utf8", pool: 10, timeout: 5000)
|
18
21
|
|
22
|
+
**Important note**: `configure_from_file!` won't work as expected if you have already `DATABASE_URL` set as part of your environment variables.
|
23
|
+
This is because in ActiveRecord when that env variable is set it will merge its properties into the current connection configuration.
|
24
|
+
|
19
25
|
#### 3. Enable ActiveRecord connection management
|
20
26
|
|
21
27
|
This ActiveRecord middleware cleans up your database connections after each request. Add it to your `config.ru` file:
|
@@ -27,7 +33,7 @@ This ActiveRecord middleware cleans up your database connections after each requ
|
|
27
33
|
This will give you most of the standard `db:` tasks you get in Rails. Add it to your `Rakefile`.
|
28
34
|
|
29
35
|
require "bundler/setup"
|
30
|
-
|
36
|
+
load "tasks/grape-activerecord.rake"
|
31
37
|
|
32
38
|
namespace :db do
|
33
39
|
# Some db tasks require your app code to be loaded, or at least your gems
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
1
3
|
# Grape and ActiveRecord integration
|
2
4
|
module Grape
|
3
5
|
# Grape and ActiveRecord integration
|
@@ -11,6 +13,8 @@ module Grape
|
|
11
13
|
attr_accessor :fixtures_path
|
12
14
|
# Name of the seeds file in db_dir
|
13
15
|
attr_accessor :seed_file
|
16
|
+
# Internal compatibility layer across different major versions of AR
|
17
|
+
attr_accessor :_normalizer
|
14
18
|
end
|
15
19
|
|
16
20
|
# Connect to database with a Hash. Example:
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Grape
|
2
|
+
module ActiveRecord
|
3
|
+
# Compatibility layer for ActiveRecord 4
|
4
|
+
class Compatibility4
|
5
|
+
attr_reader :major_version
|
6
|
+
|
7
|
+
# Compatibility layer for ActiveRecord 4
|
8
|
+
def initialize
|
9
|
+
@major_version = 4
|
10
|
+
::ActiveRecord::Base.default_timezone = :utc
|
11
|
+
::ActiveRecord::Base.logger = Logger.new(STDOUT)
|
12
|
+
end
|
13
|
+
|
14
|
+
# All db migration dir paths
|
15
|
+
def migrations_paths
|
16
|
+
::ActiveRecord::Migrator.migrations_paths
|
17
|
+
end
|
18
|
+
|
19
|
+
# The dir in which to put new migrations
|
20
|
+
def migrations_path
|
21
|
+
::ActiveRecord::Migrator.migrations_path
|
22
|
+
end
|
23
|
+
|
24
|
+
# Basename of migration classes
|
25
|
+
def migration_base_class_name
|
26
|
+
'ActiveRecord::Migration'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Grape
|
2
|
+
module ActiveRecord
|
3
|
+
# Compatibility layer for ActiveRecord 5
|
4
|
+
class Compatibility5
|
5
|
+
attr_reader :major_version
|
6
|
+
|
7
|
+
# Compatibility layer for ActiveRecord 5
|
8
|
+
def initialize
|
9
|
+
@major_version = 5
|
10
|
+
::ActiveRecord::Base.default_timezone = :utc
|
11
|
+
::ActiveRecord::Base.logger = Logger.new(STDOUT)
|
12
|
+
end
|
13
|
+
|
14
|
+
# All db migration dir paths
|
15
|
+
def migrations_paths
|
16
|
+
::ActiveRecord::Migrator.migrations_paths
|
17
|
+
end
|
18
|
+
|
19
|
+
# The dir in which to put new migrations
|
20
|
+
def migrations_path
|
21
|
+
::ActiveRecord::Migrator.migrations_paths[0]
|
22
|
+
end
|
23
|
+
|
24
|
+
# Basename of migration classes
|
25
|
+
def migration_base_class_name
|
26
|
+
'ActiveRecord::Migration[5.0]'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -1,7 +1,8 @@
|
|
1
|
-
::ActiveRecord::Base.default_timezone = :utc
|
2
|
-
::ActiveRecord::Base.logger = Logger.new(STDOUT)
|
3
|
-
|
4
1
|
Grape::ActiveRecord.db_dir = 'db'
|
5
2
|
Grape::ActiveRecord.migrations_paths = %w(db/migrate)
|
6
3
|
Grape::ActiveRecord.fixtures_path = 'test/fixtures'
|
7
4
|
Grape::ActiveRecord.seed_file = 'seeds.rb'
|
5
|
+
Grape::ActiveRecord._normalizer = case ::ActiveRecord::VERSION::MAJOR
|
6
|
+
when 4 then Grape::ActiveRecord::Compatibility4.new
|
7
|
+
when 5 then Grape::ActiveRecord::Compatibility5.new
|
8
|
+
end
|
@@ -1,4 +1,2 @@
|
|
1
|
-
|
2
|
-
load "
|
3
|
-
require "grape/activerecord/rake/activerecord_#{ActiveRecord::VERSION::MAJOR}"
|
4
|
-
load "grape/activerecord/tasks.rake"
|
1
|
+
$stderr.puts "WARNING: loading 'grape/activerecord/rake' in your Rakefile is deprecated; do 'load \"tasks/grape-activerecord.rake\"' instead"
|
2
|
+
load "tasks/grape-activerecord.rake"
|
data/lib/grape/activerecord.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
require 'erb'
|
2
1
|
require 'active_record'
|
3
2
|
require 'hashie-forbidden_attributes'
|
4
3
|
require 'grape/activerecord/version'
|
5
4
|
require 'grape/activerecord/activerecord'
|
5
|
+
require 'grape/activerecord/compatibility_4'
|
6
|
+
require 'grape/activerecord/compatibility_5'
|
6
7
|
require 'grape/activerecord/defaults'
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'active_support/core_ext/string/strip'
|
4
|
+
require 'active_support/core_ext/string/inflections'
|
5
|
+
require 'grape/activerecord'
|
6
|
+
load 'active_record/railties/databases.rake'
|
7
|
+
|
8
|
+
#
|
9
|
+
# Configure and override the default activerecord db rake tasks
|
10
|
+
#
|
11
|
+
|
12
|
+
Rake::Task.define_task('db:_load_config') do
|
13
|
+
::ActiveRecord::Base.logger = nil
|
14
|
+
::ActiveRecord::Tasks::DatabaseTasks.tap do |config|
|
15
|
+
config.root = Rake.application.original_dir
|
16
|
+
config.env = Grape::ActiveRecord.rack_env.to_s
|
17
|
+
config.db_dir = Grape::ActiveRecord.db_dir
|
18
|
+
config.migrations_paths = Array(Grape::ActiveRecord.migrations_paths)
|
19
|
+
config.fixtures_path = Grape::ActiveRecord.fixtures_path
|
20
|
+
config.database_configuration = ::ActiveRecord::Base.configurations
|
21
|
+
config.seed_loader = Object.new
|
22
|
+
config.seed_loader.instance_eval do
|
23
|
+
def load_seed
|
24
|
+
load "#{Grape::ActiveRecord.db_dir}/#{Grape::ActiveRecord.seed_file}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
Rake::Task['db:load_config'].clear
|
31
|
+
Rake::Task.define_task('db:load_config') do
|
32
|
+
# Run the user's db:environment task first, so they have an opportunity to set a custom db config location
|
33
|
+
Rake::Task['db:environment'].invoke
|
34
|
+
end
|
35
|
+
|
36
|
+
Rake::Task.define_task('db:environment') do
|
37
|
+
# defined by user
|
38
|
+
end
|
39
|
+
|
40
|
+
# Load db config at the end of user-defined db:environment
|
41
|
+
Rake::Task['db:environment'].enhance do
|
42
|
+
Rake::Task['db:_load_config'].invoke
|
43
|
+
end
|
44
|
+
|
45
|
+
Rake::Task['db:test:deprecated'].clear if Rake::Task.task_defined?('db:test:deprecated')
|
46
|
+
|
47
|
+
#
|
48
|
+
# Define grape-activerecord helper tasks
|
49
|
+
#
|
50
|
+
|
51
|
+
namespace :db do
|
52
|
+
namespace :test do
|
53
|
+
task :environment do
|
54
|
+
ENV['RACK_ENV'] = 'test'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
desc "Create a migration (parameters: NAME, VERSION)"
|
59
|
+
task :create_migration do
|
60
|
+
unless ENV["NAME"]
|
61
|
+
puts "No NAME specified. Example usage: `rake db:create_migration NAME=create_widgets`"
|
62
|
+
exit 1
|
63
|
+
end
|
64
|
+
|
65
|
+
name = ENV["NAME"]
|
66
|
+
version = ENV["VERSION"] || Time.now.utc.strftime("%Y%m%d%H%M%S")
|
67
|
+
|
68
|
+
Grape::ActiveRecord._normalizer.migrations_paths.each do |directory|
|
69
|
+
next unless File.exists?(directory)
|
70
|
+
migration_files = Pathname(directory).children
|
71
|
+
if duplicate = migration_files.find { |path| path.basename.to_s.include?(name) }
|
72
|
+
puts "Another migration is already named \"#{name}\": #{duplicate}."
|
73
|
+
exit 1
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
filename = "#{version}_#{name}.rb"
|
78
|
+
dirname = Grape::ActiveRecord._normalizer.migrations_path
|
79
|
+
path = File.join(dirname, filename)
|
80
|
+
|
81
|
+
FileUtils.mkdir_p(dirname)
|
82
|
+
File.write path, <<-MIGRATION.strip_heredoc
|
83
|
+
class #{name.camelize} < #{Grape::ActiveRecord._normalizer.migration_base_class_name}
|
84
|
+
def change
|
85
|
+
end
|
86
|
+
end
|
87
|
+
MIGRATION
|
88
|
+
|
89
|
+
puts path
|
90
|
+
end
|
91
|
+
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: 2.0.
|
4
|
+
version: 2.1.0.pre.rc1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jordan Hollinger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|
@@ -28,30 +28,36 @@ dependencies:
|
|
28
28
|
name: activerecord
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '4.0'
|
34
|
+
- - "<="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '5.0'
|
34
37
|
type: :runtime
|
35
38
|
prerelease: false
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
38
|
-
- - "
|
41
|
+
- - ">="
|
39
42
|
- !ruby/object:Gem::Version
|
40
43
|
version: '4.0'
|
44
|
+
- - "<="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '5.0'
|
41
47
|
- !ruby/object:Gem::Dependency
|
42
48
|
name: hashie-forbidden_attributes
|
43
49
|
requirement: !ruby/object:Gem::Requirement
|
44
50
|
requirements:
|
45
51
|
- - "~>"
|
46
52
|
- !ruby/object:Gem::Version
|
47
|
-
version: 0.1
|
53
|
+
version: '0.1'
|
48
54
|
type: :runtime
|
49
55
|
prerelease: false
|
50
56
|
version_requirements: !ruby/object:Gem::Requirement
|
51
57
|
requirements:
|
52
58
|
- - "~>"
|
53
59
|
- !ruby/object:Gem::Version
|
54
|
-
version: 0.1
|
60
|
+
version: '0.1'
|
55
61
|
description: Extends Grape with simple ActiveRecord integration
|
56
62
|
email: jordan.hollinger@gmail.com
|
57
63
|
executables: []
|
@@ -62,11 +68,12 @@ files:
|
|
62
68
|
- README.md
|
63
69
|
- lib/grape/activerecord.rb
|
64
70
|
- lib/grape/activerecord/activerecord.rb
|
71
|
+
- lib/grape/activerecord/compatibility_4.rb
|
72
|
+
- lib/grape/activerecord/compatibility_5.rb
|
65
73
|
- lib/grape/activerecord/defaults.rb
|
66
74
|
- lib/grape/activerecord/rake.rb
|
67
|
-
- lib/grape/activerecord/rake/activerecord_4.rb
|
68
|
-
- lib/grape/activerecord/tasks.rake
|
69
75
|
- lib/grape/activerecord/version.rb
|
76
|
+
- lib/tasks/grape-activerecord.rake
|
70
77
|
homepage: https://github.com/jhollinger/grape-activerecord
|
71
78
|
licenses:
|
72
79
|
- MIT
|
@@ -79,12 +86,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
86
|
requirements:
|
80
87
|
- - ">="
|
81
88
|
- !ruby/object:Gem::Version
|
82
|
-
version: 1.
|
89
|
+
version: 2.1.0
|
83
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
91
|
requirements:
|
85
|
-
- - "
|
92
|
+
- - ">"
|
86
93
|
- !ruby/object:Gem::Version
|
87
|
-
version:
|
94
|
+
version: 1.3.1
|
88
95
|
requirements: []
|
89
96
|
rubyforge_project:
|
90
97
|
rubygems_version: 2.4.5.1
|
@@ -1,35 +0,0 @@
|
|
1
|
-
Rake::Task.define_task('db:_load_config') do
|
2
|
-
ActiveRecord::Base.logger = nil
|
3
|
-
|
4
|
-
ActiveRecord::Tasks::DatabaseTasks.tap do |config|
|
5
|
-
config.root = Rake.application.original_dir
|
6
|
-
config.env = Grape::ActiveRecord.rack_env.to_s
|
7
|
-
config.db_dir = Grape::ActiveRecord.db_dir
|
8
|
-
config.migrations_paths = Array(Grape::ActiveRecord.migrations_paths)
|
9
|
-
config.fixtures_path = Grape::ActiveRecord.fixtures_path
|
10
|
-
config.database_configuration = ActiveRecord::Base.configurations
|
11
|
-
config.seed_loader = Object.new
|
12
|
-
config.seed_loader.instance_eval do
|
13
|
-
def load_seed
|
14
|
-
load "#{ActiveRecord::Tasks::DatabaseTasks.db_dir}/#{Grape::ActiveRecord.seed_file}"
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
Rake::Task['db:load_config'].clear
|
21
|
-
Rake::Task.define_task('db:load_config') do
|
22
|
-
# Run the user's db:environment task first, so they have an opportunity to set a custom db config location
|
23
|
-
Rake::Task['db:environment'].invoke
|
24
|
-
end
|
25
|
-
|
26
|
-
Rake::Task.define_task('db:environment') do
|
27
|
-
# defined by user
|
28
|
-
end
|
29
|
-
|
30
|
-
# Load db config at the end of user-defined db:environment
|
31
|
-
Rake::Task['db:environment'].enhance do
|
32
|
-
Rake::Task['db:_load_config'].invoke
|
33
|
-
end
|
34
|
-
|
35
|
-
Rake::Task['db:test:deprecated'].clear if Rake::Task.task_defined?('db:test:deprecated')
|
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'pathname'
|
2
|
-
require 'fileutils'
|
3
|
-
require 'active_support/core_ext/string/strip'
|
4
|
-
require 'active_support/core_ext/string/inflections'
|
5
|
-
|
6
|
-
namespace :db do
|
7
|
-
namespace :test do
|
8
|
-
task :environment do
|
9
|
-
ENV['RACK_ENV'] = 'test'
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
desc "Create a migration (parameters: NAME, VERSION)"
|
14
|
-
task :create_migration do
|
15
|
-
unless ENV["NAME"]
|
16
|
-
puts "No NAME specified. Example usage: `rake db:create_migration NAME=create_widgets`"
|
17
|
-
exit 1
|
18
|
-
end
|
19
|
-
|
20
|
-
name = ENV["NAME"]
|
21
|
-
version = ENV["VERSION"] || Time.now.utc.strftime("%Y%m%d%H%M%S")
|
22
|
-
|
23
|
-
ActiveRecord::Migrator.migrations_paths.each do |directory|
|
24
|
-
next unless File.exists?(directory)
|
25
|
-
migration_files = Pathname(directory).children
|
26
|
-
if duplicate = migration_files.find { |path| path.basename.to_s.include?(name) }
|
27
|
-
puts "Another migration is already named \"#{name}\": #{duplicate}."
|
28
|
-
exit 1
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
filename = "#{version}_#{name}.rb"
|
33
|
-
dirname = ActiveRecord::Migrator.migrations_path
|
34
|
-
path = File.join(dirname, filename)
|
35
|
-
|
36
|
-
FileUtils.mkdir_p(dirname)
|
37
|
-
File.write path, <<-MIGRATION.strip_heredoc
|
38
|
-
class #{name.camelize} < ActiveRecord::Migration
|
39
|
-
def change
|
40
|
-
end
|
41
|
-
end
|
42
|
-
MIGRATION
|
43
|
-
|
44
|
-
puts path
|
45
|
-
end
|
46
|
-
end
|