Module: ActiveRecord::Prunable::ClassMethods

Defined in:
lib/active_record/prunable.rb

Instance Method Summary collapse

Instance Method Details

#batch_removal(batch_size = nil) ⇒ Object Also known as: remove_in_batches



67
68
69
70
# File 'lib/active_record/prunable.rb', line 67

def batch_removal(batch_size = nil)
  batch_size = 1000 unless batch_size.is_a?(Integer)
  class_variable_set(:@@prunable_batch_size, batch_size)
end

#prune!(*params, prune_method: nil, current_time: nil, batch_size: nil, in_batches: false) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/active_record/prunable.rb', line 49

def prune!(*params, prune_method: nil, current_time: nil, batch_size: nil, in_batches: false)
  logger.info "Pruning old records of #{self}"
  return false unless prunable_model?

  scope = resolve_scope(*params, current_time)
  batch_size ||= class_variable_get(:@@prunable_batch_size) if class_variable_defined?(:@@prunable_batch_size)
  batch_size ||= 1000 if in_batches
  destroyed_records = prune(scope, prune_method, batch_size)

  if destroyed_records.zero?
    logger.info 'Nothing to prune.'
  else
    logger.info "#{destroyed_records} records have been removed."
  end

  destroyed_records
end

#prune_created_after(duration, batch_removal: nil, remove_in_batches: nil) ⇒ Object Also known as: prune_after



33
34
35
36
37
38
# File 'lib/active_record/prunable.rb', line 33

def prune_created_after(duration, batch_removal: nil, remove_in_batches: nil)
  class_variable_set(:@@prune_created_after, duration)

  batch_removal_arg = batch_removal || remove_in_batches
  batch_removal(batch_removal_arg) if batch_removal_arg
end

#prune_method(method) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/active_record/prunable.rb', line 23

def prune_method(method)
  unless valid_prune_method?(method)
    logger.info "Incorrect prune method #{method} has been ignored for #{self}"
    return false
  end

  logger.info "Prune method #{method} has been set for #{self}"
  class_variable_set(:@@prune_method, method)
end

#prune_updated_after(duration, batch_removal: nil, remove_in_batches: nil) ⇒ Object



42
43
44
45
46
47
# File 'lib/active_record/prunable.rb', line 42

def prune_updated_after(duration, batch_removal: nil, remove_in_batches: nil)
  class_variable_set(:@@prune_updated_after, duration)

  batch_removal_arg = batch_removal || remove_in_batches
  batch_removal(batch_removal_arg) if batch_removal_arg
end