Module: InheritanceIntegerType::Extensions

Included in:
ActiveRecord::Base
Defined in:
lib/inheritance_integer_type/extensions.rb

Instance Method Summary collapse

Instance Method Details

#_inheritance_mappingObject



37
38
39
# File 'lib/inheritance_integer_type/extensions.rb', line 37

def _inheritance_mapping
  @_inheritance_mapping ||= (superclass == ActiveRecord::Base ? {} : superclass._inheritance_mapping.dup)
end

#_inheritance_mapping=(val) ⇒ Object



41
42
43
# File 'lib/inheritance_integer_type/extensions.rb', line 41

def _inheritance_mapping=(val)
  @_inheritance_mapping = val
end

#find_sti_class(type_name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/inheritance_integer_type/extensions.rb', line 8

def find_sti_class(type_name)
  lookup = self._inheritance_mapping[type_name.to_i]
  if lookup
    if ActiveRecord::VERSION::MAJOR < 5
      super(lookup)
    else
      begin
        if store_full_sti_class
          lookup.constantize
        else
          compute_type(lookup)
        end
      rescue NameError
        raise SubclassNotFound,
          "The single-table inheritance mechanism failed to locate the subclass: '#{type_name}'. " +
          "This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " +
          "Please rename this column if you didn't intend it to be used for storing the inheritance class " +
          "or overwrite #{name}.inheritance_column to use another column for that information."
      end
    end
  else
    super
  end
end

#integer_inheritance=(val) ⇒ Object



33
34
35
# File 'lib/inheritance_integer_type/extensions.rb', line 33

def integer_inheritance=(val)
  self._inheritance_mapping[val] = method(:sti_name).super_method.call
end

#merge_mapping!(mapping) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
# File 'lib/inheritance_integer_type/extensions.rb', line 45

def merge_mapping!(mapping)
  conflicts = _inheritance_mapping.keys & mapping.keys
  raise ArgumentError.new("Duplicate mapping detected for keys: #{conflicts}") if conflicts.any?

  _inheritance_mapping.merge!(mapping)
end

#sti_nameObject



3
4
5
6
# File 'lib/inheritance_integer_type/extensions.rb', line 3

def sti_name
  klass = super
  self._inheritance_mapping.key(klass) || klass
end