Sha256: eedc0c967cb737230d4bddd826ead61c2303dc64c4541522256a8e26f7468d21

Contents?: true

Size: 995 Bytes

Versions: 1

Compression:

Stored size: 995 Bytes

Contents

module Standby
  class ConnectionHolder < ActiveRecord::Base
    self.abstract_class = true

    class << self
      # for delayed activation
      def activate(target)
        env_name = "#{ActiveRecord::ConnectionHandling::RAILS_ENV.call}_#{target}"
        if Standby.version_gte?('7.0')
          spec = ActiveRecord::Base.configurations.find_db_config(env_name)&.configuration_hash
        else
          spec = ActiveRecord::Base.configurations[env_name]
        end
        raise Error, "Standby target '#{target}' is invalid!" if spec.nil?

        establish_connection spec
      end
    end
  end

  class << self
    def connection_holder(target)
      klass_name = "Standby#{target.to_s.camelize}ConnectionHolder"
      standby_connections[klass_name] ||= begin
        klass = Class.new(Standby::ConnectionHolder) do
          self.abstract_class = true
        end
        Object.const_set(klass_name, klass)
        klass.activate(target)
        klass
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
standby-5.0.0 lib/standby/connection_holder.rb