module OTR::ActiveRecord
ActiveRecord
configuration module
Constants
- VERSION
Gem version
Attributes
_normalizer[RW]
Internal compatibility layer across different major versions of AR
db_dir[RW]
Relative path to the “db” dir
fixtures_path[RW]
Relative path to the fixtures directory
migrations_paths[RW]
Relative path(s) to the migrations directory
seed_file[RW]
Name of the seeds file in db_dir
Public Class Methods
configure_from_file!(path)
click to toggle source
Connect to database with a yml file. Example: “config/database.yml”
# File lib/otr-activerecord/activerecord.rb, line 58 def self.configure_from_file!(path) raise "#{path} does not exist!" unless File.file? path ::ActiveRecord::Base.configurations = (YAML.load(ERB.new(File.read(path)).result) || {}). reduce({}) { |a, (env, config)| if config.has_key? "database" a[env] = {"migrations_paths" => ::OTR::ActiveRecord.migrations_paths}.merge config elsif env == rack_env.to_s config.each do |dbname, subconfig| a[dbname.to_sym] = {"migrations_paths" => ::OTR::ActiveRecord.migrations_paths}.merge subconfig end end a } end
configure_from_hash!(spec)
click to toggle source
Connect to database with a Hash. Example: {adapter: 'postgresql', host: 'localhost', database: 'db', username: 'user', password: 'pass', encoding: 'utf8', pool: 10, timeout: 5000}
# File lib/otr-activerecord/activerecord.rb, line 26 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
configure_from_url!(url)
click to toggle source
Connect to database with a DB URL. Example: “postgres://user:pass@localhost/db”
# File lib/otr-activerecord/activerecord.rb, line 32 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
establish_connection!(db = rack_env)
click to toggle source
Establish a connection to the given db (defaults to current rack env)
# File lib/otr-activerecord/activerecord.rb, line 75 def self.establish_connection!(db = rack_env) ::ActiveRecord::Base.establish_connection(db) end
rack_env()
click to toggle source
The current Rack environment
# File lib/otr-activerecord/activerecord.rb, line 80 def self.rack_env (ENV['RACK_ENV'] || ENV['RAILS_ENV'] || ENV['APP_ENV'] || ENV['OTR_ENV'] || 'development').to_sym end