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>)


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

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)


75
76
77
78
79
80
81
82
83
# File 'lib/deimos/active_record_consume/batch_record_list.rb', line 75

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.



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

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) ⇒ Array<BatchRecord>

Filter and return removed invalid batch records by the specified method

Parameters:

  • method (Proc)

Returns:



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/deimos/active_record_consume/batch_record_list.rb', line 23

def filter!(method)
  self.batch_records, invalid = self.batch_records.partition do |batch_record|
    case method.parameters.size
    when 2
      method.call(batch_record.record, batch_record.associations)
    else
      method.call(batch_record.record)
    end
  end
  invalid
end

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

Parameters:

  • assoc_name (String)

Returns:

  • (Array<Integer,String>)


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

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>)


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

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