Class: Deimos::ActiveRecordConsume::BatchRecordList

Inherits:
Object
  • Object
show all
Defined in:
lib/deimos/active_record_consume/batch_record_list.rb

Overview

A set of BatchRecords which typically are worked with together (hence the batching!)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records) ⇒ BatchRecordList

Returns a new instance of BatchRecordList.

Parameters:



14
15
16
17
18
# File 'lib/deimos/active_record_consume/batch_record_list.rb', line 14

def initialize(records)
  self.batch_records = records
  self.klass = records.first&.klass
  self.bulk_import_column = records.first&.bulk_import_column&.to_sym
end

Instance Attribute Details

#batch_recordsArray<BatchRecord>

Returns:



8
9
10
# File 'lib/deimos/active_record_consume/batch_record_list.rb', line 8

def batch_records
  @batch_records
end

#bulk_import_columnObject

Returns the value of attribute bulk_import_column.



9
10
11
# File 'lib/deimos/active_record_consume/batch_record_list.rb', line 9

def bulk_import_column
  @bulk_import_column
end

#klassObject

Returns the value of attribute klass.



9
10
11
# File 'lib/deimos/active_record_consume/batch_record_list.rb', line 9

def klass
  @klass
end

Instance Method Details

#associationsArray<ActiveRecord::Reflection::AssociationReflection>

Get the list of relevant associations, based on the keys of the association hashes of all records in this list.

Returns:

  • (Array<ActiveRecord::Reflection::AssociationReflection>)


35
36
37
38
39
40
# File 'lib/deimos/active_record_consume/batch_record_list.rb', line 35

def associations
  return @associations if @associations

  keys = self.batch_records.map { |r| r.associations.keys }.flatten.uniq.map(&:to_sym)
  @associations = self.klass.reflect_on_all_associations.select { |assoc| keys.include?(assoc.name) }
end

#delete_old_records(assoc, import_id) ⇒ Object

Parameters:

  • assoc (ActiveRecord::Reflection::AssociationReflection)
  • import_id (String)


66
67
68
69
70
71
72
73
74
# File 'lib/deimos/active_record_consume/batch_record_list.rb', line 66

def delete_old_records(assoc, import_id)
  return if self.batch_records.none?

  primary_keys = self.primary_keys(assoc.name)
  assoc.klass.
    where(assoc.foreign_key => primary_keys).
    where("#{self.bulk_import_column} != ?", import_id).
    delete_all
end

#fill_primary_keys!Object

Go back to the DB and use the bulk_import_id to set the actual primary key (‘id`) of the records.



44
45
46
47
48
49
50
51
52
53
# File 'lib/deimos/active_record_consume/batch_record_list.rb', line 44

def fill_primary_keys!
  primary_col = self.klass.primary_key
  bulk_import_map = self.klass.
    where(self.bulk_import_column => self.batch_records.map(&:bulk_import_id)).
    select(primary_col, self.bulk_import_column).
    index_by(&self.bulk_import_column).to_h
  self.batch_records.each do |r|
    r.record[primary_col] = bulk_import_map[r.bulk_import_id][primary_col]
  end
end

#filter!(method) ⇒ Object

Filter out any invalid records.

Parameters:

  • method (Proc)


22
23
24
# File 'lib/deimos/active_record_consume/batch_record_list.rb', line 22

def filter!(method)
  self.batch_records.delete_if { |record| !method.call(record.record) }
end

#primary_keys(assoc_name) ⇒ Array<Integer,String>

Parameters:

  • assoc_name (String)

Returns:

  • (Array<Integer,String>)


57
58
59
60
61
62
# File 'lib/deimos/active_record_consume/batch_record_list.rb', line 57

def primary_keys(assoc_name)
  assoc = self.associations.find { |a| a.name == assoc_name }
  self.records.map do |record|
    record[assoc.active_record_primary_key]
  end
end

#recordsArray<ActiveRecord::Base>

Get the original ActiveRecord objects.

Returns:

  • (Array<ActiveRecord::Base>)


28
29
30
# File 'lib/deimos/active_record_consume/batch_record_list.rb', line 28

def records
  self.batch_records.map(&:record)
end