ActiveRecord and ActionPack now use the new descendants implementation.
This commit is contained in:
parent
a2b7fcb07c
commit
033e0a041f
|
@ -1,4 +1,5 @@
|
|||
require 'active_support/configurable'
|
||||
require 'active_support/descendants_tracker'
|
||||
require 'active_support/core_ext/module/anonymous'
|
||||
|
||||
module AbstractController
|
||||
|
@ -10,6 +11,7 @@ module AbstractController
|
|||
attr_internal :action_name
|
||||
|
||||
include ActiveSupport::Configurable
|
||||
extend ActiveSupport::DescendantsTracker
|
||||
|
||||
class << self
|
||||
attr_reader :abstract
|
||||
|
@ -21,17 +23,6 @@ module AbstractController
|
|||
@abstract = true
|
||||
end
|
||||
|
||||
def inherited(klass)
|
||||
::AbstractController::Base.descendants << klass.to_s
|
||||
super
|
||||
end
|
||||
|
||||
# A list of all descendents of AbstractController::Base. This is
|
||||
# useful for initializers which need to add behavior to all controllers.
|
||||
def descendants
|
||||
@descendants ||= []
|
||||
end
|
||||
|
||||
# A list of all internal methods for a controller. This finds the first
|
||||
# abstract superclass of a controller, and gets a list of all public
|
||||
# instance methods on that abstract class. Public instance methods of
|
||||
|
|
|
@ -2066,7 +2066,7 @@ module ActiveRecord
|
|||
unless klass.descends_from_active_record?
|
||||
sti_column = aliased_table[klass.inheritance_column]
|
||||
sti_condition = sti_column.eq(klass.sti_name)
|
||||
klass.send(:subclasses).each {|subclass| sti_condition = sti_condition.or(sti_column.eq(subclass.sti_name)) }
|
||||
klass.descendants.each {|subclass| sti_condition = sti_condition.or(sti_column.eq(subclass.sti_name)) }
|
||||
|
||||
@join << sti_condition
|
||||
end
|
||||
|
|
|
@ -2,6 +2,7 @@ require 'yaml'
|
|||
require 'set'
|
||||
require 'active_support/benchmarkable'
|
||||
require 'active_support/dependencies'
|
||||
require 'active_support/descendants_tracker'
|
||||
require 'active_support/time'
|
||||
require 'active_support/core_ext/class/attribute'
|
||||
require 'active_support/core_ext/class/attribute_accessors'
|
||||
|
@ -274,28 +275,6 @@ module ActiveRecord #:nodoc:
|
|||
# on to any new database connections made and which can be retrieved on both a class and instance level by calling +logger+.
|
||||
cattr_accessor :logger, :instance_writer => false
|
||||
|
||||
def self.inherited(child) #:nodoc:
|
||||
@@subclasses[self] ||= []
|
||||
@@subclasses[self] << child
|
||||
super
|
||||
end
|
||||
|
||||
def self.reset_subclasses #:nodoc:
|
||||
nonreloadables = []
|
||||
subclasses.each do |klass|
|
||||
unless ActiveSupport::Dependencies.autoloaded? klass
|
||||
nonreloadables << klass
|
||||
next
|
||||
end
|
||||
klass.instance_variables.each { |var| klass.send(:remove_instance_variable, var) }
|
||||
klass.instance_methods(false).each { |m| klass.send :undef_method, m }
|
||||
end
|
||||
@@subclasses = {}
|
||||
nonreloadables.each { |klass| (@@subclasses[klass.superclass] ||= []) << klass }
|
||||
end
|
||||
|
||||
@@subclasses = {}
|
||||
|
||||
##
|
||||
# :singleton-method:
|
||||
# Contains the database configuration - as is typically stored in config/database.yml -
|
||||
|
@ -810,7 +789,7 @@ module ActiveRecord #:nodoc:
|
|||
end
|
||||
|
||||
def reset_column_information_and_inheritable_attributes_for_all_subclasses#:nodoc:
|
||||
subclasses.each { |klass| klass.reset_inheritable_attributes; klass.reset_column_information }
|
||||
descendants.each { |klass| klass.reset_inheritable_attributes; klass.reset_column_information }
|
||||
end
|
||||
|
||||
def attribute_method?(attribute)
|
||||
|
@ -975,7 +954,7 @@ module ActiveRecord #:nodoc:
|
|||
def type_condition
|
||||
sti_column = arel_table[inheritance_column]
|
||||
condition = sti_column.eq(sti_name)
|
||||
subclasses.each{|subclass| condition = condition.or(sti_column.eq(subclass.sti_name)) }
|
||||
descendants.each { |subclass| condition = condition.or(sti_column.eq(subclass.sti_name)) }
|
||||
|
||||
condition
|
||||
end
|
||||
|
@ -1165,14 +1144,6 @@ module ActiveRecord #:nodoc:
|
|||
with_scope(method_scoping, :overwrite, &block)
|
||||
end
|
||||
|
||||
# Returns a list of all subclasses of this class, meaning all descendants.
|
||||
def subclasses
|
||||
@@subclasses[self] ||= []
|
||||
@@subclasses[self] + @@subclasses[self].inject([]) {|list, subclass| list + subclass.subclasses }
|
||||
end
|
||||
|
||||
public :subclasses
|
||||
|
||||
# Sets the default options for the model. The format of the
|
||||
# <tt>options</tt> argument is the same as in find.
|
||||
#
|
||||
|
@ -1900,6 +1871,7 @@ module ActiveRecord #:nodoc:
|
|||
extend ActiveModel::Naming
|
||||
extend QueryCache::ClassMethods
|
||||
extend ActiveSupport::Benchmarkable
|
||||
extend ActiveSupport::DescendantsTracker
|
||||
|
||||
include ActiveModel::Conversion
|
||||
include Validations
|
||||
|
|
|
@ -105,8 +105,9 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
protected
|
||||
|
||||
def observed_subclasses
|
||||
observed_classes.sum([]) { |klass| klass.send(:subclasses) }
|
||||
observed_classes.sum([]) { |klass| klass.send(:descendants) }
|
||||
end
|
||||
|
||||
def observe_callbacks?
|
||||
|
|
|
@ -2076,10 +2076,6 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
assert !SubStiPost.descends_from_active_record?
|
||||
end
|
||||
|
||||
def test_base_subclasses_is_public_method
|
||||
assert ActiveRecord::Base.public_methods.map(&:to_sym).include?(:subclasses)
|
||||
end
|
||||
|
||||
def test_find_on_abstract_base_class_doesnt_use_type_condition
|
||||
old_class = LooseDescendant
|
||||
Object.send :remove_const, :LooseDescendant
|
||||
|
|
Loading…
Reference in New Issue