Class: OnlineMigrations::BackgroundDataMigrations::DeleteOrphanedRecords

Inherits:
DataMigration
  • Object
show all
Defined in:
lib/online_migrations/background_data_migrations/delete_orphaned_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, #count, named

Constructor Details

#initialize(model_name, associations, _options = {}) ⇒ DeleteOrphanedRecords

Returns a new instance of DeleteOrphanedRecords.



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

def initialize(model_name, associations, _options = {})
  @model = Object.const_get(model_name, false)
  @associations = associations.map(&:to_sym)
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



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

def associations
  @associations
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

Instance Method Details

#collectionObject



14
15
16
# File 'lib/online_migrations/background_data_migrations/delete_orphaned_records.rb', line 14

def collection
  model.unscoped.where.missing(*associations)
end

#process(record) ⇒ Object



18
19
20
# File 'lib/online_migrations/background_data_migrations/delete_orphaned_records.rb', line 18

def process(record)
  record.destroy
end