Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extract common logic into a method

  • Loading branch information...
commit 7293cac8548fb8083f718922d8d3ca9b3a18767f 1 parent 6af7192
@jonleighton jonleighton authored
View
2  activerecord/lib/active_record/attribute_methods.rb
@@ -61,7 +61,7 @@ def instance_method_already_implemented?(method_name)
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.
View
7 activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -371,12 +371,7 @@ def retrieve_connection_pool(klass)
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
View
8 activerecord/lib/active_record/core.rb
@@ -116,13 +116,7 @@ def arel_engine
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
View
31 activerecord/lib/active_record/inheritance.rb
@@ -13,12 +13,12 @@ module Inheritance
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 @@ def instantiate(record)
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 @@ def class_of_active_record_descendant(klass)
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
View
10 activerecord/lib/active_record/model_schema.rb
@@ -128,10 +128,10 @@ def quoted_table_name
# 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 @@ def full_table_name_prefix #:nodoc:
# 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
View
1  activerecord/test/cases/attribute_methods/read_test.rb
@@ -15,6 +15,7 @@ def type; :integer; end
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
View
3  activerecord/test/cases/connection_adapters/connection_handler_test.rb
@@ -8,6 +8,9 @@ def setup
@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
Please sign in to comment.
Something went wrong with that request. Please try again.