summaryrefslogtreecommitdiff
path: root/lib/tasks/assets.rake
blob: 078d3c6334db46e916c96d434555c5c9ae382beb (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
namespace :assets do
  desc "Regenerate ImageMagick variants for every image asset from its stored original -- rerun whenever a new style is added to FileAttachment::STYLES after assets already exist. Scope to one asset first with ASSET_ID=123."
  task :regenerate_variants => :environment do
    scope = Asset.where(upload_content_type: FileAttachment::IMAGE_CONTENT_TYPES +
                                              FileAttachment::VECTOR_CONTENT_TYPES +
                                              FileAttachment::RASTERIZED_CONTENT_TYPES)
    scope = scope.where(id: ENV["ASSET_ID"]) if ENV["ASSET_ID"].present?

    skipped = []
    scope.find_each do |asset|
      original_path = asset.send(:file_path, :original)

      unless File.exist?(original_path)
        puts "Skipping Asset##{asset.id} (#{asset.name}) -- no original file at #{original_path}"
        next
      end

      if asset.send(:generate_all_variants, original_path)
        puts "Regenerated variants for Asset##{asset.id} (#{asset.name})"
      else
        skipped << asset.id
        puts "Skipped Asset##{asset.id} (#{asset.name}) -- source has no rasterisable page"
      end
    end

    if skipped.any?
      puts "\n#{skipped.size} asset(s) skipped, no variants written: #{skipped.join(", ")}"
    end
  end

desc "One-shot: flag each live page's current first-by-position image as its headline"
  task backfill_headline: :environment do
    count = 0
    page_ids = (Node.where.not(head_id: nil).pluck(:head_id) +
                Node.where.not(draft_id: nil).pluck(:draft_id)).uniq

    Page.where(id: page_ids).find_each do |page|
      next if page.related_assets.where(headline: true).exists?

      first_image = page.related_assets.joins(:asset).merge(Asset.images).order(:position).first
      next unless first_image

      first_image.update!(headline: true)
      count += 1
    end

    puts "Flagged #{count} pages' current first image as headline."
  end

end