Module: ActiveType::RecordExtension::Inheritance::ClassMethods

Defined in:
lib/active_type/record_extension/inheritance.rb

Instance Method Summary collapse

Instance Method Details

#descends_from_active_record?Boolean

Returns:

  • (Boolean)


63
64
65
# 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) ⇒ Object



67
68
69
70
71
72
73
74
# 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) ⇒ Object



76
77
78
79
80
81
82
83
# 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_nameObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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_ancestorsObject



51
52
53
54
55
56
57
# 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_nameObject



59
60
61
# File 'lib/active_type/record_extension/inheritance.rb', line 59

def sti_name
  extended_record_base_class.sti_name
end