Class: AbstractImporter::Collection

Inherits:
Struct
  • Object
show all
Defined in:
lib/abstract_importer/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, scope, options = nil) ⇒ Collection

Returns a new instance of Collection.



4
5
6
# File 'lib/abstract_importer/collection.rb', line 4

def initialize(name, scope, options=nil)
  super name, scope.model, scope.table_name, scope, options
end

Instance Attribute Details

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



2
3
4
# File 'lib/abstract_importer/collection.rb', line 2

def model
  @model
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/abstract_importer/collection.rb', line 2

def name
  @name
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



2
3
4
# File 'lib/abstract_importer/collection.rb', line 2

def options
  @options
end

#scopeObject

Returns the value of attribute scope

Returns:

  • (Object)

    the current value of scope



2
3
4
# File 'lib/abstract_importer/collection.rb', line 2

def scope
  @scope
end

#table_nameObject

Returns the value of attribute table_name

Returns:

  • (Object)

    the current value of table_name



2
3
4
# File 'lib/abstract_importer/collection.rb', line 2

def table_name
  @table_name
end

Instance Method Details

#association_attrsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/abstract_importer/collection.rb', line 8

def association_attrs
  return @association_attrs if defined?(@association_attrs)

  # Instead of calling `tenant.people.build(__)`, we'll reflect on the
  # association to find its foreign key and its owner's id, so that we
  # can call `Person.new(__.merge(tenant_id: id))`.
  @association_attrs = {}
  association = scope.instance_variable_get(:@association)
  if association
    unless association.is_a?(ActiveRecord::Associations::HasManyThroughAssociation)
      @association_attrs.merge!(association.reflection.foreign_key.to_sym => association.owner.id)
    end
    if association.reflection.inverse_of && association.reflection.inverse_of.polymorphic?
      @association_attrs.merge!(association.reflection.inverse_of.foreign_type.to_sym => association.owner.class.name)
    end
  end
  @association_attrs.freeze
end

#has_legacy_id?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/abstract_importer/collection.rb', line 27

def has_legacy_id?
  @has_legacy_id ||= model.column_names.member?("legacy_id")
end