mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Extract common logic into a method
This commit is contained in:
parent
6af7192af5
commit
7293cac854
7 changed files with 34 additions and 28 deletions
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue