Sha256: 5bda29aa37392ced40ad4a036739d55bf45615eb988a8cb17615102db25db739

Contents?: true

Size: 1.54 KB

Versions: 11

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true
require 'fresh_connection/access_control'
require 'fresh_connection/replica_connection_handler'

module FreshConnection
  module Extend
    module ArBase
      def read_master
        all.read_master
      end

      def with_master(&block)
        FreshConnection::AccessControl.manage_access(
          model: self,
          replica_access: false,
          &block
        )
      end

      def connection
        super.tap {|c| c.model_class = self }
      end

      def replica_connection
        __replica_handler.connection(replica_spec_name)
      end

      def clear_all_replica_connections!
        __replica_handler.clear_all_connections!
      end

      def establish_fresh_connection(spec_name = nil)
        spec_name = spec_name.to_s
        spec_name = "replica" if spec_name.empty?
        @_replica_spec_name = spec_name

        __replica_handler.refresh_connection(replica_spec_name)
      end

      def master_db_only!
        @_fresh_connection_master_only = true
      end

      def master_db_only?
        @_fresh_connection_master_only ||=
          (self != ActiveRecord::Base && superclass.master_db_only?)
      end

      def replica_spec_name
        @_replica_spec_name ||= __search_replica_spec_name
      end

      private

      def __search_replica_spec_name
        if self == ActiveRecord::Base
          "replica"
        else
          superclass.replica_spec_name
        end
      end

      def __replica_handler
        FreshConnection::ReplicaConnectionHandler.instance
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
fresh_connection-3.1.3 lib/fresh_connection/extend/ar_base.rb
fresh_connection-3.1.2 lib/fresh_connection/extend/ar_base.rb
fresh_connection-3.1.1 lib/fresh_connection/extend/ar_base.rb
fresh_connection-3.1.1.rc1 lib/fresh_connection/extend/ar_base.rb
fresh_connection-3.1.0 lib/fresh_connection/extend/ar_base.rb
fresh_connection-3.0.3 lib/fresh_connection/extend/ar_base.rb
fresh_connection-3.0.2 lib/fresh_connection/extend/ar_base.rb
fresh_connection-3.0.2.rc1 lib/fresh_connection/extend/ar_base.rb
fresh_connection-3.0.1 lib/fresh_connection/extend/ar_base.rb
fresh_connection-3.0.1.rc2 lib/fresh_connection/extend/ar_base.rb
fresh_connection-3.0.1.rc1 lib/fresh_connection/extend/ar_base.rb