Sha256: 56924a8bde9cd423ba72101cfee36415322470d6a8a9779590f99310c44b3893

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

module FreshConnection
  module Extend
    module ArRelation
      def self.included(base)
        base.alias_method_chain :exec_queries, :fresh_connection

        if FreshConnection.rails_4?
          base.__send__(:include, ForRails4)
        elsif FreshConnection.rails_3?
          base.__send__(:include, ForRails3)
        end
      end

      def calculate(operation, column_name, options = {})
        slave_access = enable_slave_access && options[:readonly] != false
        @klass.manage_access(slave_access) { super }
      end

      private

      def exec_queries_with_fresh_connection
        return @records if loaded?

        @klass.manage_access(enable_slave_access) do
          exec_queries_without_fresh_connection
        end
      end

      module ForRails4
        private
        def enable_slave_access
          connection.open_transactions == 0 && (readonly_value.nil? || readonly_value)
        end
      end

      module ForRails3
        private
        def enable_slave_access
          connection.open_transactions == 0 && (@readonly_value.nil? || @readonly_value)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fresh_connection-0.2.3 lib/fresh_connection/extend/ar_relation.rb
fresh_connection-0.2.2 lib/fresh_connection/extend/ar_relation.rb
fresh_connection-0.2.1 lib/fresh_connection/extend/ar_relation.rb
fresh_connection-0.2.0 lib/fresh_connection/extend/ar_relation.rb