class AbstractImporter::PolymorphicMapping

Attributes

collection[R]
foreign_key[R]
foreign_type[R]

Public Class Methods

new(collection, association) click to toggle source
# File lib/abstract_importer/polymorphic_mapping.rb, line 5
def initialize(collection, association)
  @collection = collection
  @foreign_key = association.foreign_key.to_sym
  @foreign_type = association.foreign_key.gsub(/_id$/, "_type").to_sym
  @table_name_by_foreign_model = Hash.new do |map, foreign_model|
    map[foreign_model] = foreign_model && foreign_model.constantize.table_name.to_sym
  end
end

Public Instance Methods

[](attrs)
Alias for: apply
applicable?(attrs) click to toggle source
# File lib/abstract_importer/polymorphic_mapping.rb, line 14
def applicable?(attrs)
  attrs.key?(foreign_key) && attrs.key?(foreign_type)
end
Also aliased as: applies_to?
applies_to?(attrs)
Alias for: applicable?
apply(attrs) click to toggle source
# File lib/abstract_importer/polymorphic_mapping.rb, line 27
def apply(attrs)
  depends_on = foreign_table_for(attrs)
  collection.map_foreign_key(attrs[foreign_key], foreign_key, depends_on) if depends_on
end
Also aliased as: []
foreign_model_for(attrs) click to toggle source
# File lib/abstract_importer/polymorphic_mapping.rb, line 19
def foreign_model_for(attrs)
  attrs[foreign_type]
end
foreign_table_for(attrs) click to toggle source
# File lib/abstract_importer/polymorphic_mapping.rb, line 23
def foreign_table_for(attrs)
  table_name_for(foreign_model_for(attrs))
end

Private Instance Methods

table_name_for(foreign_model) click to toggle source
# File lib/abstract_importer/polymorphic_mapping.rb, line 35
def table_name_for(foreign_model)
  @table_name_by_foreign_model[foreign_model]
end