about summary refs log tree commit diff
path: root/app/lib/vacuum/preview_cards_vacuum.rb
blob: 14fdeda1ca9f62a3db814bbbcd2e50b9f5eab45b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

class Vacuum::PreviewCardsVacuum
  TTL = 1.day.freeze

  def initialize(retention_period)
    @retention_period = retention_period
  end

  def perform
    vacuum_cached_images! if retention_period?
  end

  private

  def vacuum_cached_images!
    preview_cards_past_retention_period.find_each do |preview_card|
      preview_card.image.destroy
      preview_card.save
    end
  end

  def preview_cards_past_retention_period
    PreviewCard.cached.where(PreviewCard.arel_table[:updated_at].lt(@retention_period.ago))
  end

  def retention_period?
    @retention_period.present?
  end
end