Class: OnlineMigrations::BackgroundDataMigrations::DeleteAssociatedRecords

Inherits:
DataMigration
  • Object
show all
Defined in:
lib/online_migrations/background_data_migrations/delete_associated_records.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DataMigration

#after_cancel, #after_complete, #after_pause, #after_resume, #after_start, #after_stop, #around_process, #build_enumerator, collection_batch_size, named

Constructor Details

#initialize(model_name, record_id, association, _options = {}) ⇒ DeleteAssociatedRecords

Returns a new instance of DeleteAssociatedRecords.



9
10
11
12
13
# File 'lib/online_migrations/background_data_migrations/delete_associated_records.rb', line 9

def initialize(model_name, record_id, association, _options = {})
  model = Object.const_get(model_name, false)
  @record = model.find(record_id)
  @association = association
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



7
8
9
# File 'lib/online_migrations/background_data_migrations/delete_associated_records.rb', line 7

def association
  @association
end

#recordObject (readonly)

Returns the value of attribute record.



7
8
9
# File 'lib/online_migrations/background_data_migrations/delete_associated_records.rb', line 7

def record
  @record
end

Instance Method Details

#collectionObject



15
16
17
18
19
20
21
# File 'lib/online_migrations/background_data_migrations/delete_associated_records.rb', line 15

def collection
  if !@record.respond_to?(association)
    raise ArgumentError, "'#{@record.class.name}' has no association called '#{association}'"
  end

  record.public_send(association).in_batches(of: 100)
end

#countObject



27
28
29
# File 'lib/online_migrations/background_data_migrations/delete_associated_records.rb', line 27

def count
  record.public_send(association).count
end

#process(relation) ⇒ Object



23
24
25
# File 'lib/online_migrations/background_data_migrations/delete_associated_records.rb', line 23

def process(relation)
  relation.delete_all
end