lib/connection_manager/using.rb in connection_manager-1.0.4 vs lib/connection_manager/using.rb in connection_manager-1.1.0

- old
+ new

@@ -1,40 +1,54 @@ -require 'active_support/core_ext/module/delegation' module ConnectionManager module Using - module Relation - - # Specify connection class to used for query.For - # example: - # - # users = User.using(MySlaveConnection).first - def using(connection_class_name) - @klass = ConnectionManager::Using::Proxy.new(@klass,connection_class_name) - self - end - end - class Proxy attr_accessor :klass, :connection_class - + def initialize(klass,connection_class) - @klass = klass # the @klass insance from an ActiveRecord::Relation + @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 through the query building code in AR + # 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) @@ -42,16 +56,8 @@ end def respond_to?(method_name, include_private = false) @klass.respond_to?(method_name) || super end - end - end -end -ActiveRecord::Relation.send(:include,ConnectionManager::Using::Relation) -module ActiveRecord - class Base - class << self - delegate :using, :to => (ActiveRecord::VERSION::MAJOR == 4 ? :all : :scoped) end end end