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)


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rom/rails/active_record/configuration.rb', line 69

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
# File 'lib/rom/rails/active_record/configuration.rb', line 39

def call
  specs = { default: build(default_configuration.symbolize_keys) }

  if rails6?
    configurations.configs_for(env_name: env).each do |config|
      specs[config.spec_name.to_sym] = build(config.config.symbolize_keys)
    end
  end

  specs
end

#default_configurationObject



51
52
53
54
55
56
57
# File 'lib/rom/rails/active_record/configuration.rb', line 51

def default_configuration
  if rails6?
    configurations.default_hash(env)
  else
    configurations.fetch(env)
  end
end