Class: ROM::Rails::ActiveRecord::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/rails/active_record/configuration.rb

Overview

A helper class to derive ‘rom-sql` configuration from ActiveRecord.

Constant Summary collapse

BASE_OPTIONS =
[
  :root,
  :adapter,
  :database,
  :password,
  :username,
  :hostname,
  :host
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: ::Rails.env, root: ::Rails.root, configurations: ::ActiveRecord::Base.configurations) ⇒ Configuration

Returns a new instance of Configuration.



25
26
27
28
29
30
31
# File 'lib/rom/rails/active_record/configuration.rb', line 25

def initialize(env: ::Rails.env, root: ::Rails.root, configurations: ::ActiveRecord::Base.configurations)
  @configurations = configurations
  @env  = env
  @root = root

  @uri_builder = ROM::Rails::ActiveRecord::UriBuilder.new
end

Instance Attribute Details

#configurationsObject (readonly)

Returns the value of attribute configurations.



20
21
22
# File 'lib/rom/rails/active_record/configuration.rb', line 20

def configurations
  @configurations
end

#envObject (readonly)

Returns the value of attribute env.



21
22
23
# File 'lib/rom/rails/active_record/configuration.rb', line 21

def env
  @env
end

#rootObject (readonly)

Returns the value of attribute root.



22
23
24
# File 'lib/rom/rails/active_record/configuration.rb', line 22

def root
  @root
end

#uri_builderObject (readonly)

Returns the value of attribute uri_builder.



23
24
25
# File 'lib/rom/rails/active_record/configuration.rb', line 23

def uri_builder
  @uri_builder
end

Instance Method Details

#build(config) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds a configuration hash from a flat database config hash.

This is used to support typical database.yml-complaint configs. It also uses adapter interface for things that are adapter-specific like handling schema naming.

Parameters:

  • (Hash, String)

Returns:

  • (Hash)


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rom/rails/active_record/configuration.rb', line 66

def build(config)
  adapter = config.fetch(:adapter)
  uri_options = config.except(:adapter).merge(
    root: root,
    scheme: adapter
  )
  other_options = config.except(*BASE_OPTIONS)

  uri = uri_builder.build(adapter, uri_options)
  { uri: uri, options: other_options }
end

#callObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This relies on ActiveRecord being initialized already.

Returns gateway configuration for the current environment.

Parameters:

  • (Rails::Application)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rom/rails/active_record/configuration.rb', line 39

def call
  specs = {}

  configurations.configs_for(env_name: env).each do |config|
    if config.respond_to?(:configuration_hash)
      name, hash = [config.name, config.configuration_hash]
    else  # Rails 6.0
      name, hash = [config.spec_name, config.config]
    end

    specs[:default] ||= hash
    specs[name.to_sym] = hash
  end

  specs.transform_values { |hash| build hash.symbolize_keys }
end