-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9662 from alphagov/landing-page-featured-images-2
Add support for landing page images in image / featured blocks
- Loading branch information
Showing
11 changed files
with
311 additions
and
5 deletions.
There are no files selected for viewing
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,51 @@ | ||
module LandingPageImageBlock | ||
extend ActiveSupport::Concern | ||
|
||
IMAGE_PATTERN = /^\[Image:\s*(.*?)\s*\]/ | ||
EXPECTED_IMAGE_KINDS = { | ||
desktop_image: "landing_page_image", | ||
tablet_image: "landing_page_image", | ||
mobile_image: "landing_page_image", | ||
}.freeze | ||
|
||
included do | ||
attr_reader :desktop_image, :tablet_image, :mobile_image, :alt | ||
|
||
validates :desktop_image, :tablet_image, :mobile_image, presence: true | ||
validates_each :desktop_image, :tablet_image, :mobile_image do |record, attr, value| | ||
next if value.blank? | ||
|
||
expected_kind = EXPECTED_IMAGE_KINDS.fetch(attr) | ||
actual_kind = value.image_data.image_kind | ||
record.errors.add(attr, "is of the wrong image kind: #{actual_kind}") if actual_kind != expected_kind | ||
end | ||
|
||
def present_image | ||
{ | ||
"image" => { | ||
"alt" => alt, | ||
"sources" => present_image_sources, | ||
}, | ||
} | ||
end | ||
|
||
def present_image_sources | ||
{ | ||
"desktop" => desktop_image.url(:landing_page_desktop_1x), | ||
"desktop_2x" => desktop_image.url(:landing_page_desktop_2x), | ||
"tablet" => tablet_image.url(:landing_page_tablet_1x), | ||
"tablet_2x" => tablet_image.url(:landing_page_tablet_2x), | ||
"mobile" => mobile_image.url(:landing_page_mobile_1x), | ||
"mobile_2x" => mobile_image.url(:landing_page_mobile_2x), | ||
} | ||
end | ||
|
||
def find_image(image_expression) | ||
match = IMAGE_PATTERN.match(image_expression) | ||
return if match.nil? | ||
|
||
image_id = match.captures.first | ||
images.find { _1.filename == image_id } | ||
end | ||
end | ||
end |
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
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
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,18 @@ | ||
class LandingPage::FeaturedBlock < LandingPage::CompoundBlock | ||
include ActiveModel::API | ||
include LandingPageImageBlock | ||
|
||
def initialize(source, images, content_blocks) | ||
super(source, images, "featured_content", content_blocks) | ||
|
||
image_sources = @source.dig("image", "sources") || {} | ||
@desktop_image = find_image(image_sources["desktop"]) | ||
@tablet_image = find_image(image_sources["tablet"]) | ||
@mobile_image = find_image(image_sources["mobile"]) | ||
@alt = @source.dig("image", "alt") || "" | ||
end | ||
|
||
def present_for_publishing_api | ||
super.merge(present_image) | ||
end | ||
end |
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,18 @@ | ||
class LandingPage::ImageBlock < LandingPage::BaseBlock | ||
include ActiveModel::API | ||
include LandingPageImageBlock | ||
|
||
def initialize(source, images) | ||
super(source, images) | ||
|
||
image_sources = @source.dig("image", "sources") || {} | ||
@desktop_image = find_image(image_sources["desktop"]) | ||
@tablet_image = find_image(image_sources["tablet"]) | ||
@mobile_image = find_image(image_sources["mobile"]) | ||
@alt = @source.dig("image", "alt") || "" | ||
end | ||
|
||
def present_for_publishing_api | ||
super.merge(present_image) | ||
end | ||
end |
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,83 @@ | ||
require "test_helper" | ||
|
||
class FeaturedBlockTest < ActiveSupport::TestCase | ||
setup do | ||
@valid_featured_images = [ | ||
build(:image, image_data: build(:landing_page_image_data, file: upload_fixture("landing_page_image.png", "image/png"))), | ||
] | ||
@valid_featured_content_blocks = [{ "type" => "some-block-type" }] | ||
@valid_featured_block_config = { | ||
"type" => "featured", | ||
"image" => { | ||
"alt" => "some alt text", | ||
"sources" => { | ||
"desktop" => "[Image: landing_page_image.png]", | ||
"tablet" => "[Image: landing_page_image.png]", | ||
"mobile" => "[Image: landing_page_image.png]", | ||
}, | ||
}, | ||
"featured_content" => { "blocks" => @valid_featured_content_blocks }, | ||
} | ||
end | ||
|
||
test "valid when given correct params" do | ||
subject = LandingPage::FeaturedBlock.new(@valid_featured_block_config, @valid_featured_images, @valid_featured_content_blocks) | ||
assert subject.valid? | ||
end | ||
|
||
test "presents featured blocks to publishing api" do | ||
subject = LandingPage::FeaturedBlock.new(@valid_featured_block_config, @valid_featured_images, @valid_featured_content_blocks) | ||
expected_result = { | ||
"type" => "featured", | ||
"image" => { | ||
"alt" => "some alt text", | ||
"sources" => { | ||
"desktop" => "http://asset-manager/landing_page_desktop_1x", | ||
"desktop_2x" => "http://asset-manager/landing_page_desktop_2x", | ||
"tablet" => "http://asset-manager/landing_page_tablet_1x", | ||
"tablet_2x" => "http://asset-manager/landing_page_tablet_2x", | ||
"mobile" => "http://asset-manager/landing_page_mobile_1x", | ||
"mobile_2x" => "http://asset-manager/landing_page_mobile_2x", | ||
}, | ||
}, | ||
"featured_content" => { | ||
"blocks" => [{ "type" => "some-block-type" }], | ||
}, | ||
} | ||
assert_equal(expected_result, subject.present_for_publishing_api) | ||
end | ||
|
||
test "invalid when missing images" do | ||
subject = LandingPage::FeaturedBlock.new(@valid_featured_block_config.except("image"), @valid_featured_images, @valid_featured_content_blocks) | ||
assert subject.invalid? | ||
assert_equal [ | ||
"Desktop image can't be blank", | ||
"Tablet image can't be blank", | ||
"Mobile image can't be blank", | ||
], subject.errors.to_a | ||
end | ||
|
||
test "invalid when image expressions are not found" do | ||
no_images = [] | ||
subject = LandingPage::FeaturedBlock.new(@valid_featured_block_config, no_images, @valid_featured_content_blocks) | ||
assert subject.invalid? | ||
assert_equal [ | ||
"Desktop image can't be blank", | ||
"Tablet image can't be blank", | ||
"Mobile image can't be blank", | ||
], subject.errors.to_a | ||
end | ||
|
||
test "invalid when missing featured content blocks" do | ||
subject = LandingPage::FeaturedBlock.new(@valid_featured_block_config, @valid_featured_images, nil) | ||
assert subject.invalid? | ||
assert_equal ["Content blocks can't be blank"], subject.errors.to_a | ||
end | ||
|
||
test "invalid when featured content blocks are invalid" do | ||
invalid_blocks_config = [{ "invalid" => "because I do not have a type" }] | ||
subject = LandingPage::FeaturedBlock.new(@valid_featured_block_config, @valid_featured_images, invalid_blocks_config) | ||
assert subject.invalid? | ||
assert_equal ["Type can't be blank"], subject.errors.to_a | ||
end | ||
end |
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,64 @@ | ||
require "test_helper" | ||
|
||
class ImageBlockTest < ActiveSupport::TestCase | ||
setup do | ||
@valid_landing_page_images = [ | ||
build(:image, image_data: build(:landing_page_image_data, file: upload_fixture("landing_page_image.png", "image/png"))), | ||
] | ||
@valid_image_block_config = { | ||
"type" => "image", | ||
"image" => { | ||
"sources" => { | ||
"desktop" => "[Image: landing_page_image.png]", | ||
"tablet" => "[Image: landing_page_image.png]", | ||
"mobile" => "[Image: landing_page_image.png]", | ||
}, | ||
}, | ||
} | ||
end | ||
|
||
test "valid when given correct params" do | ||
subject = LandingPage::ImageBlock.new(@valid_image_block_config, @valid_landing_page_images) | ||
assert subject.valid? | ||
end | ||
|
||
test "presents hero blocks to publishing api" do | ||
subject = LandingPage::ImageBlock.new(@valid_image_block_config, @valid_landing_page_images) | ||
expected_result = { | ||
"type" => "image", | ||
"image" => { | ||
"alt" => "", | ||
"sources" => { | ||
"desktop" => "http://asset-manager/landing_page_desktop_1x", | ||
"desktop_2x" => "http://asset-manager/landing_page_desktop_2x", | ||
"tablet" => "http://asset-manager/landing_page_tablet_1x", | ||
"tablet_2x" => "http://asset-manager/landing_page_tablet_2x", | ||
"mobile" => "http://asset-manager/landing_page_mobile_1x", | ||
"mobile_2x" => "http://asset-manager/landing_page_mobile_2x", | ||
}, | ||
}, | ||
} | ||
assert_equal(expected_result, subject.present_for_publishing_api) | ||
end | ||
|
||
test "invalid when missing images" do | ||
subject = LandingPage::ImageBlock.new(@valid_image_block_config.except("image"), @valid_landing_page_images) | ||
assert subject.invalid? | ||
assert_equal [ | ||
"Desktop image can't be blank", | ||
"Tablet image can't be blank", | ||
"Mobile image can't be blank", | ||
], subject.errors.to_a | ||
end | ||
|
||
test "invalid when image expressions are not found" do | ||
no_images = [] | ||
subject = LandingPage::ImageBlock.new(@valid_image_block_config, no_images) | ||
assert subject.invalid? | ||
assert_equal [ | ||
"Desktop image can't be blank", | ||
"Tablet image can't be blank", | ||
"Mobile image can't be blank", | ||
], subject.errors.to_a | ||
end | ||
end |
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