Class: DeleteYouLater::DestroyLaterJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/delete_you_later/destroy_later_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(model_name, model_id, assoc, scope: DeleteYouLater.configuration.scope, batch_size: DeleteYouLater.configuration.batch_size) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/delete_you_later/destroy_later_job.rb', line 3

def perform(model_name, model_id, assoc, scope: DeleteYouLater.configuration.scope, batch_size: DeleteYouLater.configuration.batch_size)
  model = model_name.constantize
  association = model.reflect_on_association(assoc.to_sym)
  dependent = association.klass
  foreign_key = association.foreign_key
  last_seen = 0
  loop do
    ids = association.klass.select(:id).where(foreign_key => model_id).where("id > ?", last_seen)
    if scope
      ids = ids.public_send(scope)
    end
    ids = ids.limit(batch_size).pluck(:id)
    break if ids.empty?
    last_seen = ids.last
    dependent.where(id: ids).find_each do |record|
      record.destroy!
    end
  end
end