otr-activerecord 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 58dbba472854748f534103046ed89afd5f6920f8
4
+ data.tar.gz: eace235fb0201c8c3ba63a59cad9b4230fe3e707
5
+ SHA512:
6
+ metadata.gz: 2302a10494dfa0f20481b80318249aac5db711d7998390b15d48682ddf77d92173804f44ca0bae35e3ad576b886ae80f2bd65fa449c00272e954dabb5586a72f
7
+ data.tar.gz: 42f5730e6b715517336ea15a337138293c1beadab4ea710be0f6005b5b29234f3e9ac2e17bdaba5e4efaeccbf2b18c6aada9a2882d5fdb49e593adcb355546d1
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2016 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.md ADDED
@@ -0,0 +1,66 @@
1
+ # otr-activerecord
2
+
3
+ An easy way to use ActiveRecord "off the rails." Works with Grape, Sinatra, plain old Rack, or even in a boring little script!. The defaults are all very Railsy (`config/database.yml`, `db/seeds.rb`, `db/migrate`, etc.), but you can easily change them. (Formerly known as `grape-activerecord`.) Supports:
4
+
5
+ * ActiveRecord 4
6
+ * ActiveRecord 5 (new, possibly buggy; PRs and Issues welcome!)
7
+
8
+ ## How to use
9
+
10
+ #### 1. Add it to your Gemfile
11
+
12
+ gem "otr-activerecord"
13
+
14
+ #### 2. Configure your database connection
15
+
16
+ After loading your gems, tell `OTR::ActiveRecord` about your database config using one of the following examples:
17
+
18
+ OTR::ActiveRecord.configure_from_file! "config/database.yml"
19
+ OTR::ActiveRecord.configure_from_url! ENV['DATABASE_URL'] # e.g. postgres://user:pass@host/db
20
+ OTR::ActiveRecord.configure_from_hash!(adapter: "postgresql", host: "localhost", database: "db", username: "user", password: "pass", encoding: "utf8", pool: 10, timeout: 5000)
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
+
25
+ #### 3. Enable ActiveRecord connection management in Rack apps
26
+
27
+ This ActiveRecord middleware cleans up your database connections after each request. Add it to your `config.ru` file:
28
+
29
+ use ActiveRecord::ConnectionAdapters::ConnectionManagement
30
+
31
+ #### 4. Import ActiveRecord tasks into your Rakefile
32
+
33
+ This will give you most of the standard `db:` tasks you get in Rails. Add it to your `Rakefile`.
34
+
35
+ require "bundler/setup"
36
+ load "tasks/otr-activerecord.rake"
37
+
38
+ namespace :db do
39
+ # Some db tasks require your app code to be loaded; they'll expect to find it here
40
+ task :environment do
41
+ require_relative "app"
42
+ end
43
+ end
44
+
45
+ Unlike in Rails, creating a new migration is also a rake task. Run `bundle exec rake -T` to get a full list of tasks.
46
+
47
+ bundle exec rake db:create_migration[create_widgets]
48
+
49
+ ## Examples
50
+
51
+ Look under [/examples](https://github.com/jhollinger/otr-activerecord/tree/master/examples) for some example apps.
52
+
53
+ ## Advanced options
54
+
55
+ The defaults for db-related files like migrations, seeds, and fixtures are the same as Rails. If you want to override them, use the following options in your `Rakefile`:
56
+
57
+ OTR::ActiveRecord.db_dir = 'db'
58
+ OTR::ActiveRecord.migrations_paths = ['db/migrate']
59
+ OTR::ActiveRecord.fixtures_path = 'test/fixtures'
60
+ OTR::ActiveRecord.seed_file = 'seeds.rb'
61
+
62
+ ## License
63
+
64
+ Licensed under the MIT License
65
+
66
+ Copyright 2016 Jordan Hollinger
@@ -0,0 +1,44 @@
1
+ require 'erb'
2
+
3
+ # "Off the Rails" ActiveRecord configuration/integration for Grape, Sinatra, Rack, and any other kind of app
4
+ module OTR
5
+ # ActiveRecord configuration module
6
+ module ActiveRecord
7
+ class << self
8
+ # Relative path to the "db" dir
9
+ attr_accessor :db_dir
10
+ # Relative path(s) to the migrations directory
11
+ attr_accessor :migrations_paths
12
+ # Relative path to the fixtures directory
13
+ attr_accessor :fixtures_path
14
+ # Name of the seeds file in db_dir
15
+ attr_accessor :seed_file
16
+ # Internal compatibility layer across different major versions of AR
17
+ attr_accessor :_normalizer
18
+ end
19
+
20
+ # Connect to database with a Hash. Example:
21
+ # {adapter: 'postgresql', host: 'localhost', database: 'db', username: 'user', password: 'pass', encoding: 'utf8', pool: 10, timeout: 5000}
22
+ def self.configure_from_hash!(spec)
23
+ ::ActiveRecord::Base.configurations = {rack_env => spec}.stringify_keys
24
+ ::ActiveRecord::Base.establish_connection(rack_env)
25
+ end
26
+
27
+ # Connect to database with a DB URL. Example: "postgres://user:pass@localhost/db"
28
+ def self.configure_from_url!(url)
29
+ configure_from_hash! ::ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(url).to_hash
30
+ end
31
+
32
+ # Connect to database with a yml file. Example: "config/database.yml"
33
+ def self.configure_from_file!(path)
34
+ raise "#{path} does not exist!" unless File.file? path
35
+ ::ActiveRecord::Base.configurations = YAML.load(ERB.new(File.read(path)).result) || {}
36
+ ::ActiveRecord::Base.establish_connection(rack_env)
37
+ end
38
+
39
+ # The current Rack environment
40
+ def self.rack_env
41
+ (ENV['RACK_ENV'] || ENV['RAILS_ENV'] || ENV['OTR_ENV'] || 'development').to_sym
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,30 @@
1
+ module OTR
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
+ OTR::ActiveRecord.migrations_paths
17
+ end
18
+
19
+ # The dir in which to put new migrations
20
+ def migrations_path
21
+ OTR::ActiveRecord.migrations_paths[0]
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 OTR
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
+ OTR::ActiveRecord.migrations_paths
17
+ end
18
+
19
+ # The dir in which to put new migrations
20
+ def migrations_path
21
+ OTR::ActiveRecord.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
@@ -0,0 +1,8 @@
1
+ OTR::ActiveRecord.db_dir = 'db'
2
+ OTR::ActiveRecord.migrations_paths = %w(db/migrate)
3
+ OTR::ActiveRecord.fixtures_path = 'test/fixtures'
4
+ OTR::ActiveRecord.seed_file = 'seeds.rb'
5
+ OTR::ActiveRecord._normalizer = case ::ActiveRecord::VERSION::MAJOR
6
+ when 4 then OTR::ActiveRecord::Compatibility4.new
7
+ when 5 then OTR::ActiveRecord::Compatibility5.new
8
+ end
@@ -0,0 +1,6 @@
1
+ module OTR
2
+ module ActiveRecord
3
+ # Gem version
4
+ VERSION = '1.0.0'
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_record'
2
+ require 'hashie-forbidden_attributes'
3
+ require 'otr-activerecord/version'
4
+ require 'otr-activerecord/activerecord'
5
+ require 'otr-activerecord/compatibility_4'
6
+ require 'otr-activerecord/compatibility_5'
7
+ require 'otr-activerecord/defaults'
@@ -0,0 +1,84 @@
1
+ require 'pathname'
2
+ require 'fileutils'
3
+ require 'active_support/core_ext/string/strip'
4
+ require 'active_support/core_ext/string/inflections'
5
+ require 'otr-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 = OTR::ActiveRecord.rack_env.to_s
17
+ config.db_dir = OTR::ActiveRecord.db_dir
18
+ config.migrations_paths = Array(OTR::ActiveRecord.migrations_paths)
19
+ config.fixtures_path = OTR::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 "#{OTR::ActiveRecord.db_dir}/#{OTR::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 otr-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"
59
+ task :create_migration, [:name] do |_, args|
60
+ name, version = args[:name], Time.now.utc.strftime("%Y%m%d%H%M%S")
61
+
62
+ OTR::ActiveRecord._normalizer.migrations_paths.each do |directory|
63
+ next unless File.exists?(directory)
64
+ migration_files = Pathname(directory).children
65
+ if duplicate = migration_files.find { |path| path.basename.to_s.include?(name) }
66
+ abort "Another migration is already named \"#{name}\": #{duplicate}."
67
+ end
68
+ end
69
+
70
+ filename = "#{version}_#{name}.rb"
71
+ dirname = OTR::ActiveRecord._normalizer.migrations_path
72
+ path = File.join(dirname, filename)
73
+
74
+ FileUtils.mkdir_p(dirname)
75
+ File.write path, <<-MIGRATION.strip_heredoc
76
+ class #{name.camelize} < #{OTR::ActiveRecord._normalizer.migration_base_class_name}
77
+ def change
78
+ end
79
+ end
80
+ MIGRATION
81
+
82
+ puts path
83
+ end
84
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: otr-activerecord
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jordan Hollinger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ - - "<="
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.0'
30
+ - - "<="
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: hashie-forbidden_attributes
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.1'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.1'
47
+ description: 'Off The Rails ActiveRecord: Use ActiveRecord with Grape, Sinatra, Rack,
48
+ or anything else! Formerly known as ''grape-activerecord''.'
49
+ email: jordan.hollinger@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - LICENSE
55
+ - README.md
56
+ - lib/otr-activerecord.rb
57
+ - lib/otr-activerecord/activerecord.rb
58
+ - lib/otr-activerecord/compatibility_4.rb
59
+ - lib/otr-activerecord/compatibility_5.rb
60
+ - lib/otr-activerecord/defaults.rb
61
+ - lib/otr-activerecord/version.rb
62
+ - lib/tasks/otr-activerecord.rake
63
+ homepage: https://github.com/jhollinger/otr-activerecord
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.1.0
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.4.5.1
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: 'Off The Rails: Use ActiveRecord with Grape, Sinatra, Rack, or anything else!'
87
+ test_files: []
88
+ has_rdoc: