summaryrefslogtreecommitdiff
path: root/lib/tasks/assets.rake
blob: e778021e7ae60979208a892acacec7a563a3c656 (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
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?

    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

      asset.send(:generate_all_variants, original_path)
      puts "Regenerated variants for Asset##{asset.id} (#{asset.name})"
    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