Sha256: 9c269f2f0df243bdabfd8390875f991962c65c8771c59453fced1e8b5869518a

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

module ConnectionManager
  module Using
    class Proxy
      attr_accessor :klass, :connection_class

      def initialize(klass,connection_class)
        @klass = klass  # the @klass from an ActiveRecord::Relation
        @connection_class = (connection_class.is_a?(String) ? connection_class.constantize : connection_class)
      end

      # Use the connection from the connection class
      def connection
        ConnectionManager.logger.info "Using proxy connection: #{@connection_class.name} for #{@klass.name}" if ConnectionManager.logger
        @connection_class.connection
      end

      # Make sure we return the @klass superclass,
      # which used throughout the query building code in AR
      def superclass
        @klass.superclass
      end

      def >= compare
        return @klass >= compare.klass if compare.is_a?(self.class)
        @klass >= compare
      end

      def == compare
        return @klass == compare.klass if compare.is_a?(self.class)
        @klass == compare
      end

      def != compare
        return @klass != compare.klass if compare.is_a?(self.class)
        @klass != compare
      end

      def descendants
        @klass.descendants
      end

      def subclasses
        @klass.subclasses
      end

      def parent
        @klass.parent
      end

      # Pass all methods to @klass, this ensures objects
      # build from the query are the correct class and
      # any settings in the model like table_name_prefix
      # are used.
      def method_missing(name, *args, &blk)
        @klass.send(name, *args,&blk)
      end

      def respond_to?(method_name, include_private = false)
        @klass.respond_to?(method_name) || super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
connection_manager-1.1.0 lib/connection_manager/using.rb