-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script to populate hero images from scoped resources banner images
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
lib/tasks/upgrade/migrate_hero_image_from_banner_image.rake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |