Skip to content

Commit

Permalink
Expand images when presenting landing pages
Browse files Browse the repository at this point in the history
  • Loading branch information
richardTowers committed Nov 6, 2024
1 parent d2e55f0 commit 1fecd90
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
51 changes: 51 additions & 0 deletions app/presenters/publishing_api/landing_page_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class LandingPagePresenter

attr_reader :update_type

IMAGE_PATTERN = /^\[Image:\s*(.*?)\s*\]/

def initialize(item, update_type: nil)
@item = item
@update_type = update_type || default_update_type(item)
Expand Down Expand Up @@ -49,11 +51,60 @@ def document_type
def details
body = YAML.load(item.body, permitted_classes: [Date])
.merge(PayloadBuilder::Attachments.for(item))

body = recursively_expand_images(body)

extends_slug = body.delete("extends")
return body unless extends_slug

extends = Document.find_by(slug: extends_slug).latest_edition
YAML.load(extends.body, permitted_classes: [Date]).merge(body)
end

def recursively_expand_images(input)
case input
in String => s if s =~ IMAGE_PATTERN
image_id = IMAGE_PATTERN.match(s).captures.first
image = item.images.find { _1.filename == image_id }
present_image(image, s)
in Hash => h
h.transform_values { recursively_expand_images(_1) }
in Array => a
a.map { recursively_expand_images(_1) }
else
input
end
end

def present_image(image, pattern)
return { errors: ["Image not found for pattern #{pattern}"] } if image.nil?

result = {
id: image.filename,
image_kind: image.image_data.image_kind,
alt_text: image.alt_text,
url: image.url,
caption: image.caption,
created_at: image.created_at,
updated_at: image.updated_at,
}

if image.image_data.all_asset_variants_uploaded?
result[:versions] = present_image_versions(image)
else
result[:errors] = ["Image versions have not finished uploading"]
end

result
end

def present_image_versions(image)
image.image_data.image_kind_config.versions.map do |v|
{
name: v.name,
url: image.url(v.name),
}
end
end
end
end
101 changes: 101 additions & 0 deletions test/unit/app/presenters/publishing_api/landing_page_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,105 @@ class PublishingApi::LandingPagePresenterTest < ActiveSupport::TestCase

assert_equal expected_details, presented_content[:details].deep_stringify_keys
end

test "it recursively expands images in the body" do
body = <<~YAML
blocks:
- type: hero
image:
sources:
- "[Image: big-cheese.960x640.jpg]"
- "[Image: minister-of-funk.960x640.jpg]"
- type: grid_container
blocks:
- type: hero
image:
sources:
- "[Image: big-cheese.960x640.jpg]"
YAML

landing_page = create(
:landing_page,
document: create(:document, id: 12_346, slug: "/landing-page/with-images"),
body:,
title: "Landing Page title",
summary: "Landing Page summary",
first_published_at: @first_published_at = Time.zone.now,
updated_at: 1.year.ago,
images: [
build(:image, image_data: build(:image_data, file: upload_fixture("big-cheese.960x640.jpg", "image/jpg"))),
build(:image, image_data: build(:image_data, file: upload_fixture("minister-of-funk.960x640.jpg", "image/jpg"))),
],
)

presented_landing_page = PublishingApi::LandingPagePresenter.new(landing_page)
presented_content = I18n.with_locale("en") { presented_landing_page.content }

assert_pattern do
presented_content[:details].deep_symbolize_keys => {
blocks: [
{
type: "hero",
image: {
sources: [
{ id: "big-cheese.960x640.jpg", image_kind: "default", versions: Array },
{ id: "minister-of-funk.960x640.jpg", image_kind: "default", versions: Array },
]
}
},
{
type: "grid_container",
blocks: [{
type: "hero",
image: {
sources: [
{ id: "big-cheese.960x640.jpg", image_kind: "default", versions: Array },
]
}
}],
},
]}
end
end

test "it presents errors if files are not found" do
body = <<~YAML
blocks:
- type: hero
image:
sources:
- "[Image: non-existent-file.jpg]"
YAML

landing_page = create(
:landing_page,
document: create(:document, id: 12_346, slug: "/landing-page/with-images"),
body:,
title: "Landing Page title",
summary: "Landing Page summary",
first_published_at: @first_published_at = Time.zone.now,
updated_at: 1.year.ago,
images: [],
)

presented_landing_page = PublishingApi::LandingPagePresenter.new(landing_page)
presented_content = I18n.with_locale("en") { presented_landing_page.content }
details = presented_content[:details].deep_symbolize_keys

assert_pattern do
details =>
{
blocks: [
{
type: "hero",
image: {
sources: [
{ errors: ["Image not found for pattern [Image: non-existent-file.jpg]"] },
]
}
},
],
}
end
end
end

0 comments on commit 1fecd90

Please sign in to comment.