module ActiveRecord::Configurations

Constants

VERSION

Attributes

configurations[R]
environments[R]

Public Class Methods

extended(child) click to toggle source
# File lib/active_record/configurations.rb, line 45
def self.extended(child)
        child.instance_variable_set(:@environments, {})
        child.instance_variable_set(:@configurations, {})
        
        # This is needed to explicitly override the behaviour provided by ActiveRecord::Core.
        def child.configurations
                instance_variable_get(:@configurations)
        end
        
        def child.configurations= value
                instance_variable_set(:@configurations, value)
        end
        
        # Remove the shitty "default"
        child.configurations.delete("default_env")
        
        # Add one that makes sense:
        child.configure(:default) do
                # The provided uri overrides other options.
                dsn ->{ENV[prefix.upcase + "_DSN"]}
                database ->{prefix + "_#{name}"}
                
                host 'localhost'
                prefix 'database'
        end
end

Public Instance Methods

configuration?(name) click to toggle source
# File lib/active_record/configurations.rb, line 119
def configuration? name
        self.configurations.include? name.to_s
end
configure(name, parent: :default, **options, &block) click to toggle source
# File lib/active_record/configurations.rb, line 137
def configure(name, parent: :default, **options, &block)
        parent = self.lookup_environment(parent)
        
        environment = Build::Environment.new(parent, {name: name.to_s}, name: name, **options, &block)
        
        self.environments[name] = environment
        
        configuration = environment.to_h
        
        if dsn = configuration[:dsn]
                resolve_database_dsn(dsn, configuration)
        end
        
        self.configurations[name.to_s] = configuration.stringify_keys
end
lookup_environment(name) click to toggle source
# File lib/active_record/configurations.rb, line 109
def lookup_environment(name)
        return nil unless name
        
        if environment = self.environments[name]
                environment
        # elsif self.superclass
        #    self.superclass.lookup_environment(name)
        end
end
requires_connection?(name) click to toggle source
# File lib/active_record/configurations.rb, line 123
def requires_connection? name
        if self.connected?
                return false
        end
        
        return self.configuration?(name)
end
resolve_database_dsn(string, environment) click to toggle source
# File lib/active_record/configurations.rb, line 75
def resolve_database_dsn(string, environment)
        uri = URI.parse(string)
        
        case uri.scheme
        when 'postgres'
                environment[:adapter] = 'postgresql'
        when 'mysql'
                environment[:adapter] = 'mysql2'
        else
                environment[:adapter] = uri.scheme
        end
        
        if username = uri.user
                environment[:username] = username
        end
        
        if password = uri.password
                environment[:password] = password
        end
        
        if host = uri.host
                environment[:host] = host
        end
        
        if port = uri.port
                environment[:port] = port
        end
        
        if path = uri.path
                # The path should always be relative:
                environment[:database] = path.sub(/^\//, '')
        end
end
setup_connection(name = DATABASE_ENV) click to toggle source
# File lib/active_record/configurations.rb, line 131
def setup_connection(name = DATABASE_ENV)
        if requires_connection?(name)
                self.establish_connection(name)
        end
end