Skip to content

Commit

Permalink
Merge pull request #1178 from alphagov/default-to-placeholder-image-f…
Browse files Browse the repository at this point in the history
…or-newslike-content

Default to placeholder image for newslike content
  • Loading branch information
emmabeynon authored Dec 6, 2018
2 parents 8411d58 + 6d28543 commit 6c24f9b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 38 deletions.
18 changes: 18 additions & 0 deletions app/presenters/content_item/news_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module ContentItem
module NewsImage
def image
content_item.dig("details", "image") || default_news_image || placeholder_image
end

private

def default_news_image
organisation = content_item.dig("links", "primary_publishing_organisation")
organisation[0].dig("details", "default_news_image") if organisation.present?
end

def placeholder_image
"https://assets.publishing.service.gov.uk/government/assets/placeholder.jpg"
end
end
end
12 changes: 1 addition & 11 deletions app/presenters/news_article_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,5 @@ class NewsArticlePresenter < ContentItemPresenter
include ContentItem::Shareable
include ContentItem::TitleAndContext
include ContentItem::Metadata

def image
content_item["details"].dig("image") || default_news_image
end

private

def default_news_image
organisation = content_item["links"].dig("primary_publishing_organisation")
organisation[0].dig("details", "default_news_image") if organisation.present?
end
include ContentItem::NewsImage
end
5 changes: 1 addition & 4 deletions app/presenters/speech_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ class SpeechPresenter < ContentItemPresenter
include ContentItem::Updatable
include ContentItem::TitleAndContext
include ContentItem::Metadata

def image
content_item["details"]["image"]
end
include ContentItem::NewsImage

def delivery_type
if document_type == 'authored_article'
Expand Down
5 changes: 1 addition & 4 deletions app/presenters/world_location_news_article_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ class WorldLocationNewsArticlePresenter < ContentItemPresenter
include ContentItem::Shareable
include ContentItem::TitleAndContext
include ContentItem::Metadata

def image
content_item["details"]["image"]
end
include ContentItem::NewsImage
end
41 changes: 41 additions & 0 deletions test/presenters/content_item/news_image_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "test_helper"

class ContentItemNewsImageTest < ActiveSupport::TestCase
class DummyContentItem
include ContentItem::NewsImage
attr_accessor :content_item

def initialize
@content_item = {
"base_path" => "/a/base/path",
"details" => {},
"links" => {},
}
end
end

test "presents the document's image if present" do
item = DummyContentItem.new
image = { "url" => "http://www.test.dev.gov.uk/lead_image.jpg" }
item.content_item["details"]["image"] = image

assert_equal image, item.image
end

test "presents the document's organisation's default_news_image if document's image is not present" do
item = DummyContentItem.new
default_news_image = { "url" => "http://www.test.dev.gov.uk/default_news_image.jpg" }
item.content_item["links"]["primary_publishing_organisation"] = [
"details" => { "default_news_image" => default_news_image }
]

assert_equal default_news_image, item.image
end

test "presents a placeholder image if document has no image or default news image" do
item = DummyContentItem.new
placeholder_image = "https://assets.publishing.service.gov.uk/government/assets/placeholder.jpg"

assert_equal placeholder_image, item.image
end
end
19 changes: 0 additions & 19 deletions test/presenters/news_article_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,6 @@ class PresentedNewsArticleTest < NewsArticlePresenterTestCase
test 'presents the locale' do
assert_equal schema_item['locale'], presented_item.locale
end

test 'presents the document\'s image if present' do
assert_equal schema_item['details']['image'], presented_item.image
end

test 'presents the document\'s organisation\'s default_news_image if document\'s image is not present' do
default_news_image = { 'url' => 'http://www.test.dev.gov.uk/default_news_image.jpg' }
example = schema_item
example['details'].delete('image')
example['links'] = {
'primary_publishing_organisation' => [
'details' => {
'default_news_image' => default_news_image
}
]
}
presented_item = present_example(example)
assert_equal default_news_image, presented_item.image
end
end

class HistoryModePresentedNewsArticle < NewsArticlePresenterTestCase
Expand Down

0 comments on commit 6c24f9b

Please sign in to comment.