module ActiveType::RecordExtension::Inheritance::ClassMethods

Public Instance Methods

descends_from_active_record?() click to toggle source
# File lib/active_type/record_extension/inheritance.rb, line 63
def descends_from_active_record?
  extended_record_base_class.descends_from_active_record?
end
has_many(name, scope=nil, *args, &extension) click to toggle source
Calls superclass method
# File lib/active_type/record_extension/inheritance.rb, line 67
def has_many(name, scope=nil, *args, &extension)
  new_args, new_scope = Inheritance.add_foreign_key_option(extended_record_base_class, scope, *args)
  if ActiveRecord::VERSION::MAJOR <= 3 || new_scope.nil?
    super(name, **new_args, &extension)
  else
    super(name, new_scope, **new_args, &extension)
  end
end
has_one(name, scope=nil, *args, &extension) click to toggle source
Calls superclass method
# File lib/active_type/record_extension/inheritance.rb, line 76
def has_one(name, scope=nil, *args, &extension)
  new_args, new_scope = Inheritance.add_foreign_key_option(extended_record_base_class, scope, *args)
  if ActiveRecord::VERSION::MAJOR <= 3 || new_scope.nil?
    super(name, **new_args, &extension)
  else
    super(name, new_scope, **new_args, &extension)
  end
end
model_name() click to toggle source
# File lib/active_type/record_extension/inheritance.rb, line 30
def model_name
  @_model_name ||= begin
    if name
      # Namespace detection copied from ActiveModel::Naming
      namespace = module_ancestors.detect do |n|
        n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming?
      end
      # We create a Name object, with the sti class name, but self as the @klass reference
      # This way lookup_ancestors is invoked on the right class instead of the extended_record_base_class
      dup_model_name = ActiveModel::Name.new(self, namespace, sti_name)
      key = name.underscore.to_sym
      # We set the `i18n_key` to lookup on the derived class key
      # We keep the others the same to preserve parameter and route names
      dup_model_name.instance_variable_set(:@i18n_key, key)
      dup_model_name
    else # name is nil for the anonymous intermediate class
      extended_record_base_class.model_name
    end
  end
end
module_ancestors() click to toggle source
# File lib/active_type/record_extension/inheritance.rb, line 51
def module_ancestors
  if extended_record_base_class.respond_to?(:module_parents)
    extended_record_base_class.module_parents
  else
    extended_record_base_class.parents
  end
end
sti_name() click to toggle source
# File lib/active_type/record_extension/inheritance.rb, line 59
def sti_name
  extended_record_base_class.sti_name
end

Private Instance Methods

find_sti_class(type_name) click to toggle source
Calls superclass method
# File lib/active_type/record_extension/inheritance.rb, line 89
def find_sti_class(type_name)
  sti_class = super

  # Consider this class hierarchy
  # class Parent < ActiveRecord::Base; end
  # class Child < Parent; end
  # class ExtendedParent < ActiveType::Record[Parent]; end
  # class ExtendedChild < ActiveType::Record[Child]; end
  if self < sti_class
    # i.e. ExtendendChild.find(child.id)
    # => self = ExtendedChild; sti_class = Child
    # instantiate as ExtendedChild
    self
  elsif sti_class < extended_record_base_class
    # i.e. ExtendedParent.find(child.id)
    # => sti_class = Child; self = ExtendedParent; extended_record_base_class = Parent
    # There is no really good solution here, since we cannot instantiate as both ExtendedParent
    # and Child. We opt to instantiate as ExtendedParent, since the other option can be
    # achieved by using Parent.find(child.id)
    self
  else
    sti_class
  end
end