Sha256: 7e06ee6e8af2174c3d2c4f166cd25aec081d0d0ded8b40a182ffdcc8449df6e1

Contents?: true

Size: 952 Bytes

Versions: 24

Compression:

Stored size: 952 Bytes

Contents

# frozen_string_literal: true

module Katalyst
  module Navigation
    module GarbageCollection
      extend ActiveSupport::Concern

      included do
        after_update :remove_stale_versions
      end

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

          next unless orphaned_versions.any?

          # find links 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 links 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
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
katalyst-navigation-1.8.4 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.8.3 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.8.2 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.8.1 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.8.0 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.6.0 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.5.2 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.5.1 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.5.0 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.4.1 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.4.0 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.3.4 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.3.3 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.3.2 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.3.1 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.3.0 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.2.0 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.1.2 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.1.1 app/models/concerns/katalyst/navigation/garbage_collection.rb
katalyst-navigation-1.1.0 app/models/concerns/katalyst/navigation/garbage_collection.rb