Sha256: 52e171ab2881d051219a30811aff6cbef0099e8a137549069e802ae2c3e60206

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require "active_record"
require "active_support/concern"
require "active_support/core_ext/module/aliasing"

module Replicat
  module Replicable
    extend ActiveSupport::Concern

    included do
      class << self
        def proxy
          @proxy ||= Proxy.new(self)
        end

        alias_method_chain :connection, :proxy

        attr_accessor :connection_name
      end
    end

    module ClassMethods
      def connection_with_proxy
        if has_any_replication?
          proxy
        else
          connection_without_proxy
        end
      end

      def has_any_replication?
        has_configuration? && replications.present?
      end

      def has_configuration?
        !!configuration
      end

      def configuration
        connection_name && configurations[connection_name]
      end

      def replications
        configuration["replications"]
      end

      def using(connection_name)
        if block_given?
          proxy.current_connection_name = connection_name
          yield
        else
          ScopeProxy.new(klass: self, connection_name: connection_name)
        end
      ensure
        proxy.current_connection_name = nil if block_given?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
replicat-0.0.2 lib/replicat/replicable.rb