lib/connection_manager/using.rb in connection_manager-0.3.8 vs lib/connection_manager/using.rb in connection_manager-0.3.9
- old
+ new
@@ -9,21 +9,26 @@
r
end
private
# We use dup here because its just too tricky to make sure we override
- # all the methods nesseccary when using a child class of the model. This
+ # all the methods necessary when using a child class of the model. This
# action is lazy and the created sub is named to a constant so we only
# have to do it once.
def fetch_duplicate_class(connection_class_name)
begin
return "#{self.name}::#{connection_class_name}Dup".constantize
rescue NameError
return build_dup_class(connection_class_name)
end
end
+ # Modifies the dup class to use the connection class connection.
+ # We want to use the current class table name, but the connection
+ # class database as the prefix. We also want the superclass method to
+ # return the connection class as AR sometimes uses the the superclass
+ # connection
def build_dup_class(connection_class_name)
con_class = connection_class_name.constantize
db_name = con_class.database_name
dup_klass = dup
dup_klass.class_eval <<-STR
@@ -32,46 +37,22 @@
self.table_name = '#{db_name}.#{table_name.split('.').last}'
class << self
def model_name
#{self.name}.model_name
end
+ def connection_class
+ #{connection_class_name}
+ end
+ def connection
+ connection_class.connection
+ end
+ def superclass
+ connection_class
+ end
end
STR
- extend_dup_class(dup_klass,connection_class_name)
self.const_set("#{connection_class_name}Dup", dup_klass)
- end
-
- # Extend the connection override module from the connetion to the supplied class
- def extend_dup_class(dup_class,connection_class_name)
- begin
- mod = "#{connection_class_name}::ConnectionOverrideMod".constantize
- dup_class.extend(mod)
- rescue NameError
- built = build_connection_override_module(connection_class_name).constantize
- dup_class.extend(built)
- end
- end
-
- # Added a module to the connection class. The module is extended on dup class
- # to override the connection and superclass
- def build_connection_override_module(connection_class_name)
- connection_class_name.constantize.class_eval <<-STR
- module ConnectionOverrideMod
- def connection_class
- #{connection_class_name}
- end
-
- def connection
- connection_class.connection
- end
-
- def superclass
- connection_class
- end
- end
- STR
- "#{connection_class_name}::ConnectionOverrideMod"
end
end
# Instance method for casting to a duplication class
def using(connection_class)