diff --git a/app/helpers/content_helper.rb b/app/helpers/content_helper.rb index 37dc9042..54aaec04 100644 --- a/app/helpers/content_helper.rb +++ b/app/helpers/content_helper.rb @@ -1,3 +1,5 @@ +require 'ostruct' + module ContentHelper # @see [CustomMarkdown config/initializers/markdown.rb] # @param markdown [String] @@ -53,40 +55,50 @@ 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}", @@ -94,4 +106,20 @@ def null_resource(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 diff --git a/app/models/page.rb b/app/models/page.rb index bad92ab9..7c03b968 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -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) @@ -84,6 +84,7 @@ def tier3? parent&.tier2? end + # @return [Date] def created_at released_at || super end diff --git a/app/views/pages/_card.html.slim b/app/views/pages/_card.html.slim index b1a64745..29e5e1d2 100644 --- a/app/views/pages/_card.html.slim +++ b/app/views/pages/_card.html.slim @@ -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 diff --git a/spec/helpers/content_helper_spec.rb b/spec/helpers/content_helper_spec.rb index acab70fd..32fb267e 100644 --- a/spec/helpers/content_helper_spec.rb +++ b/spec/helpers/content_helper_spec.rb @@ -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 '