Module: InventoryRefresh::InventoryCollection::Helpers::AssociationsHelper

Included in:
InventoryRefresh::InventoryCollection
Defined in:
lib/inventory_refresh/inventory_collection/helpers/associations_helper.rb

Instance Method Summary collapse

Instance Method Details

#association_to_base_class_mappingHash{Symbol => String}

Returns Hash with association name mapped to base class of the association.

Returns:

  • (Hash{Symbol => String})

    Hash with association name mapped to base class of the association



49
50
51
52
53
54
55
# File 'lib/inventory_refresh/inventory_collection/helpers/associations_helper.rb', line 49

def association_to_base_class_mapping
  return {} unless model_class

  @association_to_base_class_mapping ||= model_class.reflect_on_all_associations.each_with_object({}) do |x, obj|
    obj[x.name] = x.klass.base_class.name unless x.polymorphic?
  end
end

#association_to_foreign_key_mappingHash{Symbol => String}

Returns Hash with association name mapped to foreign key column name.

Returns:

  • (Hash{Symbol => String})

    Hash with association name mapped to foreign key column name



13
14
15
16
17
18
19
# File 'lib/inventory_refresh/inventory_collection/helpers/associations_helper.rb', line 13

def association_to_foreign_key_mapping
  return {} unless model_class

  @association_to_foreign_key_mapping ||= belongs_to_associations.each_with_object({}) do |x, obj|
    obj[x.name] = x.foreign_key
  end
end

#association_to_foreign_type_mappingHash{Symbol => String}

Returns Hash with association name mapped to polymorphic foreign key type column name.

Returns:

  • (Hash{Symbol => String})

    Hash with association name mapped to polymorphic foreign key type column name



31
32
33
34
35
36
37
# File 'lib/inventory_refresh/inventory_collection/helpers/associations_helper.rb', line 31

def association_to_foreign_type_mapping
  return {} unless model_class

  @association_to_foreign_type_mapping ||= model_class.reflect_on_all_associations.each_with_object({}) do |x, obj|
    obj[x.name] = x.foreign_type if x.polymorphic?
  end
end

#belongs_to_associationsArray<ActiveRecord::Reflection::BelongsToReflection">

Returns All belongs_to associations.

Returns:

  • (Array<ActiveRecord::Reflection::BelongsToReflection">)

    All belongs_to associations



8
9
10
# File 'lib/inventory_refresh/inventory_collection/helpers/associations_helper.rb', line 8

def belongs_to_associations
  model_class.reflect_on_all_associations.select { |x| x.kind_of?(ActiveRecord::Reflection::BelongsToReflection) }
end

#fixed_foreign_keysArray<Symbol>

Returns List of all column names that are foreign keys and cannot removed, otherwise we couldn’t save the record.

Returns:

  • (Array<Symbol>)

    List of all column names that are foreign keys and cannot removed, otherwise we couldn’t save the record



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/inventory_refresh/inventory_collection/helpers/associations_helper.rb', line 66

def fixed_foreign_keys
  # Foreign keys that are part of a manager_ref must be present, otherwise the record would get lost. This is a
  # minimum check we can do to not break a referential integrity.
  return @fixed_foreign_keys_cache unless @fixed_foreign_keys_cache.nil?

  manager_ref_set = (manager_ref - manager_ref_allowed_nil)
  @fixed_foreign_keys_cache = manager_ref_set.map { |x| association_to_foreign_key_mapping[x] }.compact
  @fixed_foreign_keys_cache += foreign_keys & manager_ref
  @fixed_foreign_keys_cache.map!(&:to_sym)
  @fixed_foreign_keys_cache
end

#foreign_key_to_association_mappingHash{String => Hash}

Returns Hash with foreign_key column name mapped to association name.

Returns:

  • (Hash{String => Hash})

    Hash with foreign_key column name mapped to association name



22
23
24
25
26
27
28
# File 'lib/inventory_refresh/inventory_collection/helpers/associations_helper.rb', line 22

def foreign_key_to_association_mapping
  return {} unless model_class

  @foreign_key_to_association_mapping ||= belongs_to_associations.each_with_object({}) do |x, obj|
    obj[x.foreign_key] = x.name
  end
end

#foreign_keysArray<Symbol>

Returns List of all column names that are foreign keys.

Returns:

  • (Array<Symbol>)

    List of all column names that are foreign keys



58
59
60
61
62
# File 'lib/inventory_refresh/inventory_collection/helpers/associations_helper.rb', line 58

def foreign_keys
  return [] unless model_class

  @foreign_keys_cache ||= belongs_to_associations.map(&:foreign_key).map!(&:to_sym)
end

#foreign_type_to_association_mappingHash{Symbol => String}

Returns Hash with polymorphic foreign key type column name mapped to association name.

Returns:

  • (Hash{Symbol => String})

    Hash with polymorphic foreign key type column name mapped to association name



40
41
42
43
44
45
46
# File 'lib/inventory_refresh/inventory_collection/helpers/associations_helper.rb', line 40

def foreign_type_to_association_mapping
  return {} unless model_class

  @foreign_type_to_association_mapping ||= model_class.reflect_on_all_associations.each_with_object({}) do |x, obj|
    obj[x.foreign_type] = x.name if x.polymorphic?
  end
end