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/version.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

Constant Summary collapse

VERSION =

Gem version

'2.4.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.db_dirObject

Relative path to the “db” dir



20
21
22
# File 'lib/otr-activerecord/activerecord.rb', line 20

def db_dir
  @db_dir
end

.fixtures_pathObject

Relative path to the fixtures directory



24
25
26
# File 'lib/otr-activerecord/activerecord.rb', line 24

def fixtures_path
  @fixtures_path
end

.migrations_pathsObject

Relative path(s) to the migrations directory



22
23
24
# File 'lib/otr-activerecord/activerecord.rb', line 22

def migrations_paths
  @migrations_paths
end

.seed_fileObject

Name of the seeds file in db_dir



26
27
28
# File 'lib/otr-activerecord/activerecord.rb', line 26

def seed_file
  @seed_file
end

.shimObject

Internal compatibility layer across different major versions of AR



28
29
30
# File 'lib/otr-activerecord/activerecord.rb', line 28

def shim
  @shim
end

Class Method Details

.configure_from_file!(path) ⇒ Object

Connect to database with a yml file. Example: “config/database.yml”



63
64
65
66
67
# File 'lib/otr-activerecord/activerecord.rb', line 63

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



33
34
35
# File 'lib/otr-activerecord/activerecord.rb', line 33

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”



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/otr-activerecord/activerecord.rb', line 38

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)



70
71
72
# File 'lib/otr-activerecord/activerecord.rb', line 70

def self.establish_connection!(db = rack_env)
  ::ActiveRecord::Base.establish_connection(db)
end

.rack_envObject

The current Rack environment



75
76
77
# File 'lib/otr-activerecord/activerecord.rb', line 75

def self.rack_env
  (ENV['RACK_ENV'] || ENV['RAILS_ENV'] || ENV['APP_ENV'] || ENV['OTR_ENV'] || 'development').to_sym
end