From 7293cac8548fb8083f718922d8d3ca9b3a18767f Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 23 Dec 2011 00:36:54 +0000 Subject: [PATCH] Extract common logic into a method --- .../lib/active_record/attribute_methods.rb | 2 +- .../abstract/connection_pool.rb | 7 +---- activerecord/lib/active_record/core.rb | 8 +---- activerecord/lib/active_record/inheritance.rb | 31 +++++++++++++------ .../lib/active_record/model_schema.rb | 10 +++--- .../test/cases/attribute_methods/read_test.rb | 1 + .../connection_handler_test.rb | 3 ++ 7 files changed, 34 insertions(+), 28 deletions(-) diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 54099d3196..7cb4326754 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -61,7 +61,7 @@ module ActiveRecord raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" end - if superclass == Base + if active_record_super == Base super else # If B < A and A defines its own attribute method, then we don't want to overwrite that. diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index f69a14f740..5749d45a18 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -371,12 +371,7 @@ connection. For example: ActiveRecord::Base.connection.close pool = @class_to_pool[klass.name] return pool if pool return nil if ActiveRecord::Base == klass - - if klass.superclass && klass.superclass < Model - retrieve_connection_pool klass.superclass - else - retrieve_connection_pool ActiveRecord::Base - end + retrieve_connection_pool klass.active_record_super end end diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 84ac6dd93d..0df62b7c62 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -116,13 +116,7 @@ module ActiveRecord if self == ActiveRecord::Base ActiveRecord::Base else - if connection_handler.connection_pools[name] - self - elsif superclass < ActiveRecord::Model - superclass.arel_engine - else - ActiveRecord::Base - end + connection_handler.connection_pools[name] ? self : active_record_super.arel_engine end end end diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb index 9b73c0d33c..ec57151d40 100644 --- a/activerecord/lib/active_record/inheritance.rb +++ b/activerecord/lib/active_record/inheritance.rb @@ -13,12 +13,12 @@ module ActiveRecord module ClassMethods # True if this isn't a concrete subclass needing a STI type condition. def descends_from_active_record? - if !(superclass < Model) - true - elsif superclass.abstract_class? - superclass.descends_from_active_record? + sup = active_record_super + + if sup.abstract_class? + sup.descends_from_active_record? else - superclass == Base || !columns_hash.include?(inheritance_column) + sup == Base || !columns_hash.include?(inheritance_column) end end @@ -81,6 +81,20 @@ module ActiveRecord instance end + # If this class includes ActiveRecord::Model then it won't have a + # superclass. So this provides a way to get to the 'root' (ActiveRecord::Base), + # through inheritance hierarchy, ending in Base, whether or not that is + # actually an ancestor of the class. + # + # Mainly for internal use. + def active_record_super #:nodoc: + if self == Base || superclass && superclass < Model::Tag + superclass + else + Base + end + end + protected # Returns the class descending directly from ActiveRecord::Base or an @@ -90,12 +104,11 @@ module ActiveRecord raise ActiveRecordError, "#{name} doesn't belong in a hierarchy descending from ActiveRecord" end - if klass == Base || klass.superclass == Base || - klass.superclass < Model::Tag && klass.superclass.abstract_class? || - !(klass.superclass < Model::Tag) + sup = klass.active_record_super + if klass == Base || sup == Base || sup.abstract_class? klass else - class_of_active_record_descendant(klass.superclass) + class_of_active_record_descendant(sup) end end diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb index 5fd0b12706..5754806cdd 100644 --- a/activerecord/lib/active_record/model_schema.rb +++ b/activerecord/lib/active_record/model_schema.rb @@ -128,10 +128,10 @@ module ActiveRecord # Computes the table name, (re)sets it internally, and returns it. def reset_table_name #:nodoc: - if (superclass < ActiveRecord::Model) && superclass.abstract_class? - self.table_name = superclass.table_name || compute_table_name + if active_record_super.abstract_class? + self.table_name = active_record_super.table_name || compute_table_name elsif abstract_class? - self.table_name = superclass == Base ? nil : superclass.table_name + self.table_name = active_record_super == Base ? nil : active_record_super.table_name else self.table_name = compute_table_name end @@ -143,10 +143,10 @@ module ActiveRecord # The name of the column containing the object's class when Single Table Inheritance is used def inheritance_column - if self == Base || !(superclass < Model) + if self == Base 'type' else - (@inheritance_column ||= nil) || superclass.inheritance_column + (@inheritance_column ||= nil) || active_record_super.inheritance_column end end diff --git a/activerecord/test/cases/attribute_methods/read_test.rb b/activerecord/test/cases/attribute_methods/read_test.rb index 7665f1c12e..0df9ffc0c5 100644 --- a/activerecord/test/cases/attribute_methods/read_test.rb +++ b/activerecord/test/cases/attribute_methods/read_test.rb @@ -15,6 +15,7 @@ module ActiveRecord def setup @klass = Class.new do def self.superclass; Base; end + def self.active_record_super; Base; end def self.base_class; self; end include ActiveRecord::AttributeMethods diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb index 04d543fea9..dc99ac665c 100644 --- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb +++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb @@ -8,6 +8,9 @@ module ActiveRecord @handler.establish_connection 'america', Base.connection_pool.spec @klass = Class.new do def self.name; 'america'; end + class << self + alias active_record_super superclass + end end @subklass = Class.new(@klass) do def self.name; 'north america'; end