Sha256: 0d8e2e8ece8ee56dd6b9bfb8b1fd85f10d8b27b7925c50fbfa6d90311958483e

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

require 'active_support/core_ext/class/attribute'
require 'fresh_connection/slave_connection_handler'
require 'fresh_connection/access_control'

module FreshConnection
  module Extend
    module ArBase
      def self.extended(base)
        base.class_attribute :slave_connection_handler, :instance_writer => false
        base.slave_connection_handler = FreshConnection::SlaveConnectionHandler.new
      end

      case ActiveRecord::VERSION::MAJOR
      when 4
        delegate :read_master, to: :all
      when 3
        delegate :read_master, to: :scoped
      end

      def manage_access(slave_access, &block)
        if master_db_only?
          FreshConnection::AccessControl.force_master_access(&block)
        else
          FreshConnection::AccessControl.access(slave_access, &block)
        end
      end

      def with_master(&block)
        manage_access(false, &block)
      end

      def establish_fresh_connection(slave_group = nil)
        slave_connection_handler.establish_connection(name, slave_group)
      end

      def slave_connection
        slave_connection_handler.connection(self)
      end

      def clear_all_slave_connections!
        slave_connection_handler.clear_all_connections!
      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 slave_connection_put_aside!
        slave_connection_handler.put_aside!
      end

      def recovery(failure_connection, exception)
        slave_connection_handler.recovery(self, failure_connection, exception)
      end

      def slave_group
        slave_connection_handler.slave_group(self)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fresh_connection-0.4.4 lib/fresh_connection/extend/ar_base.rb
fresh_connection-1.0.1 lib/fresh_connection/extend/ar_base.rb
fresh_connection-1.0.0 lib/fresh_connection/extend/ar_base.rb
fresh_connection-0.4.3 lib/fresh_connection/extend/ar_base.rb