Class: OnlineMigrations::BackgroundMigrations::DeleteAssociatedRecords

Inherits:
OnlineMigrations::BackgroundMigration show all
Defined in:
lib/online_migrations/background_migrations/delete_associated_records.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from OnlineMigrations::BackgroundMigration

#count, 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_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_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_migrations/delete_associated_records.rb', line 7

def record
  @record
end

Instance Method Details

#process_batch(relation) ⇒ Object



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

def process_batch(relation)
  relation.delete_all
end

#relationObject



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

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

  record.public_send(association)
end