Module: Katalyst::Content::GarbageCollection

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/katalyst/content/garbage_collection.rb

Instance Method Summary collapse

Instance Method Details

#remove_stale_versionsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/concerns/katalyst/content/garbage_collection.rb', line 12

def remove_stale_versions
  transaction do
    # find all the versions that are not linked to the record
    orphaned_versions = versions.inactive

    next unless orphaned_versions.any?

    # find items that are not included in active versions
    orphaned_items = items.pluck(:id) - versions.active.pluck(:nodes).flat_map { |k| k.map(&:id) }.uniq

    # delete orphaned items with a 2 hour grace period to allow for in-progress editing
    items.where(id: orphaned_items, updated_at: ..2.hours.ago).destroy_all

    # delete orphaned versions without a grace period as they can only be created by updates
    orphaned_versions.destroy_all
  end
end