Skip to content

Commit

Permalink
Thumbnail update (#834)
Browse files Browse the repository at this point in the history
* Add ostruct to page model for default thumbnail

* WIP: refactor for thumbnail logic

* Update logic for thumbnail in card

* Update naming and specs

* Update placeholder resource

* Update based on PR comments
  • Loading branch information
martikat authored Aug 5, 2024
1 parent be69967 commit 6b91209
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 17 deletions.
48 changes: 38 additions & 10 deletions app/helpers/content_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'ostruct'

module ContentHelper
# @see [CustomMarkdown config/initializers/markdown.rb]
# @param markdown [String]
Expand Down Expand Up @@ -53,45 +55,71 @@ def side_navigation(page)
end
end

# @return [Resource, OpenStruct]
def signup
Resource.by_name('ctas.signup') || null_resource('ctas.signup')
Resource.by_name('ctas.signup') || placeholder_resource('ctas.signup')
end

# @return [Resource, OpenStruct]
def not_found
Resource.by_name('error.not_found') || null_resource('error.not_found')
Resource.by_name('error.not_found') || placeholder_resource('error.not_found')
end

# @return [Resource, OpenStruct]
def internal_server_error
Resource.by_name('error.internal_server_error') || null_resource('error.internal_server_error')
Resource.by_name('error.internal_server_error') || placeholder_resource('error.internal_server_error')
end

# @return [Resource, OpenStruct]
def unprocessable_entity
Resource.by_name('error.unprocessable_entity') || null_resource('error.unprocessable_entity')
Resource.by_name('error.unprocessable_entity') || placeholder_resource('error.unprocessable_entity')
end

# @return [Resource, OpenStruct]
def service_unavailable
Resource.by_name('error.service_unavailable') || null_resource('error.service_unavailable')
Resource.by_name('error.service_unavailable') || placeholder_resource('error.service_unavailable')
end

# @return [Resource, OpenStruct]
def feedback
Resource.by_name('ctas.feedback') || null_resource('ctas.feedback')
Resource.by_name('ctas.feedback') || placeholder_resource('ctas.feedback')
end

# @return [Resource, OpenStruct]
def content_footer
Resource.by_name('ctas.content_footer') || null_resource('ctas.content_footer')
Resource.by_name('ctas.content_footer') || placeholder_resource('ctas.content_footer')
end

# @return [Resource, OpenStruct]
def other_useful_resources
Resource.by_name('other_useful_resources') || null_resource('other_useful_resources')
Resource.by_name('other_useful_resources') || placeholder_resource('other_useful_resources')
end

def null_resource(name)
OpenStruct.new(
# @param name [String]
# @return [OpenStruct]
def placeholder_resource(name)
::OpenStruct.new(
name: name,
title: "Title for #{name}",
body: "Body for #{name}",
link_to_text: "Link for #{name}",
link_to: '#',
)
end

# @param page [Page]
# @return [String]
def card_thumbnail(page)
thumbnail = page.thumbnail || placeholder_thumbnail
image_tag(thumbnail.url, class: 'full-width-image', alt: thumbnail.description, title: thumbnail.title)
end

# @return [OpenStruct]
def placeholder_thumbnail
::OpenStruct.new(
url: 'https://placehold.co/380x254/347ca9/FFFFFF/png',
description: 'Default description',
title: 'Default title',
)
end
end
5 changes: 3 additions & 2 deletions app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def navigation?
page_style == 'side-nav'
end

# @return [ContentfulModel::Asset]
# @return [ContentfulModel::Asset, nil]
def thumbnail
return ::OpenStruct.new(url: 'https://placehold.co/380x254/347ca9/FFFFFF/png') if fields[:image].blank?
return if fields[:image].blank?

fetch_or_store self.class.to_key(fields[:image].id) do
ContentfulModel::Asset.find(fields[:image].id)
Expand All @@ -84,6 +84,7 @@ def tier3?
parent&.tier2?
end

# @return [Date]
def created_at
released_at || super
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/pages/_card.html.slim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.govuk-grid-column-one-third
.hf-card
a href=card.path
.hf-card-container
= image_tag(card.thumbnail.url, class: 'full-width-image', alt: card.thumbnail.description, title: card.thumbnail.title)
.hf-card-container
= card_thumbnail(card)

.hf-card-details
h3.govuk-heading-m
Expand Down
33 changes: 30 additions & 3 deletions spec/helpers/content_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,40 @@
end
end

describe 'Null resource' do
subject(:null_resource) { helper.null_resource('example') }
describe '#placeholder_resource' do
subject(:placeholder_resource) { helper.placeholder_resource('example') }

context 'with name passed in' do
it 'returns content using name' do
expect(null_resource.title).to eq 'Title for example'
expect(placeholder_resource.title).to eq 'Title for example'
end
end
end

describe '#card_thumbnail' do
let(:page) { Page.by_slug(slug) }

context 'with image' do
let(:slug) { 'reducing-paperwork' }

it do
expect(helper.card_thumbnail(page)).to include 'images.ctfassets'
end
end

context 'with placeholder' do
let(:slug) { 'interactions' }

it do
expect(helper.card_thumbnail(page)).to start_with '<img class='
end
end
end

describe '#placeholder_thumbnail' do
it do
expect(helper.placeholder_thumbnail).to be_a OpenStruct
expect(helper.placeholder_thumbnail.url).to eql 'https://placehold.co/380x254/347ca9/FFFFFF/png'
end
end
end
18 changes: 18 additions & 0 deletions spec/models/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,22 @@
specify { expect(page.pages.count).to be 0 }
end
end

describe '#thumbnail' do
context 'with linked asset' do
let(:slug) { 'reducing-paperwork' }

it do
expect(page.thumbnail).to be_a ContentfulModel::Asset
end
end

context 'without linked asset' do
let(:slug) { 'interactions' }

it do
expect(page.thumbnail).to be_nil
end
end
end
end

0 comments on commit 6b91209

Please sign in to comment.