Module: OTR::ActiveRecord
- Defined in:
- lib/otr-activerecord/activerecord.rb,
lib/otr-activerecord/shim/v6.rb,
lib/otr-activerecord/shim/v7.rb,
lib/otr-activerecord/shim/v8.rb,
lib/otr-activerecord/version.rb,
lib/otr-activerecord/shim/base.rb,
lib/otr-activerecord/middleware/query_cache.rb,
lib/otr-activerecord/middleware/connection_management.rb
Overview
ActiveRecord configuration module
Defined Under Namespace
Classes: ConnectionManagement, QueryCache, Shim, ShimBase
Constant Summary collapse
- VERSION =
Gem version
'2.5.0'
Class Attribute Summary collapse
-
.db_dir ⇒ Object
Relative path to the “db” dir.
-
.fixtures_path ⇒ Object
Relative path to the fixtures directory.
-
.migrations_paths ⇒ Object
Relative path(s) to the migrations directory.
-
.seed_file ⇒ Object
Name of the seeds file in db_dir.
-
.shim ⇒ Object
Internal compatibility layer across different major versions of AR.
Class Method Summary collapse
-
.configure_from_file!(path) ⇒ Object
Connect to database with a yml file.
-
.configure_from_hash!(spec) ⇒ Object
Connect to database with a Hash.
-
.configure_from_url!(url) ⇒ Object
Connect to database with a DB URL.
-
.establish_connection!(db = rack_env) ⇒ Object
Establish a connection to the given db (defaults to current rack env).
-
.rack_env ⇒ Object
The current Rack environment.
Class Attribute Details
.db_dir ⇒ Object
Relative path to the “db” dir
15 16 17 |
# File 'lib/otr-activerecord/activerecord.rb', line 15 def db_dir @db_dir end |
.fixtures_path ⇒ Object
Relative path to the fixtures directory
19 20 21 |
# File 'lib/otr-activerecord/activerecord.rb', line 19 def fixtures_path @fixtures_path end |
.migrations_paths ⇒ Object
Relative path(s) to the migrations directory
17 18 19 |
# File 'lib/otr-activerecord/activerecord.rb', line 17 def migrations_paths @migrations_paths end |
.seed_file ⇒ Object
Name of the seeds file in db_dir
21 22 23 |
# File 'lib/otr-activerecord/activerecord.rb', line 21 def seed_file @seed_file end |
.shim ⇒ Object
Internal compatibility layer across different major versions of AR
23 24 25 |
# File 'lib/otr-activerecord/activerecord.rb', line 23 def shim @shim end |
Class Method Details
.configure_from_file!(path) ⇒ Object
Connect to database with a yml file. Example: “config/database.yml”
58 59 60 61 62 |
# File 'lib/otr-activerecord/activerecord.rb', line 58 def self.configure_from_file!(path) yaml = ERB.new(File.read(path)).result spec = YAML.safe_load(yaml, aliases: true) || {} ::ActiveRecord::Base.configurations = transform_config spec end |
.configure_from_hash!(spec) ⇒ Object
Connect to database with a Hash. Example: ‘postgresql’, host: ‘localhost’, database: ‘db’, username: ‘user’, password: ‘pass’, encoding: ‘utf8’, pool: 10, timeout: 5000
28 29 30 |
# File 'lib/otr-activerecord/activerecord.rb', line 28 def self.configure_from_hash!(spec) ::ActiveRecord::Base.configurations = transform_config({rack_env.to_s => spec}) end |
.configure_from_url!(url) ⇒ Object
Connect to database with a DB URL. Example: “postgres://user:pass@localhost/db”
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/otr-activerecord/activerecord.rb', line 33 def self.configure_from_url!(url) 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 |
.establish_connection!(db = rack_env) ⇒ Object
Establish a connection to the given db (defaults to current rack env)
65 66 67 |
# File 'lib/otr-activerecord/activerecord.rb', line 65 def self.establish_connection!(db = rack_env) ::ActiveRecord::Base.establish_connection(db) end |
.rack_env ⇒ Object
The current Rack environment
70 71 72 |
# File 'lib/otr-activerecord/activerecord.rb', line 70 def self.rack_env (ENV['RACK_ENV'] || ENV['RAILS_ENV'] || ENV['APP_ENV'] || ENV['OTR_ENV'] || 'development').to_sym end |