Skip to content

Commit

Permalink
WIP refactor presenter - todo, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
richardTowers committed Nov 5, 2024
1 parent 8afcfc3 commit 8be74fd
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions app/presenters/publishing_api/landing_page_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,8 @@ 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 { |i| i.filename == image_id && i.image_data.all_asset_variants_uploaded? }
return s if image.nil?

{
id: image.filename,
alt_text: image.alt_text,
url: image.url,
caption: image.caption,
created_at: image.created_at,
updated_at: image.updated_at,
versions: image.image_data.image_kind_config.versions.map do |v|
{
name: v.name,
url: image.url(v.name),
}
end,
}
image = item.images.find { _1.filename == image_id }
present_image(image)
in Hash => h
h.transform_values { recursively_expand_images(_1) }
in Array => a
Expand All @@ -90,5 +75,33 @@ def recursively_expand_images(input)
input
end
end

def present_image(image)
return { errors: ["Image not found for pattern #{s}"] } 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.to_h do |v|
[v.name, image.url(v.name)]
end
end
end
end

0 comments on commit 8be74fd

Please sign in to comment.