pax_global_header00006660000000000000000000000064141755541600014522gustar00rootroot0000000000000052 comment=53457369db933fc3b46e1029590dc75e3336600b otr-activerecord-2.1.1/000077500000000000000000000000001417555416000147775ustar00rootroot00000000000000otr-activerecord-2.1.1/.gitignore000066400000000000000000000000661417555416000167710ustar00rootroot00000000000000/*.gem /Gemfile.lock /examples/**/*.sqlite3 .DS_Store otr-activerecord-2.1.1/.rspec000066400000000000000000000000261417555416000161120ustar00rootroot00000000000000--require spec_helper otr-activerecord-2.1.1/CHANGELOG.md000066400000000000000000000033731417555416000166160ustar00rootroot00000000000000### 2.1.0 (2022-01-30) * Don't set logger - allows apps to do that - [PR #28](https://github.com/jhollinger/otr-activerecord/pull/28) - [anakinj](https://github.com/anakinj) ### 2.0.4 (2022-01-20) * Fix YAML loading on Ruby 3.1 - [PR #39](https://github.com/jhollinger/otr-activerecord/pull/39) - [scudelletti](https://github.com/scudelletti) ### 2.0.3 (2021-10-22) * Preliminary support for ActiveRecord 7 (tested against 7.0.0.alpha2) ### 2.0.2 (2021-09-14) * Fix `configure_from_file!` for flat files (Brynbayliss87) ### 2.0.1 (2021-06-24) * Fix configure_from_url! for AR 6.1 ### 2.0.0 (2021-05-06) * Parse three-tier database.yml * Require manually calling OTR::ActiveRecord::establish_connection! ### 1.4.2 (2021-04-03) * Allow AR 6.1 ### 1.4.0 (2019-06-05) * Add `OTR::ActiveRecord::QueryCache` middleware. ### 1.3.0 * Active Record 6.0 support ### 1.2.7 (2019-01-11) * A less hacky way of fixing the bug fixed in 1.2.6. `ENV["RACK_ENV"]`/`ENV["RAILS_ENV"]` will no longer be forced into `development` when blank. ### 1.2.6 (2018-12-11) * Bugfix to default env in development mode w/AR 5.2 ### 1.2.5 (2018-04-23) * Support ActiveRecord 5.2 ### 1.2.4 (2017-08-23) * Bugfix to db:create_migration ### 1.2.3 (2017-08-17) * Bugfix to OTR::ActiveRecord.configure_from_hash! for ActiveRecord 5.1 * Look for APP_ENV if RAILS_ENV and RACK_ENV aren't set ### 1.2.2 (2017-08-17) * Support ActiveRecord 5.1 ### 1.2.1 (2017-02-11) * Bugfix to db:test: Rake tasks for Rails 5 ### 1.2.0 (2016-08-24) * Bugfix to AR 5.0.X version restriction - [PR #2](https://github.com/jhollinger/otr-activerecord/pull/2) - [vidok](https://github.com/vidok) ### 1.0.0-rc1 (2016-07-31) * Port from grape-activerecord * Tweak `db:create_migration` to take a real rake arg rather than an env var otr-activerecord-2.1.1/Gemfile000066400000000000000000000001361417555416000162720ustar00rootroot00000000000000 source 'https://rubygems.org' gemspec gem 'activerecord', '>6.0' gem 'rspec' gem 'sqlite3' otr-activerecord-2.1.1/LICENSE000066400000000000000000000020441417555416000160040ustar00rootroot00000000000000Copyright (c) 2016 Jordan Hollinger Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. otr-activerecord-2.1.1/README.md000066400000000000000000000060331417555416000162600ustar00rootroot00000000000000# otr-activerecord 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: * ActiveRecord 7.0.0.alpha2 (new AR features, like encryption, not tested) * ActiveRecord 6 * ActiveRecord 5 * ActiveRecord 4 ## How to use #### 1. Add it to your Gemfile gem "otr-activerecord" #### 2. Configure your database connection After loading your gems, tell `OTR::ActiveRecord` about your database config using one of the following examples: OTR::ActiveRecord.configure_from_file! "config/database.yml" OTR::ActiveRecord.configure_from_url! ENV['DATABASE_URL'] # e.g. postgres://user:pass@host/db OTR::ActiveRecord.configure_from_hash!(adapter: "postgresql", host: "localhost", database: "db", username: "user", password: "pass", encoding: "utf8", pool: 10, timeout: 5000) **Important note**: `configure_from_file!` won't work as expected if you have already `DATABASE_URL` set as part of your environment variables. This is because in ActiveRecord when that env variable is set it will merge its properties into the current connection configuration. #### 3. Connect to your database(s) If you have a single database (most apps), use this helper: OTR::ActiveRecord.establish_connection! If you're using multiple databases, call your base class(es) instead: MyBase.establish_connection :primary MyBase.establish_connection :primary_replica ... #### 4. Enable middleware for Rack apps Add these middlewares in `config.ru`: # Clean up database connections after every request (required) use OTR::ActiveRecord::ConnectionManagement # Enable ActiveRecord's QueryCache for every request (optional) use OTR::ActiveRecord::QueryCache #### 5. Import ActiveRecord tasks into your Rakefile This will give you most of the standard `db:` tasks you get in Rails. Add it to your `Rakefile`. require "bundler/setup" load "tasks/otr-activerecord.rake" namespace :db do # Some db tasks require your app code to be loaded; they'll expect to find it here task :environment do require_relative "app" end end Unlike in Rails, creating a new migration is also a rake task. Run `bundle exec rake -T` to get a full list of tasks. bundle exec rake db:create_migration[create_widgets] ## Examples Look under [/examples](https://github.com/jhollinger/otr-activerecord/tree/master/examples) for some example apps. ## Advanced options 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`: OTR::ActiveRecord.db_dir = 'db' OTR::ActiveRecord.migrations_paths = ['db/migrate'] OTR::ActiveRecord.fixtures_path = 'test/fixtures' OTR::ActiveRecord.seed_file = 'seeds.rb' ## License Licensed under the MIT License Copyright 2016 Jordan Hollinger otr-activerecord-2.1.1/Rakefile000066400000000000000000000000631417555416000164430ustar00rootroot00000000000000require 'bundler' Bundler::GemHelper.install_tasks otr-activerecord-2.1.1/examples/000077500000000000000000000000001417555416000166155ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/000077500000000000000000000000001417555416000224475ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/.gitignore000066400000000000000000000000271417555416000244360ustar00rootroot00000000000000/.bundle /db/*.sqlite3 otr-activerecord-2.1.1/examples/grape-activerecord4/Gemfile000066400000000000000000000005421417555416000237430ustar00rootroot00000000000000source 'https://rubygems.org' gem 'rack', '~> 1.6.0' gem 'grape', '~> 1.2.2' gem 'grape-entity', '~> 0.5.1' gem 'activerecord', '~> 4.2.7', require: 'active_record' gem 'otr-activerecord', '~> 1.1.0' gem 'sqlite3' gem 'rake' group :development do gem 'shotgun' end group :test do gem 'rack-test', require: 'rack/test' gem 'database_cleaner' end otr-activerecord-2.1.1/examples/grape-activerecord4/Gemfile.lock000066400000000000000000000036421417555416000246760ustar00rootroot00000000000000GEM remote: https://rubygems.org/ specs: activemodel (4.2.7) activesupport (= 4.2.7) builder (~> 3.1) activerecord (4.2.7) activemodel (= 4.2.7) activesupport (= 4.2.7) arel (~> 6.0) activesupport (4.2.7) i18n (~> 0.7) json (~> 1.7, >= 1.7.7) minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) arel (6.0.3) axiom-types (0.1.1) descendants_tracker (~> 0.0.4) ice_nine (~> 0.11.0) thread_safe (~> 0.3, >= 0.3.1) builder (3.2.2) coercible (1.0.0) descendants_tracker (~> 0.0.1) database_cleaner (1.5.1) descendants_tracker (0.0.4) thread_safe (~> 0.3, >= 0.3.1) equalizer (0.0.11) grape (1.2.2) activesupport builder mustermann-grape (~> 1.0.0) rack (>= 1.3.0) rack-accept virtus (>= 1.0.0) grape-entity (0.5.1) activesupport multi_json (>= 1.3.2) hashie (3.4.4) hashie-forbidden_attributes (0.1.1) hashie (>= 3.0) i18n (0.7.0) ice_nine (0.11.2) json (1.8.6) minitest (5.9.0) multi_json (1.12.1) mustermann (1.0.3) mustermann-grape (1.0.0) mustermann (~> 1.0.0) otr-activerecord (1.1.0) activerecord (>= 4.0, <= 5.0) hashie-forbidden_attributes (~> 0.1) rack (1.6.4) rack-accept (0.4.5) rack (>= 0.4) rack-test (0.6.3) rack (>= 1.0) rake (10.5.0) shotgun (0.9.1) rack (>= 1.0) sqlite3 (1.3.11) thread_safe (0.3.5) tzinfo (1.2.2) thread_safe (~> 0.1) virtus (1.0.5) axiom-types (~> 0.1) coercible (~> 1.0) descendants_tracker (~> 0.0, >= 0.0.3) equalizer (~> 0.0, >= 0.0.9) PLATFORMS ruby DEPENDENCIES activerecord (~> 4.2.7) database_cleaner grape (~> 1.2.2) grape-entity (~> 0.5.1) otr-activerecord (~> 1.1.0) rack (~> 1.6.0) rack-test rake shotgun sqlite3 BUNDLED WITH 2.0.1 otr-activerecord-2.1.1/examples/grape-activerecord4/Rakefile000066400000000000000000000007051417555416000241160ustar00rootroot00000000000000require 'bundler/setup' require 'rake/testtask' load 'tasks/otr-activerecord.rake' #OTR::ActiveRecord.db_dir = 'db' #OTR::ActiveRecord.migrations_paths = ['db/migrate'] #OTR::ActiveRecord.fixtures_path = 'test/fixtures' #OTR::ActiveRecord.seed_file = 'seeds.rb' namespace :db do task :environment do require_relative 'config/application' end end Rake::TestTask.new do |t| t.libs << 'test' t.test_files = FileList['test/**/*_test.rb'] end otr-activerecord-2.1.1/examples/grape-activerecord4/app/000077500000000000000000000000001417555416000232275ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/app/entities/000077500000000000000000000000001417555416000250535ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/app/entities/v1/000077500000000000000000000000001417555416000254015ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/app/entities/v1/widget.rb000066400000000000000000000003141417555416000272070ustar00rootroot00000000000000module Entities module V1 class Widget < Grape::Entity expose :id, documentation: {type: 'integer'} expose :name, documentation: {type: 'integer', desc: "Widget name"} end end end otr-activerecord-2.1.1/examples/grape-activerecord4/app/models/000077500000000000000000000000001417555416000245125ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/app/models/widget.rb000066400000000000000000000000461417555416000263220ustar00rootroot00000000000000class Widget < ActiveRecord::Base end otr-activerecord-2.1.1/examples/grape-activerecord4/app/routes/000077500000000000000000000000001417555416000245505ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/app/routes/v1.rb000066400000000000000000000002161417555416000254220ustar00rootroot00000000000000module Routes module V1 class API < Grape::API version 'v1' format :json mount Routes::V1::Widgets end end end otr-activerecord-2.1.1/examples/grape-activerecord4/app/routes/v1/000077500000000000000000000000001417555416000250765ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/app/routes/v1/widgets.rb000066400000000000000000000003051417555416000270670ustar00rootroot00000000000000module Routes module V1 class Widgets < Grape::API desc "Get a list of Widgets" get :widgets do present Widget.all, with: Entities::V1::Widget end end end end otr-activerecord-2.1.1/examples/grape-activerecord4/config.ru000066400000000000000000000002271417555416000242650ustar00rootroot00000000000000require './config/application' use OTR::ActiveRecord::ConnectionManagement run Rack::Cascade.new([ Routes::V1::API, # add versions as desired ]) otr-activerecord-2.1.1/examples/grape-activerecord4/config/000077500000000000000000000000001417555416000237145ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/config/application.rb000066400000000000000000000006571417555416000265540ustar00rootroot00000000000000require './config/environment' $:.unshift Config.root.join('lib') # Connect to database OTR::ActiveRecord.configure_from_file! Config.root.join('config', 'database.yml') OTR::ActiveRecord.establish_connection! # Load application [ %w(app models ** *.rb), %w(app entities ** *.rb), %w(app routes v* *.rb), %w(app routes ** *.rb), ].each do |pattern| Dir.glob(Config.root.join(*pattern)).each { |file| require file } end otr-activerecord-2.1.1/examples/grape-activerecord4/config/database.yml000066400000000000000000000004741417555416000262100ustar00rootroot00000000000000development: adapter: sqlite3 encoding: utf8 database: db/development.sqlite3 pool: 5 timeout: 5000 test: adapter: sqlite3 encoding: utf8 database: db/test.sqlite3 pool: 5 timeout: 5000 production: adapter: sqlite3 encoding: utf8 database: db/production.sqlite3 pool: 5 timeout: 5000 otr-activerecord-2.1.1/examples/grape-activerecord4/config/environment.rb000066400000000000000000000004541417555416000266100ustar00rootroot00000000000000require 'ostruct' require 'pathname' # Load environment settings Config = OpenStruct.new Config.env = ENV['RACK_ENV'] ? ENV['RACK_ENV'].to_sym : :development Config.root = Pathname.new(File.expand_path('../..', __FILE__)) # Load dependencies require 'bundler' Bundler.require(:default, Config.env) otr-activerecord-2.1.1/examples/grape-activerecord4/db/000077500000000000000000000000001417555416000230345ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/db/migrate/000077500000000000000000000000001417555416000244645ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/db/migrate/20140913065838_create_widgets.rb000066400000000000000000000002571417555416000314670ustar00rootroot00000000000000class CreateWidgets < ActiveRecord::Migration def change create_table :widgets do |t| t.string :name, null: false t.timestamps null: false end end end otr-activerecord-2.1.1/examples/grape-activerecord4/db/schema.rb000066400000000000000000000017251417555416000246260ustar00rootroot00000000000000# encoding: UTF-8 # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 20140913065838) do create_table "widgets", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end end otr-activerecord-2.1.1/examples/grape-activerecord4/db/seeds.rb000066400000000000000000000001131417555416000244570ustar00rootroot00000000000000Widget.create! name: 'A' Widget.create! name: 'B' Widget.create! name: 'C' otr-activerecord-2.1.1/examples/grape-activerecord4/lib/000077500000000000000000000000001417555416000232155ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/lib/.keep000066400000000000000000000000001417555416000241300ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/test/000077500000000000000000000000001417555416000234265ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/test/fixtures/000077500000000000000000000000001417555416000252775ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/test/fixtures/widgets.yml000066400000000000000000000001301417555416000274620ustar00rootroot00000000000000a: name: Widget A created_at: 2016-02-05 00:00:00 updated_at: 2016-02-05 00:00:00 otr-activerecord-2.1.1/examples/grape-activerecord4/test/models/000077500000000000000000000000001417555416000247115ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/test/models/widgets_test.rb000066400000000000000000000002511417555416000277410ustar00rootroot00000000000000require_relative '../test_helper' class WidgetsTest < TestCase def test_update widget = widgets(:a) widget.name = 'Widget B' assert widget.save end end otr-activerecord-2.1.1/examples/grape-activerecord4/test/routes/000077500000000000000000000000001417555416000247475ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/test/routes/v1/000077500000000000000000000000001417555416000252755ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/test/routes/v1/widgets_test.rb000066400000000000000000000002531417555416000303270ustar00rootroot00000000000000require_relative '../../test_helper' class ApiV1WidgestTest < ApiV1TestCase def test_get_widgets get '/v1/widgets' assert_equal 1, json_response.size end end otr-activerecord-2.1.1/examples/grape-activerecord4/test/support/000077500000000000000000000000001417555416000251425ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord4/test/support/api_test_case.rb000066400000000000000000000004371417555416000302760ustar00rootroot00000000000000require_relative './test_case' class ApiV1TestCase < TestCase include Rack::Test::Methods include ApiTestHelpers def app Routes::V1::API end end class ApiV2TestCase < TestCase include Rack::Test::Methods include ApiTestHelpers def app Routes::V1::AP2 end end otr-activerecord-2.1.1/examples/grape-activerecord4/test/support/helpers.rb000066400000000000000000000001561417555416000271330ustar00rootroot00000000000000module ApiTestHelpers def json_response JSON.parse(last_response.body, symbolize_names: true) end end otr-activerecord-2.1.1/examples/grape-activerecord4/test/support/test_case.rb000066400000000000000000000010271417555416000274410ustar00rootroot00000000000000require_relative './helpers' class TestCase < Minitest::Test def setup DatabaseCleaner.start end def teardown DatabaseCleaner.clean end fixture_names = Dir.glob('./test/fixtures/*.yml').map { |path| File.basename(path).sub(/\.yml$/, '') } fixtures = ActiveRecord::FixtureSet.create_fixtures('test/fixtures', fixture_names) fixtures.each do |fixture_set| define_method fixture_set.name do |record_name| id = fixture_set[record_name.to_s]['id'] fixture_set.model_class.find(id) end end end otr-activerecord-2.1.1/examples/grape-activerecord4/test/test_helper.rb000066400000000000000000000004571417555416000262770ustar00rootroot00000000000000ENV['RACK_ENV'] = 'test' require 'minitest/autorun' require_relative '../config/application' require 'active_record/fixtures' ActiveRecord::Base.logger = nil ActiveRecord::Migration.verbose = false DatabaseCleaner.strategy = :transaction Dir.glob('./test/support/*.rb').each { |file| require file } otr-activerecord-2.1.1/examples/grape-activerecord5/000077500000000000000000000000001417555416000224505ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/.gitignore000066400000000000000000000000351417555416000244360ustar00rootroot00000000000000/.bundle /database/*.sqlite3 otr-activerecord-2.1.1/examples/grape-activerecord5/Gemfile000066400000000000000000000005421417555416000237440ustar00rootroot00000000000000source 'https://rubygems.org' gem 'rack', '~> 1.6.0' gem 'grape', '~> 1.2.2' gem 'grape-entity', '~> 0.5.1' gem 'activerecord', '~> 5.0.2', require: 'active_record' gem 'otr-activerecord', '~> 1.2.1' gem 'sqlite3' gem 'rake' group :development do gem 'shotgun' end group :test do gem 'rack-test', require: 'rack/test' gem 'database_cleaner' end otr-activerecord-2.1.1/examples/grape-activerecord5/Gemfile.lock000066400000000000000000000035751417555416000247040ustar00rootroot00000000000000GEM remote: https://rubygems.org/ specs: activemodel (5.0.2) activesupport (= 5.0.2) activerecord (5.0.2) activemodel (= 5.0.2) activesupport (= 5.0.2) arel (~> 7.0) activesupport (5.0.2) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) minitest (~> 5.1) tzinfo (~> 1.1) arel (7.1.4) axiom-types (0.1.1) descendants_tracker (~> 0.0.4) ice_nine (~> 0.11.0) thread_safe (~> 0.3, >= 0.3.1) builder (3.2.3) coercible (1.0.0) descendants_tracker (~> 0.0.1) concurrent-ruby (1.0.5) database_cleaner (1.5.1) descendants_tracker (0.0.4) thread_safe (~> 0.3, >= 0.3.1) equalizer (0.0.11) grape (1.2.2) activesupport builder mustermann-grape (~> 1.0.0) rack (>= 1.3.0) rack-accept virtus (>= 1.0.0) grape-entity (0.5.1) activesupport multi_json (>= 1.3.2) hashie (3.5.5) hashie-forbidden_attributes (0.1.1) hashie (>= 3.0) i18n (0.8.1) ice_nine (0.11.2) minitest (5.10.1) multi_json (1.12.1) mustermann (1.0.3) mustermann-grape (1.0.0) mustermann (~> 1.0.0) otr-activerecord (1.2.1) activerecord (>= 4.0, < 5.1) hashie-forbidden_attributes (~> 0.1) rack (1.6.4) rack-accept (0.4.5) rack (>= 0.4) rack-test (0.6.3) rack (>= 1.0) rake (10.5.0) shotgun (0.9.1) rack (>= 1.0) sqlite3 (1.3.11) thread_safe (0.3.6) tzinfo (1.2.3) thread_safe (~> 0.1) virtus (1.0.5) axiom-types (~> 0.1) coercible (~> 1.0) descendants_tracker (~> 0.0, >= 0.0.3) equalizer (~> 0.0, >= 0.0.9) PLATFORMS ruby DEPENDENCIES activerecord (~> 5.0.2) database_cleaner grape (~> 1.2.2) grape-entity (~> 0.5.1) otr-activerecord (~> 1.2.1) rack (~> 1.6.0) rack-test rake shotgun sqlite3 BUNDLED WITH 1.17.1 otr-activerecord-2.1.1/examples/grape-activerecord5/Rakefile000066400000000000000000000007201417555416000241140ustar00rootroot00000000000000require 'bundler/setup' require 'rake/testtask' load 'tasks/otr-activerecord.rake' OTR::ActiveRecord.db_dir = 'database' OTR::ActiveRecord.migrations_paths = ['database/migrations'] OTR::ActiveRecord.fixtures_path = 'test/fixtures' OTR::ActiveRecord.seed_file = 'seeds.rb' namespace :db do task :environment do require_relative 'config/application' end end Rake::TestTask.new do |t| t.libs << 'test' t.test_files = FileList['test/**/*_test.rb'] end otr-activerecord-2.1.1/examples/grape-activerecord5/app/000077500000000000000000000000001417555416000232305ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/app/entities/000077500000000000000000000000001417555416000250545ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/app/entities/v1/000077500000000000000000000000001417555416000254025ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/app/entities/v1/widget.rb000066400000000000000000000003141417555416000272100ustar00rootroot00000000000000module Entities module V1 class Widget < Grape::Entity expose :id, documentation: {type: 'integer'} expose :name, documentation: {type: 'integer', desc: "Widget name"} end end end otr-activerecord-2.1.1/examples/grape-activerecord5/app/models/000077500000000000000000000000001417555416000245135ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/app/models/widget.rb000066400000000000000000000000461417555416000263230ustar00rootroot00000000000000class Widget < ActiveRecord::Base end otr-activerecord-2.1.1/examples/grape-activerecord5/app/routes/000077500000000000000000000000001417555416000245515ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/app/routes/v1.rb000066400000000000000000000002161417555416000254230ustar00rootroot00000000000000module Routes module V1 class API < Grape::API version 'v1' format :json mount Routes::V1::Widgets end end end otr-activerecord-2.1.1/examples/grape-activerecord5/app/routes/v1/000077500000000000000000000000001417555416000250775ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/app/routes/v1/widgets.rb000066400000000000000000000003051417555416000270700ustar00rootroot00000000000000module Routes module V1 class Widgets < Grape::API desc "Get a list of Widgets" get :widgets do present Widget.all, with: Entities::V1::Widget end end end end otr-activerecord-2.1.1/examples/grape-activerecord5/config.ru000066400000000000000000000002271417555416000242660ustar00rootroot00000000000000require './config/application' use OTR::ActiveRecord::ConnectionManagement run Rack::Cascade.new([ Routes::V1::API, # add versions as desired ]) otr-activerecord-2.1.1/examples/grape-activerecord5/config/000077500000000000000000000000001417555416000237155ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/config/application.rb000066400000000000000000000006571417555416000265550ustar00rootroot00000000000000require './config/environment' $:.unshift Config.root.join('lib') # Connect to database OTR::ActiveRecord.configure_from_file! Config.root.join('config', 'database.yml') OTR::ActiveRecord.establish_connection! # Load application [ %w(app models ** *.rb), %w(app entities ** *.rb), %w(app routes v* *.rb), %w(app routes ** *.rb), ].each do |pattern| Dir.glob(Config.root.join(*pattern)).each { |file| require file } end otr-activerecord-2.1.1/examples/grape-activerecord5/config/database.yml000066400000000000000000000005161417555416000262060ustar00rootroot00000000000000development: adapter: sqlite3 encoding: utf8 database: database/development.sqlite3 pool: 5 timeout: 5000 test: adapter: sqlite3 encoding: utf8 database: database/test.sqlite3 pool: 5 timeout: 5000 production: adapter: sqlite3 encoding: utf8 database: database/production.sqlite3 pool: 5 timeout: 5000 otr-activerecord-2.1.1/examples/grape-activerecord5/config/environment.rb000066400000000000000000000004541417555416000266110ustar00rootroot00000000000000require 'ostruct' require 'pathname' # Load environment settings Config = OpenStruct.new Config.env = ENV['RACK_ENV'] ? ENV['RACK_ENV'].to_sym : :development Config.root = Pathname.new(File.expand_path('../..', __FILE__)) # Load dependencies require 'bundler' Bundler.require(:default, Config.env) otr-activerecord-2.1.1/examples/grape-activerecord5/database/000077500000000000000000000000001417555416000242145ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/database/migrations/000077500000000000000000000000001417555416000263705ustar00rootroot0000000000000020160730195332_create_widgets.rb000066400000000000000000000002641417555416000333020ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/database/migrationsclass CreateWidgets < ActiveRecord::Migration[5.0] def change create_table :widgets do |t| t.string :name, null: false t.timestamps null: false end end end otr-activerecord-2.1.1/examples/grape-activerecord5/database/schema.rb000066400000000000000000000017031417555416000260020ustar00rootroot00000000000000# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 20160730195332) do create_table "widgets", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end end otr-activerecord-2.1.1/examples/grape-activerecord5/database/seeds.rb000066400000000000000000000001131417555416000256370ustar00rootroot00000000000000Widget.create! name: 'A' Widget.create! name: 'B' Widget.create! name: 'C' otr-activerecord-2.1.1/examples/grape-activerecord5/lib/000077500000000000000000000000001417555416000232165ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/lib/.keep000066400000000000000000000000001417555416000241310ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/test/000077500000000000000000000000001417555416000234275ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/test/fixtures/000077500000000000000000000000001417555416000253005ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/test/fixtures/widgets.yml000066400000000000000000000001301417555416000274630ustar00rootroot00000000000000a: name: Widget A created_at: 2016-02-05 00:00:00 updated_at: 2016-02-05 00:00:00 otr-activerecord-2.1.1/examples/grape-activerecord5/test/models/000077500000000000000000000000001417555416000247125ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/test/models/widgets_test.rb000066400000000000000000000002511417555416000277420ustar00rootroot00000000000000require_relative '../test_helper' class WidgetsTest < TestCase def test_update widget = widgets(:a) widget.name = 'Widget B' assert widget.save end end otr-activerecord-2.1.1/examples/grape-activerecord5/test/routes/000077500000000000000000000000001417555416000247505ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/test/routes/v1/000077500000000000000000000000001417555416000252765ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/test/routes/v1/widgets_test.rb000066400000000000000000000002531417555416000303300ustar00rootroot00000000000000require_relative '../../test_helper' class ApiV1WidgestTest < ApiV1TestCase def test_get_widgets get '/v1/widgets' assert_equal 1, json_response.size end end otr-activerecord-2.1.1/examples/grape-activerecord5/test/support/000077500000000000000000000000001417555416000251435ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/grape-activerecord5/test/support/api_test_case.rb000066400000000000000000000004371417555416000302770ustar00rootroot00000000000000require_relative './test_case' class ApiV1TestCase < TestCase include Rack::Test::Methods include ApiTestHelpers def app Routes::V1::API end end class ApiV2TestCase < TestCase include Rack::Test::Methods include ApiTestHelpers def app Routes::V1::AP2 end end otr-activerecord-2.1.1/examples/grape-activerecord5/test/support/helpers.rb000066400000000000000000000001561417555416000271340ustar00rootroot00000000000000module ApiTestHelpers def json_response JSON.parse(last_response.body, symbolize_names: true) end end otr-activerecord-2.1.1/examples/grape-activerecord5/test/support/test_case.rb000066400000000000000000000010271417555416000274420ustar00rootroot00000000000000require_relative './helpers' class TestCase < Minitest::Test def setup DatabaseCleaner.start end def teardown DatabaseCleaner.clean end fixture_names = Dir.glob('./test/fixtures/*.yml').map { |path| File.basename(path).sub(/\.yml$/, '') } fixtures = ActiveRecord::FixtureSet.create_fixtures('test/fixtures', fixture_names) fixtures.each do |fixture_set| define_method fixture_set.name do |record_name| id = fixture_set[record_name.to_s]['id'] fixture_set.model_class.find(id) end end end otr-activerecord-2.1.1/examples/grape-activerecord5/test/test_helper.rb000066400000000000000000000004571417555416000263000ustar00rootroot00000000000000ENV['RACK_ENV'] = 'test' require 'minitest/autorun' require_relative '../config/application' require 'active_record/fixtures' ActiveRecord::Base.logger = nil ActiveRecord::Migration.verbose = false DatabaseCleaner.strategy = :transaction Dir.glob('./test/support/*.rb').each { |file| require file } otr-activerecord-2.1.1/examples/rack-activerecord7.0/000077500000000000000000000000001417555416000224325ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rack-activerecord7.0/Gemfile000066400000000000000000000003301417555416000237210ustar00rootroot00000000000000source 'https://rubygems.org' gem 'rack', '~> 2.2' gem 'activerecord', '~> 7.0.1', require: 'active_record' gem 'otr-activerecord', path: '../../' gem 'sqlite3' gem 'rake' group :development do gem 'shotgun' end otr-activerecord-2.1.1/examples/rack-activerecord7.0/Gemfile.lock000066400000000000000000000016361417555416000246620ustar00rootroot00000000000000PATH remote: ../.. specs: otr-activerecord (2.1.0) activerecord (>= 4.0, < 7.1) hashie-forbidden_attributes (~> 0.1) GEM remote: https://rubygems.org/ specs: activemodel (7.0.1) activesupport (= 7.0.1) activerecord (7.0.1) activemodel (= 7.0.1) activesupport (= 7.0.1) activesupport (7.0.1) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) concurrent-ruby (1.1.9) hashie (5.0.0) hashie-forbidden_attributes (0.1.1) hashie (>= 3.0) i18n (1.9.1) concurrent-ruby (~> 1.0) minitest (5.15.0) rack (2.2.3) rake (13.0.6) shotgun (0.9.2) rack (>= 1.0) sqlite3 (1.4.2) tzinfo (2.0.4) concurrent-ruby (~> 1.0) PLATFORMS ruby DEPENDENCIES activerecord (~> 7.0.1) otr-activerecord! rack (~> 2.2) rake shotgun sqlite3 BUNDLED WITH 2.1.4 otr-activerecord-2.1.1/examples/rack-activerecord7.0/Rakefile000066400000000000000000000002211417555416000240720ustar00rootroot00000000000000require 'bundler/setup' load 'tasks/otr-activerecord.rake' namespace :db do task :environment do require_relative 'environment' end end otr-activerecord-2.1.1/examples/rack-activerecord7.0/config.ru000066400000000000000000000005171417555416000242520ustar00rootroot00000000000000require_relative 'environment' use OTR::ActiveRecord::ConnectionManagement use OTR::ActiveRecord::QueryCache map '/' do run ->(env) { body = Widget.all .map { |w| {id: w.id, name: w.name} } .to_json [200, {'Content-Type' => 'application/json', 'Content-Length' => body.size.to_s}, [body]] } end otr-activerecord-2.1.1/examples/rack-activerecord7.0/db/000077500000000000000000000000001417555416000230175ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rack-activerecord7.0/db/config.yml000066400000000000000000000005331417555416000250100ustar00rootroot00000000000000development: adapter: sqlite3 encoding: utf8 database: db/otr_example_70_dev.sqlite3 pool: 5 timeout: 5000 test: adapter: sqlite3 encoding: utf8 database: db/otr_example_70_test.sqlite3 pool: 5 timeout: 5000 production: adapter: sqlite3 encoding: utf8 database: db/otr_example_70_prod.sqlite3 pool: 5 timeout: 5000 otr-activerecord-2.1.1/examples/rack-activerecord7.0/db/migrate/000077500000000000000000000000001417555416000244475ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rack-activerecord7.0/db/migrate/.keep000066400000000000000000000000001417555416000253620ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rack-activerecord7.0/db/schema.rb000066400000000000000000000024241417555416000246060ustar00rootroot00000000000000# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # This file is the source Rails uses to define your schema when running `bin/rails # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to # be faster and is potentially less error prone than running all of your # migrations from scratch. Old migrations may fail to apply correctly if those # migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2021_10_23_013351) do create_table "bars", force: :cascade do |t| t.string "name", null: false end create_table "foos", force: :cascade do |t| t.string "name", null: false end create_table "splines", force: :cascade do |t| t.string "name", null: false t.index ["name"], name: "index_splines_on_name", unique: true end create_table "widgets", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end end otr-activerecord-2.1.1/examples/rack-activerecord7.0/db/seeds.rb000066400000000000000000000001131417555416000244420ustar00rootroot00000000000000Widget.create! name: 'A' Widget.create! name: 'B' Widget.create! name: 'C' otr-activerecord-2.1.1/examples/rack-activerecord7.0/environment.rb000066400000000000000000000004311417555416000253210ustar00rootroot00000000000000require 'json' require 'bundler' Bundler.require(:default, ENV['OTR_ENV'] ? ENV['OTR_ENV'].to_sym : :development) OTR::ActiveRecord.configure_from_file! './db/config.yml' OTR::ActiveRecord.establish_connection! class Widget < ActiveRecord::Base validates_presence_of :name end otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/000077500000000000000000000000001417555416000232745ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/.gitignore000066400000000000000000000000311417555416000252560ustar00rootroot00000000000000/.bundle /data/*.sqlite3 otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/Gemfile000066400000000000000000000002231417555416000245640ustar00rootroot00000000000000source 'https://rubygems.org' gem 'activerecord', '~> 5.2.0', require: 'active_record' gem 'otr-activerecord', path: '../../' gem 'pg' gem 'rake' otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/Gemfile.lock000066400000000000000000000015721417555416000255230ustar00rootroot00000000000000PATH remote: ../.. specs: otr-activerecord (1.3.1) activerecord (>= 4.0, < 6.1) hashie-forbidden_attributes (~> 0.1) GEM remote: https://rubygems.org/ specs: activemodel (5.2.3) activesupport (= 5.2.3) activerecord (5.2.3) activemodel (= 5.2.3) activesupport (= 5.2.3) arel (>= 9.0) activesupport (5.2.3) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) arel (9.0.0) concurrent-ruby (1.1.5) hashie (3.6.0) hashie-forbidden_attributes (0.1.1) hashie (>= 3.0) i18n (1.6.0) concurrent-ruby (~> 1.0) minitest (5.11.3) pg (1.1.4) rake (12.3.2) thread_safe (0.3.6) tzinfo (1.2.5) thread_safe (~> 0.1) PLATFORMS ruby DEPENDENCIES activerecord (~> 5.2.0) otr-activerecord! pg rake BUNDLED WITH 2.0.1 otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/Rakefile000066400000000000000000000023241417555416000247420ustar00rootroot00000000000000require 'bundler/setup' load 'tasks/otr-activerecord.rake' OTR::ActiveRecord.db_dir = 'data' OTR::ActiveRecord.migrations_paths = ['data/migrate'] OTR::ActiveRecord.seed_file = 'seeds.rb' namespace :db do task :environment do require_relative 'environment' end end namespace :widgets do desc "Query all widgets" task :query => 'db:environment' do widgets = Widget.order('name').to_a puts widgets.map(&:as_json).to_json end desc "Fetch a single widget" task :fetch, [:id] => 'db:environment' do |_, args| widget = Widget.find(args[:id]) puts widget.to_json end desc "Create a new widget" task :create, [:name] => 'db:environment' do |_, args| widget = Widget.new(name: args[:name]) if widget.save puts widget.to_json else abort({errors: widget.errors.full_message}.to_json) end end desc "Update a widget" task :update, [:id, :name] => 'db:environment' do |_, args| widget = Widget.where(id: args[:id]).first abort({errors: ["Widget '#{args[:id]}' not found"]}.to_json) if widget.nil? if widget.update_attributes({name: args[:name]}) puts widget.to_json else abort({errors: widget.errors.full_message}.to_json) end end end otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/data/000077500000000000000000000000001417555416000242055ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/data/config.yml000066400000000000000000000005031417555416000261730ustar00rootroot00000000000000development: adapter: postgresql encoding: utf8 database: otr_example_52_dev pool: 5 timeout: 5000 test: adapter: postgresql encoding: utf8 database: otr_example_52_test pool: 5 timeout: 5000 production: adapter: postgresql encoding: utf8 database: otr_example_52_prod pool: 5 timeout: 5000 otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/data/migrate/000077500000000000000000000000001417555416000256355ustar00rootroot0000000000000020160731142314_create_widgets.rb000066400000000000000000000002641417555416000325400ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/data/migrateclass CreateWidgets < ActiveRecord::Migration[5.0] def change create_table :widgets do |t| t.string :name, null: false t.timestamps null: false end end end 20171128231939_create_splines.rb000066400000000000000000000003011417555416000325550ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/data/migrateclass CreateSplines < ActiveRecord::Migration[5.2] def change create_table :splines do |t| t.string :name, null: false end add_index :splines, :name, unique: true end end otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/data/migrate/20180423164820_foo.rb000066400000000000000000000002101417555416000304060ustar00rootroot00000000000000class Foo < ActiveRecord::Migration[5.2] def change create_table :foos do |t| t.string :name, null: false end end end otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/data/schema.rb000066400000000000000000000024351417555416000257760ustar00rootroot00000000000000# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2018_04_23_164820) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" create_table "foos", force: :cascade do |t| t.string "name", null: false end create_table "splines", force: :cascade do |t| t.string "name", null: false t.index ["name"], name: "index_splines_on_name", unique: true end create_table "widgets", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end end otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/data/seeds.rb000066400000000000000000000001131417555416000256300ustar00rootroot00000000000000Widget.create! name: 'A' Widget.create! name: 'B' Widget.create! name: 'C' otr-activerecord-2.1.1/examples/rakefile-activerecord5.2/environment.rb000066400000000000000000000011001417555416000261550ustar00rootroot00000000000000require 'json' require 'bundler' Bundler.require(:default, ENV['OTR_ENV'] ? ENV['OTR_ENV'].to_sym : :development) OTR::ActiveRecord.configure_from_file! './data/config.yml' #OTR::ActiveRecord.configure_from_url! 'sqlite3:///home/jhollinger/devel/otr-activerecord/examples/rakefile-activerecord5.2/db.sqlite3' #OTR::ActiveRecord.configure_from_hash!(adapter: 'sqlite3', database: '/home/jhollinger/devel/otr-activerecord/examples/rakefile-activerecord5.2/db.sqlite3') OTR::ActiveRecord.establish_connection! class Widget < ActiveRecord::Base validates_presence_of :name end otr-activerecord-2.1.1/examples/rakefile-activerecord5/000077500000000000000000000000001417555416000231345ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord5/.gitignore000066400000000000000000000000311417555416000251160ustar00rootroot00000000000000/.bundle /data/*.sqlite3 otr-activerecord-2.1.1/examples/rakefile-activerecord5/Gemfile000066400000000000000000000002241417555416000244250ustar00rootroot00000000000000source 'https://rubygems.org' gem 'activerecord', '~> 5.0.0', require: 'active_record' gem 'otr-activerecord', '~> 1.1.0' gem 'sqlite3' gem 'rake' otr-activerecord-2.1.1/examples/rakefile-activerecord5/Gemfile.lock000066400000000000000000000015141417555416000253570ustar00rootroot00000000000000GEM remote: https://rubygems.org/ specs: activemodel (5.0.0) activesupport (= 5.0.0) activerecord (5.0.0) activemodel (= 5.0.0) activesupport (= 5.0.0) arel (~> 7.0) activesupport (5.0.0) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) minitest (~> 5.1) tzinfo (~> 1.1) arel (7.1.1) concurrent-ruby (1.0.2) hashie (3.4.4) hashie-forbidden_attributes (0.1.1) hashie (>= 3.0) i18n (0.7.0) minitest (5.9.0) otr-activerecord (1.1.0) activerecord (>= 4.0, <= 5.0) hashie-forbidden_attributes (~> 0.1) rake (11.2.2) sqlite3 (1.3.11) thread_safe (0.3.5) tzinfo (1.2.2) thread_safe (~> 0.1) PLATFORMS ruby DEPENDENCIES activerecord (~> 5.0.0) otr-activerecord (~> 1.1.0) rake sqlite3 BUNDLED WITH 2.0.1 otr-activerecord-2.1.1/examples/rakefile-activerecord5/Rakefile000066400000000000000000000022071417555416000246020ustar00rootroot00000000000000require 'bundler/setup' load 'tasks/otr-activerecord.rake' OTR::ActiveRecord.db_dir = 'data' OTR::ActiveRecord.migrations_paths = ['data/migrate'] OTR::ActiveRecord.seed_file = 'seeds.rb' namespace :db do task :environment do require_relative 'environment' end end namespace :widgets do desc "Query all widgets" task :query => 'db:environment' do widgets = Widget.order('name').to_a puts widgets.map(&:as_json).to_json end desc "Fetch a single widget" task :fetch => 'db:environment' do end desc "Create a new widget" task :create, [:name] => 'db:environment' do |_, args| widget = Widget.new(name: args[:name]) if widget.save puts widget.to_json else abort({errors: widget.errors.full_message}.to_json) end end desc "Update a widget" task :update, [:id, :name] => 'db:environment' do |_, args| widget = Widget.where(id: args[:id]).first abort({errors: ["Widget '#{args[:id]}' not found"]}.to_json) if widget.nil? if widget.update_attributes({name: args[:name]}) puts widget.to_json else abort({errors: widget.errors.full_message}.to_json) end end end otr-activerecord-2.1.1/examples/rakefile-activerecord5/data/000077500000000000000000000000001417555416000240455ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord5/data/config.yml000066400000000000000000000005021417555416000260320ustar00rootroot00000000000000development: adapter: sqlite3 encoding: utf8 database: data/development.sqlite3 pool: 5 timeout: 5000 test: adapter: sqlite3 encoding: utf8 database: data/test.sqlite3 pool: 5 timeout: 5000 production: adapter: sqlite3 encoding: utf8 database: data/production.sqlite3 pool: 5 timeout: 5000 otr-activerecord-2.1.1/examples/rakefile-activerecord5/data/migrate/000077500000000000000000000000001417555416000254755ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord5/data/migrate/20160731142314_create_widgets.rb000066400000000000000000000002641417555416000324570ustar00rootroot00000000000000class CreateWidgets < ActiveRecord::Migration[5.0] def change create_table :widgets do |t| t.string :name, null: false t.timestamps null: false end end end otr-activerecord-2.1.1/examples/rakefile-activerecord5/data/schema.rb000066400000000000000000000017031417555416000256330ustar00rootroot00000000000000# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 20160731142314) do create_table "widgets", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end end otr-activerecord-2.1.1/examples/rakefile-activerecord5/data/seeds.rb000066400000000000000000000001131417555416000254700ustar00rootroot00000000000000Widget.create! name: 'A' Widget.create! name: 'B' Widget.create! name: 'C' otr-activerecord-2.1.1/examples/rakefile-activerecord5/environment.rb000066400000000000000000000004321417555416000260240ustar00rootroot00000000000000require 'json' require 'bundler' Bundler.require(:default, ENV['OTR_ENV'] ? ENV['OTR_ENV'].to_sym : :development) OTR::ActiveRecord.configure_from_file! './data/config.yml' OTR::ActiveRecord.establish_connection! class Widget < ActiveRecord::Base validates_presence_of :name end otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/000077500000000000000000000000001417555416000232735ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/.gitignore000066400000000000000000000000311417555416000252550ustar00rootroot00000000000000/.bundle /data/*.sqlite3 otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/Gemfile000066400000000000000000000002301417555416000245610ustar00rootroot00000000000000source 'https://rubygems.org' gem 'activerecord', '~> 6.0.0', require: 'active_record' gem 'otr-activerecord', path: '../../' gem 'sqlite3' gem 'rake' otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/Gemfile.lock000066400000000000000000000016701417555416000255210ustar00rootroot00000000000000PATH remote: ../.. specs: otr-activerecord (2.0.1) activerecord (>= 4.0, < 6.3) hashie-forbidden_attributes (~> 0.1) GEM remote: https://rubygems.org/ specs: activemodel (6.0.3.5) activesupport (= 6.0.3.5) activerecord (6.0.3.5) activemodel (= 6.0.3.5) activesupport (= 6.0.3.5) activesupport (6.0.3.5) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) zeitwerk (~> 2.2, >= 2.2.2) concurrent-ruby (1.1.8) hashie (4.1.0) hashie-forbidden_attributes (0.1.1) hashie (>= 3.0) i18n (1.8.8) concurrent-ruby (~> 1.0) minitest (5.14.3) rake (12.3.2) sqlite3 (1.4.2) thread_safe (0.3.6) tzinfo (1.2.9) thread_safe (~> 0.1) zeitwerk (2.4.2) PLATFORMS ruby universal-darwin-20 DEPENDENCIES activerecord (~> 6.0.0) otr-activerecord! rake sqlite3 BUNDLED WITH 2.2.1 otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/Rakefile000066400000000000000000000023211417555416000247360ustar00rootroot00000000000000require 'bundler/setup' load 'tasks/otr-activerecord.rake' OTR::ActiveRecord.db_dir = 'data' OTR::ActiveRecord.migrations_paths = ['data/migrate'] OTR::ActiveRecord.seed_file = 'seeds.rb' namespace :db do task :environment do require_relative 'environment' end end namespace :widgets do desc "Query all widgets" task :query => 'db:environment' do widgets = Widget.order('name').to_a puts widgets.map(&:as_json).to_json end desc "Fetch a single widget" task :fetch, [:id] => 'db:environment' do |_, args| widget = Widget.find(args[:id]) puts widget.to_json end desc "Create a new widget" task :create, [:name] => 'db:environment' do |_, args| widget = Widget.new(name: args[:name]) if widget.save puts widget.to_json else abort({errors: widget.errors.full_message}.to_json) end end desc "Update a widget" task :update, [:id, :name] => 'db:environment' do |_, args| widget = Widget.where(id: args[:id]).first abort({errors: ["Widget '#{args[:id]}' not found"]}.to_json) if widget.nil? if widget.update_columns({name: args[:name]}) puts widget.to_json else abort({errors: widget.errors.full_message}.to_json) end end end otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/data/000077500000000000000000000000001417555416000242045ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/data/config.yml000066400000000000000000000005411417555416000261740ustar00rootroot00000000000000development: adapter: sqlite3 encoding: utf8 database: data/otr_example_60_dev.sqlite3 pool: 5 timeout: 5000 test: adapter: sqlite3 encoding: utf8 database: data/otr_example_60_test.sqlite3 pool: 5 timeout: 5000 production: adapter: sqlite3 encoding: utf8 database: data/otr_example_60_prod.sqlite3 pool: 5 timeout: 5000 otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/data/migrate/000077500000000000000000000000001417555416000256345ustar00rootroot0000000000000020160731142314_create_widgets.rb000066400000000000000000000002641417555416000325370ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/data/migrateclass CreateWidgets < ActiveRecord::Migration[5.0] def change create_table :widgets do |t| t.string :name, null: false t.timestamps null: false end end end 20171128231939_create_splines.rb000066400000000000000000000003011417555416000325540ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/data/migrateclass CreateSplines < ActiveRecord::Migration[5.2] def change create_table :splines do |t| t.string :name, null: false end add_index :splines, :name, unique: true end end otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/data/migrate/20180423164820_foo.rb000066400000000000000000000002101417555416000304050ustar00rootroot00000000000000class Foo < ActiveRecord::Migration[5.2] def change create_table :foos do |t| t.string :name, null: false end end end otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/data/schema.rb000066400000000000000000000024351417555416000257750ustar00rootroot00000000000000# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2018_04_23_164820) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" create_table "foos", force: :cascade do |t| t.string "name", null: false end create_table "splines", force: :cascade do |t| t.string "name", null: false t.index ["name"], name: "index_splines_on_name", unique: true end create_table "widgets", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end end otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/data/seeds.rb000066400000000000000000000001131417555416000256270ustar00rootroot00000000000000Widget.create! name: 'A' Widget.create! name: 'B' Widget.create! name: 'C' otr-activerecord-2.1.1/examples/rakefile-activerecord6.0/environment.rb000066400000000000000000000011001417555416000261540ustar00rootroot00000000000000require 'json' require 'bundler' Bundler.require(:default, ENV['OTR_ENV'] ? ENV['OTR_ENV'].to_sym : :development) OTR::ActiveRecord.configure_from_file! './data/config.yml' #OTR::ActiveRecord.configure_from_url! 'sqlite3:///home/jhollinger/devel/otr-activerecord/examples/rakefile-activerecord5.2/db.sqlite3' #OTR::ActiveRecord.configure_from_hash!(adapter: 'sqlite3', database: '/home/jhollinger/devel/otr-activerecord/examples/rakefile-activerecord5.2/db.sqlite3') OTR::ActiveRecord.establish_connection! class Widget < ActiveRecord::Base validates_presence_of :name end otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/000077500000000000000000000000001417555416000232745ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/.gitignore000066400000000000000000000000311417555416000252560ustar00rootroot00000000000000/.bundle /data/*.sqlite3 otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/Gemfile000066400000000000000000000002251417555416000245660ustar00rootroot00000000000000source 'https://rubygems.org' gem 'activerecord', '7.0.1', require: 'active_record' gem 'otr-activerecord', path: '../../' gem 'sqlite3' gem 'rake' otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/Gemfile.lock000066400000000000000000000015131417555416000255160ustar00rootroot00000000000000PATH remote: ../.. specs: otr-activerecord (2.1.0) activerecord (>= 4.0, < 7.1) hashie-forbidden_attributes (~> 0.1) GEM remote: https://rubygems.org/ specs: activemodel (7.0.1) activesupport (= 7.0.1) activerecord (7.0.1) activemodel (= 7.0.1) activesupport (= 7.0.1) activesupport (7.0.1) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) concurrent-ruby (1.1.9) hashie (5.0.0) hashie-forbidden_attributes (0.1.1) hashie (>= 3.0) i18n (1.8.11) concurrent-ruby (~> 1.0) minitest (5.15.0) rake (13.0.6) sqlite3 (1.4.2) tzinfo (2.0.4) concurrent-ruby (~> 1.0) PLATFORMS ruby DEPENDENCIES activerecord (= 7.0.1) otr-activerecord! rake sqlite3 BUNDLED WITH 2.1.4 otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/Rakefile000066400000000000000000000023211417555416000247370ustar00rootroot00000000000000require 'bundler/setup' load 'tasks/otr-activerecord.rake' OTR::ActiveRecord.db_dir = 'data' OTR::ActiveRecord.migrations_paths = ['data/migrate'] OTR::ActiveRecord.seed_file = 'seeds.rb' namespace :db do task :environment do require_relative 'environment' end end namespace :widgets do desc "Query all widgets" task :query => 'db:environment' do widgets = Widget.order('name').to_a puts widgets.map(&:as_json).to_json end desc "Fetch a single widget" task :fetch, [:id] => 'db:environment' do |_, args| widget = Widget.find(args[:id]) puts widget.to_json end desc "Create a new widget" task :create, [:name] => 'db:environment' do |_, args| widget = Widget.new(name: args[:name]) if widget.save puts widget.to_json else abort({errors: widget.errors.full_message}.to_json) end end desc "Update a widget" task :update, [:id, :name] => 'db:environment' do |_, args| widget = Widget.where(id: args[:id]).first abort({errors: ["Widget '#{args[:id]}' not found"]}.to_json) if widget.nil? if widget.update_columns({name: args[:name]}) puts widget.to_json else abort({errors: widget.errors.full_message}.to_json) end end end otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/data/000077500000000000000000000000001417555416000242055ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/data/config.yml000066400000000000000000000005411417555416000261750ustar00rootroot00000000000000development: adapter: sqlite3 encoding: utf8 database: data/otr_example_70_dev.sqlite3 pool: 5 timeout: 5000 test: adapter: sqlite3 encoding: utf8 database: data/otr_example_70_test.sqlite3 pool: 5 timeout: 5000 production: adapter: sqlite3 encoding: utf8 database: data/otr_example_70_prod.sqlite3 pool: 5 timeout: 5000 otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/data/migrate/000077500000000000000000000000001417555416000256355ustar00rootroot0000000000000020160731142314_create_widgets.rb000066400000000000000000000002641417555416000325400ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/data/migrateclass CreateWidgets < ActiveRecord::Migration[5.0] def change create_table :widgets do |t| t.string :name, null: false t.timestamps null: false end end end 20171128231939_create_splines.rb000066400000000000000000000003011417555416000325550ustar00rootroot00000000000000otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/data/migrateclass CreateSplines < ActiveRecord::Migration[5.2] def change create_table :splines do |t| t.string :name, null: false end add_index :splines, :name, unique: true end end otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/data/migrate/20180423164820_foo.rb000066400000000000000000000002101417555416000304060ustar00rootroot00000000000000class Foo < ActiveRecord::Migration[5.2] def change create_table :foos do |t| t.string :name, null: false end end end otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/data/migrate/20211023013351_bar.rb000066400000000000000000000002101417555416000303460ustar00rootroot00000000000000class Bar < ActiveRecord::Migration[7.0] def change create_table :bars do |t| t.string :name, null: false end end end otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/data/schema.rb000066400000000000000000000024241417555416000257740ustar00rootroot00000000000000# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # This file is the source Rails uses to define your schema when running `bin/rails # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to # be faster and is potentially less error prone than running all of your # migrations from scratch. Old migrations may fail to apply correctly if those # migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2021_10_23_013351) do create_table "bars", force: :cascade do |t| t.string "name", null: false end create_table "foos", force: :cascade do |t| t.string "name", null: false end create_table "splines", force: :cascade do |t| t.string "name", null: false t.index ["name"], name: "index_splines_on_name", unique: true end create_table "widgets", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end end otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/data/seeds.rb000066400000000000000000000001131417555416000256300ustar00rootroot00000000000000Widget.create! name: 'A' Widget.create! name: 'B' Widget.create! name: 'C' otr-activerecord-2.1.1/examples/rakefile-activerecord7.0/environment.rb000066400000000000000000000011001417555416000261550ustar00rootroot00000000000000require 'json' require 'bundler' Bundler.require(:default, ENV['OTR_ENV'] ? ENV['OTR_ENV'].to_sym : :development) OTR::ActiveRecord.configure_from_file! './data/config.yml' #OTR::ActiveRecord.configure_from_url! 'sqlite3:///home/jhollinger/devel/otr-activerecord/examples/rakefile-activerecord5.2/db.sqlite3' #OTR::ActiveRecord.configure_from_hash!(adapter: 'sqlite3', database: '/home/jhollinger/devel/otr-activerecord/examples/rakefile-activerecord5.2/db.sqlite3') OTR::ActiveRecord.establish_connection! class Widget < ActiveRecord::Base validates_presence_of :name end otr-activerecord-2.1.1/lib/000077500000000000000000000000001417555416000155455ustar00rootroot00000000000000otr-activerecord-2.1.1/lib/otr-activerecord.rb000066400000000000000000000004331417555416000213460ustar00rootroot00000000000000require 'active_record' require 'hashie-forbidden_attributes' require 'otr-activerecord/version' require 'otr-activerecord/activerecord' require 'otr-activerecord/middleware/connection_management' require 'otr-activerecord/middleware/query_cache' require 'otr-activerecord/defaults' otr-activerecord-2.1.1/lib/otr-activerecord/000077500000000000000000000000001417555416000210215ustar00rootroot00000000000000otr-activerecord-2.1.1/lib/otr-activerecord/activerecord.rb000066400000000000000000000067161417555416000240320ustar00rootroot00000000000000require 'erb' # "Off the Rails" ActiveRecord configuration/integration for Grape, Sinatra, Rack, and any other kind of app module OTR # ActiveRecord configuration module module ActiveRecord autoload :Compatibility4, 'otr-activerecord/compatibility_4' autoload :Compatibility5, 'otr-activerecord/compatibility_5' autoload :Compatibility6, 'otr-activerecord/compatibility_6' autoload :Compatibility7, 'otr-activerecord/compatibility_7' class << self # Relative path to the "db" dir attr_accessor :db_dir # Relative path(s) to the migrations directory attr_accessor :migrations_paths # Relative path to the fixtures directory attr_accessor :fixtures_path # Name of the seeds file in db_dir attr_accessor :seed_file # Internal compatibility layer across different major versions of AR attr_accessor :_normalizer end # Connect to database with a Hash. Example: # {adapter: 'postgresql', host: 'localhost', database: 'db', username: 'user', password: 'pass', encoding: 'utf8', pool: 10, timeout: 5000} def self.configure_from_hash!(spec) config = spec.stringify_keys.merge("migrations_paths" => ::OTR::ActiveRecord.migrations_paths) ::ActiveRecord::Base.configurations = {rack_env.to_s => config} end # Connect to database with a DB URL. Example: "postgres://user:pass@localhost/db" def self.configure_from_url!(url) require 'uri' uri = URI(url) spec = {"adapter" => uri.scheme} case spec["adapter"] when /^sqlite/i spec["database"] = url =~ /::memory:/ ? ":memory:" : "#{uri.host}#{uri.path}" else spec["host"] = uri.host if uri.host spec["port"] = uri.port if uri.port spec["database"] = uri.path.sub(/^\//, "") spec["username"] = uri.user if uri.user spec["password"] = uri.password if uri.password end if uri.query opts_ary = URI.decode_www_form(uri.query) opts = Hash[opts_ary] spec.merge!(opts) end configure_from_hash! spec end # Connect to database with a yml file. Example: "config/database.yml" def self.configure_from_file!(path) raise "#{path} does not exist!" unless File.file? path result = load_yaml(path) ::ActiveRecord::Base.configurations = begin result.each do |_env, config| if config.all? { |_, v| v.is_a?(Hash) } config.each { |_, v| append_migration_path(v) } else append_migration_path(config) end end end end # Establish a connection to the given db (defaults to current rack env) def self.establish_connection!(db = rack_env) ::ActiveRecord::Base.establish_connection(db) end def self.append_migration_path(config) config['migrations_paths'] = ::OTR::ActiveRecord.migrations_paths unless config.key?('migrations_paths') config end # The current Rack environment def self.rack_env (ENV['RACK_ENV'] || ENV['RAILS_ENV'] || ENV['APP_ENV'] || ENV['OTR_ENV'] || 'development').to_sym end # Support old Psych versions def self.load_yaml(path) erb_result = ERB.new(File.read(path)).result result = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1') YAML.safe_load(erb_result, aliases: true) else YAML.safe_load(erb_result, [], [], true) end result || {} end private_class_method :load_yaml end end otr-activerecord-2.1.1/lib/otr-activerecord/compatibility_4.rb000066400000000000000000000014521417555416000244440ustar00rootroot00000000000000module OTR module ActiveRecord # Compatibility layer for ActiveRecord 4 class Compatibility4 attr_reader :major_version # Compatibility layer for ActiveRecord 4 def initialize @major_version = 4 ::ActiveRecord::Base.default_timezone = :utc end # All db migration dir paths def migrations_paths OTR::ActiveRecord.migrations_paths end # The dir in which to put new migrations def migrations_path OTR::ActiveRecord.migrations_paths[0] end # Basename of migration classes def migration_base_class_name 'ActiveRecord::Migration' end # Force RACK_ENV/RAILS_ENV to be 'test' when running any db:test:* tasks def force_db_test_env? true end end end end otr-activerecord-2.1.1/lib/otr-activerecord/compatibility_5.rb000066400000000000000000000015571417555416000244530ustar00rootroot00000000000000module OTR module ActiveRecord # Compatibility layer for ActiveRecord 5 class Compatibility5 attr_reader :major_version # Compatibility layer for ActiveRecord 5 def initialize @major_version = 5 ::ActiveRecord::Base.default_timezone = :utc end # All db migration dir paths def migrations_paths OTR::ActiveRecord.migrations_paths end # The dir in which to put new migrations def migrations_path OTR::ActiveRecord.migrations_paths[0] end # Basename of migration classes def migration_base_class_name version = "5.#{::ActiveRecord::VERSION::MINOR}" "ActiveRecord::Migration[#{version}]" end # Force RACK_ENV/RAILS_ENV to be 'test' when running any db:test:* tasks def force_db_test_env? false end end end end otr-activerecord-2.1.1/lib/otr-activerecord/compatibility_6.rb000066400000000000000000000015771417555416000244560ustar00rootroot00000000000000module OTR module ActiveRecord # Compatibility layer for ActiveRecord 6 class Compatibility6 attr_reader :major_version # Compatibility layer for ActiveRecord 6 def initialize @major_version = 6 ::ActiveRecord::Base.default_timezone = :utc end # All db migration dir paths def migrations_paths OTR::ActiveRecord.migrations_paths end # The dir in which to put new migrations def migrations_path OTR::ActiveRecord.migrations_paths[0] end # Basename of migration classes def migration_base_class_name version = "#{@major_version}.#{::ActiveRecord::VERSION::MINOR}" "ActiveRecord::Migration[#{version}]" end # Force RACK_ENV/RAILS_ENV to be 'test' when running any db:test:* tasks def force_db_test_env? false end end end end otr-activerecord-2.1.1/lib/otr-activerecord/compatibility_7.rb000066400000000000000000000003631417555416000244470ustar00rootroot00000000000000module OTR module ActiveRecord # Compatibility layer for ActiveRecord 7 class Compatibility7 < Compatibility6 def initialize @major_version = 7 ::ActiveRecord.default_timezone = :utc end end end end otr-activerecord-2.1.1/lib/otr-activerecord/defaults.rb000066400000000000000000000010011417555416000231450ustar00rootroot00000000000000ENV["DISABLE_DATABASE_ENVIRONMENT_CHECK"] ||= "true" OTR::ActiveRecord.db_dir = 'db' OTR::ActiveRecord.migrations_paths = %w(db/migrate) OTR::ActiveRecord.fixtures_path = 'test/fixtures' OTR::ActiveRecord.seed_file = 'seeds.rb' OTR::ActiveRecord._normalizer = case ::ActiveRecord::VERSION::MAJOR when 4 then OTR::ActiveRecord::Compatibility4.new when 5 then OTR::ActiveRecord::Compatibility5.new when 6 then OTR::ActiveRecord::Compatibility6.new when 7 then OTR::ActiveRecord::Compatibility7.new end otr-activerecord-2.1.1/lib/otr-activerecord/middleware/000077500000000000000000000000001417555416000231365ustar00rootroot00000000000000otr-activerecord-2.1.1/lib/otr-activerecord/middleware/connection_management.rb000066400000000000000000000011501417555416000300130ustar00rootroot00000000000000module OTR module ActiveRecord # # Rack middleware that returns active db connections to the connection pool after a request completes. # class ConnectionManagement def initialize(app) @app = app end def call(env) testing = env['rack.test'] == true resp = @app.call env resp[2] = ::Rack::BodyProxy.new resp[2] do ::ActiveRecord::Base.clear_active_connections! unless testing end resp rescue Exception ::ActiveRecord::Base.clear_active_connections! unless testing raise end end end end otr-activerecord-2.1.1/lib/otr-activerecord/middleware/query_cache.rb000066400000000000000000000014161417555416000257550ustar00rootroot00000000000000module OTR module ActiveRecord # # Rack middleware to enable ActiveRecord's query cache for each request. # class QueryCache def initialize(app) @handler = case ::ActiveRecord::VERSION::MAJOR when 4 then ::ActiveRecord::QueryCache.new(app) when 5, 6, 7 then ActionDispatchHandler.new(app) end end def call(env) @handler.call(env) end class ActionDispatchHandler def initialize(app) @app = app end def call(env) state = nil state = ::ActiveRecord::QueryCache.run @app.call(env) ensure ::ActiveRecord::QueryCache.complete(state) if state end end end end end otr-activerecord-2.1.1/lib/otr-activerecord/version.rb000066400000000000000000000001231417555416000230270ustar00rootroot00000000000000module OTR module ActiveRecord # Gem version VERSION = '2.1.1' end end otr-activerecord-2.1.1/lib/tasks/000077500000000000000000000000001417555416000166725ustar00rootroot00000000000000otr-activerecord-2.1.1/lib/tasks/otr-activerecord.rake000066400000000000000000000050311417555416000230110ustar00rootroot00000000000000require 'pathname' require 'fileutils' require 'active_support/core_ext/string/strip' require 'active_support/core_ext/string/inflections' require 'otr-activerecord' load 'active_record/railties/databases.rake' # # Configure and override the default activerecord db rake tasks # Rake::Task.define_task('db:_load_config') do ::ActiveRecord::Base.logger = nil ::ActiveRecord::Tasks::DatabaseTasks.tap do |config| config.root = Rake.application.original_dir config.env = OTR::ActiveRecord.rack_env.to_s config.db_dir = OTR::ActiveRecord.db_dir config.migrations_paths = Array(OTR::ActiveRecord.migrations_paths) config.fixtures_path = OTR::ActiveRecord.fixtures_path config.database_configuration = ::ActiveRecord::Base.configurations config.seed_loader = Object.new config.seed_loader.instance_eval do def load_seed load "#{OTR::ActiveRecord.db_dir}/#{OTR::ActiveRecord.seed_file}" end end end end Rake::Task['db:load_config'].clear Rake::Task.define_task('db:load_config') do # Run the user's db:environment task first, so they have an opportunity to set a custom db config location Rake::Task['db:environment'].invoke end Rake::Task.define_task('db:environment') do # defined by user end # Load db config at the end of user-defined db:environment Rake::Task['db:environment'].enhance do Rake::Task['db:_load_config'].invoke end Rake::Task['db:test:deprecated'].clear if Rake::Task.task_defined?('db:test:deprecated') # # Define otr-activerecord helper tasks # namespace :db do namespace :test do task :environment do ENV['RACK_ENV'] = 'test' end end if OTR::ActiveRecord._normalizer.force_db_test_env? desc "Create a migration" task :create_migration, [:name] do |_, args| name, version = args[:name], Time.now.utc.strftime("%Y%m%d%H%M%S") OTR::ActiveRecord._normalizer.migrations_paths.each do |directory| next unless File.exists?(directory) migration_files = Pathname(directory).children if duplicate = migration_files.find { |path| path.basename.to_s.include?(name) } abort "Another migration is already named \"#{name}\": #{duplicate}." end end filename = "#{version}_#{name}.rb" dirname = OTR::ActiveRecord._normalizer.migrations_path path = File.join(dirname, filename) FileUtils.mkdir_p(dirname) File.write path, <<-MIGRATION.strip_heredoc class #{name.camelize} < #{OTR::ActiveRecord._normalizer.migration_base_class_name} def change end end MIGRATION puts path end end otr-activerecord-2.1.1/otr-activerecord.gemspec000066400000000000000000000015651417555416000216270ustar00rootroot00000000000000# encoding: utf-8 require File.expand_path('../lib/otr-activerecord/version.rb', __FILE__) Gem::Specification.new do |gem| gem.name = 'otr-activerecord' gem.version = OTR::ActiveRecord::VERSION gem.date = '2022-01-30' gem.description = 'Off The Rails ActiveRecord: Use ActiveRecord with Grape, Sinatra, Rack, or anything else! Formerly known as \'grape-activerecord\'.' gem.summary = 'Off The Rails: Use ActiveRecord with Grape, Sinatra, Rack, or anything else!' gem.homepage = 'https://github.com/jhollinger/otr-activerecord' gem.authors = ['Jordan Hollinger'] gem.email = 'jordan.hollinger@gmail.com' gem.license = 'MIT' gem.files = Dir['lib/**/**'] + ['README.md', 'LICENSE'] gem.required_ruby_version = '>= 2.1.0' gem.add_runtime_dependency 'activerecord', ['>= 4.0', '< 7.1'] gem.add_runtime_dependency 'hashie-forbidden_attributes', '~> 0.1' end otr-activerecord-2.1.1/spec/000077500000000000000000000000001417555416000157315ustar00rootroot00000000000000otr-activerecord-2.1.1/spec/fixtures/000077500000000000000000000000001417555416000176025ustar00rootroot00000000000000otr-activerecord-2.1.1/spec/fixtures/multi.yml000066400000000000000000000005141417555416000214570ustar00rootroot00000000000000.defaults: &defaults adapter: sqlite3 database: tmp/multi.sqlite3 development: primary: <<: *defaults reading: <<: *defaults replica: true production: primary: <<: *defaults reading: <<: *defaults replica: true test: primary: <<: *defaults reading: <<: *defaults replica: true otr-activerecord-2.1.1/spec/fixtures/simple.yml000066400000000000000000000002311417555416000216120ustar00rootroot00000000000000.defaults: &defaults adapter: sqlite3 database: tmp/simple.sqlite3 development: <<: *defaults production: <<: *defaults test: <<: *defaults otr-activerecord-2.1.1/spec/otr-activerecord/000077500000000000000000000000001417555416000212055ustar00rootroot00000000000000otr-activerecord-2.1.1/spec/otr-activerecord/activerecord_spec.rb000066400000000000000000000020231417555416000252130ustar00rootroot00000000000000RSpec.describe OTR::ActiveRecord do describe '.configure_from_file!' do context 'when simple configuration file is given' do let(:config) { Bundler.root.join('spec/fixtures/simple.yml') } it 'configures active record' do described_class.configure_from_file!(config) t = ::ActiveRecord::Base.configurations['test'].with_indifferent_access expect(t[:adapter]).to eq 'sqlite3' expect(t[:database]).to eq 'tmp/simple.sqlite3' expect(t[:migrations_paths]).to eq ['db/migrate'] end end context 'when configuration file with multiple roles given' do let(:config) { Bundler.root.join('spec/fixtures/multi.yml') } it 'configures active record' do described_class.configure_from_file!(config) t = ::ActiveRecord::Base.configurations['test'].with_indifferent_access expect(t[:adapter]).to eq 'sqlite3' expect(t[:database]).to eq 'tmp/multi.sqlite3' expect(t[:migrations_paths]).to eq ['db/migrate'] end end end end otr-activerecord-2.1.1/spec/spec_helper.rb000066400000000000000000000002171417555416000205470ustar00rootroot00000000000000require 'otr-activerecord' RSpec.configure do |config| config.color = true config.tty = true config.filter_run_when_matching :focus end