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"
|
raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord"
|
||||||
end
|
end
|
||||||
|
|
||||||
if superclass == Base
|
if active_record_super == Base
|
||||||
super
|
super
|
||||||
else
|
else
|
||||||
# If B < A and A defines its own attribute method, then we don't want to overwrite that.
|
# 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]
|
pool = @class_to_pool[klass.name]
|
||||||
return pool if pool
|
return pool if pool
|
||||||
return nil if ActiveRecord::Base == klass
|
return nil if ActiveRecord::Base == klass
|
||||||
|
retrieve_connection_pool klass.active_record_super
|
||||||
if klass.superclass && klass.superclass < Model
|
|
||||||
retrieve_connection_pool klass.superclass
|
|
||||||
else
|
|
||||||
retrieve_connection_pool ActiveRecord::Base
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -116,13 +116,7 @@ module ActiveRecord
|
||||||
if self == ActiveRecord::Base
|
if self == ActiveRecord::Base
|
||||||
ActiveRecord::Base
|
ActiveRecord::Base
|
||||||
else
|
else
|
||||||
if connection_handler.connection_pools[name]
|
connection_handler.connection_pools[name] ? self : active_record_super.arel_engine
|
||||||
self
|
|
||||||
elsif superclass < ActiveRecord::Model
|
|
||||||
superclass.arel_engine
|
|
||||||
else
|
|
||||||
ActiveRecord::Base
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,12 +13,12 @@ module ActiveRecord
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
# True if this isn't a concrete subclass needing a STI type condition.
|
# True if this isn't a concrete subclass needing a STI type condition.
|
||||||
def descends_from_active_record?
|
def descends_from_active_record?
|
||||||
if !(superclass < Model)
|
sup = active_record_super
|
||||||
true
|
|
||||||
elsif superclass.abstract_class?
|
if sup.abstract_class?
|
||||||
superclass.descends_from_active_record?
|
sup.descends_from_active_record?
|
||||||
else
|
else
|
||||||
superclass == Base || !columns_hash.include?(inheritance_column)
|
sup == Base || !columns_hash.include?(inheritance_column)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -81,6 +81,20 @@ module ActiveRecord
|
||||||
instance
|
instance
|
||||||
end
|
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
|
protected
|
||||||
|
|
||||||
# Returns the class descending directly from ActiveRecord::Base or an
|
# 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"
|
raise ActiveRecordError, "#{name} doesn't belong in a hierarchy descending from ActiveRecord"
|
||||||
end
|
end
|
||||||
|
|
||||||
if klass == Base || klass.superclass == Base ||
|
sup = klass.active_record_super
|
||||||
klass.superclass < Model::Tag && klass.superclass.abstract_class? ||
|
if klass == Base || sup == Base || sup.abstract_class?
|
||||||
!(klass.superclass < Model::Tag)
|
|
||||||
klass
|
klass
|
||||||
else
|
else
|
||||||
class_of_active_record_descendant(klass.superclass)
|
class_of_active_record_descendant(sup)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -128,10 +128,10 @@ module ActiveRecord
|
||||||
|
|
||||||
# Computes the table name, (re)sets it internally, and returns it.
|
# Computes the table name, (re)sets it internally, and returns it.
|
||||||
def reset_table_name #:nodoc:
|
def reset_table_name #:nodoc:
|
||||||
if (superclass < ActiveRecord::Model) && superclass.abstract_class?
|
if active_record_super.abstract_class?
|
||||||
self.table_name = superclass.table_name || compute_table_name
|
self.table_name = active_record_super.table_name || compute_table_name
|
||||||
elsif abstract_class?
|
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
|
else
|
||||||
self.table_name = compute_table_name
|
self.table_name = compute_table_name
|
||||||
end
|
end
|
||||||
|
@ -143,10 +143,10 @@ module ActiveRecord
|
||||||
|
|
||||||
# The name of the column containing the object's class when Single Table Inheritance is used
|
# The name of the column containing the object's class when Single Table Inheritance is used
|
||||||
def inheritance_column
|
def inheritance_column
|
||||||
if self == Base || !(superclass < Model)
|
if self == Base
|
||||||
'type'
|
'type'
|
||||||
else
|
else
|
||||||
(@inheritance_column ||= nil) || superclass.inheritance_column
|
(@inheritance_column ||= nil) || active_record_super.inheritance_column
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ module ActiveRecord
|
||||||
def setup
|
def setup
|
||||||
@klass = Class.new do
|
@klass = Class.new do
|
||||||
def self.superclass; Base; end
|
def self.superclass; Base; end
|
||||||
|
def self.active_record_super; Base; end
|
||||||
def self.base_class; self; end
|
def self.base_class; self; end
|
||||||
|
|
||||||
include ActiveRecord::AttributeMethods
|
include ActiveRecord::AttributeMethods
|
||||||
|
|
|
@ -8,6 +8,9 @@ module ActiveRecord
|
||||||
@handler.establish_connection 'america', Base.connection_pool.spec
|
@handler.establish_connection 'america', Base.connection_pool.spec
|
||||||
@klass = Class.new do
|
@klass = Class.new do
|
||||||
def self.name; 'america'; end
|
def self.name; 'america'; end
|
||||||
|
class << self
|
||||||
|
alias active_record_super superclass
|
||||||
|
end
|
||||||
end
|
end
|
||||||
@subklass = Class.new(@klass) do
|
@subklass = Class.new(@klass) do
|
||||||
def self.name; 'north america'; end
|
def self.name; 'north america'; end
|
||||||
|
|
Loading…
Reference in a new issue