Skip to content

Commit

Permalink
Script to populate hero images from scoped resources banner images
Browse files Browse the repository at this point in the history
  • Loading branch information
fblupi committed Sep 20, 2024
1 parent daa2702 commit f1c2596
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/tasks/upgrade/migrate_hero_image_from_banner_image.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

namespace :decidim do
namespace :upgrade do
namespace :content_blocks do
desc "Use the banner image of the scoped resource on the hero content block"
task :migrate_hero_image_from_banner_image do
logger = Logger.new($stdout)
content_blocks = Decidim::ContentBlock.where(manifest_name: "hero", scope_name: %w(participatory_process_homepage assembly_homepage))
content_blocks.each do |content_block|
content_block_attachment = content_block.attachments.find_by(name: "background_image")
resource_class = case content_block.scope_name
when "participatory_process_homepage"
Decidim::ParticipatoryProcess
when "assembly_homepage"
Decidim::Assembly
else
raise NotImplementedError
end
resource = resource_class.find(content_block.scoped_resource_id)

if content_block_attachment.attached?
logger.info("[SKIP] Content block #{content_block.id} from scoped resource #{resource_class} #{resource.id} already has a background image attachment")
next
end

unless resource.banner_image.attached?
logger.info("[SKIP] Content block #{content_block.id} scoped resource #{resource_class} #{resource.id} does not have a banner image")
next
end

logger.info("[INFO] Attaching image to content block #{content_block.id} from the banner image of the scoped resource #{resource_class} #{resource.id}")
content_block_attachment.file.attach(resource.banner_image.blob)
end
end
end
end
end

0 comments on commit f1c2596

Please sign in to comment.