Skip to content

Instantly share code, notes, and snippets.

@elight
Created July 23, 2012 22:21
Show Gist options
  • Save elight/3166609 to your computer and use it in GitHub Desktop.
Save elight/3166609 to your computer and use it in GitHub Desktop.
inheritance.rb
module ClassMethods
# True if this isn't a concrete subclass needing a STI type condition.
def descends_from_active_record?
if superclass.abstract_class?
superclass.descends_from_active_record?
else
superclass == Base || !columns_hash.include?(inheritance_column)
end
end
def finder_needs_type_condition? #:nodoc:
# This is like this because benchmarking justifies the strange :false stuff
:true == (@finder_needs_type_condition ||= descends_from_active_record? ? :false : :true)
end
def symbolized_base_class
@symbolized_base_class ||= base_class.to_s.to_sym
end
def symbolized_sti_name
@symbolized_sti_name ||= sti_name.present? ? sti_name.to_sym : symbolized_base_class
end
# Returns the base AR subclass that this class descends from. If A
# extends AR::Base, A.base_class will return A. If B descends from A
# through some arbitrarily deep hierarchy, B.base_class will return A.
#
# If B < A and C < B and if A is an abstract_class then both B.base_class
# and C.base_class would return B as the answer since A is an abstract_class.
def base_class
class_of_active_record_descendant(self)
end
#....
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment