Skip to content

Commit

Permalink
Render published date using change_history
Browse files Browse the repository at this point in the history
* first_published_at does not have reliable data, at time of writing
dates could be after public_updated_at
* the standard approach using details[“first_public_at”] is not
available
* Instead use first date in change history

When https://trello.com/c/xCJ3RN6W/ has been fixed, and when the dates
are reliable this can be switched back.

Add some tests to updatable to be explicit about expected behaviour
around presence of first_public_at
  • Loading branch information
fofr committed Mar 1, 2017
1 parent cdf481a commit e848191
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/presenters/specialist_document_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,17 @@ def title_and_context
t.delete(:context)
end
end

private

# first_published_at does not have reliable data
# at time of writing dates could be after public_updated_at
# details.first_public_at is not provided
# https://trello.com/c/xCJ3RN6W/
#
# Instead use first date in change history
def first_public_at
changes = reverse_chronological_change_history
changes.any? ? changes.last[:timestamp] : nil
end
end
24 changes: 24 additions & 0 deletions test/integration/specialist_document_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ class SpecialistDocumentTest < ActionDispatch::IntegrationTest
assert_has_component_document_footer_pair("from", [aaib])
end

test "renders published and updated in metadata and document footer" do
setup_and_visit_content_item('countryside-stewardship-grants')

assert_has_component_metadata_pair("first_published", "2 April 2015")
assert_has_component_metadata_pair("last_updated", "29 March 2016")

assert_has_component_document_footer_pair("published", "2 April 2015")
assert_has_component_document_footer_pair("updated", "29 March 2016")
end

test "renders change history in reverse chronological order" do
setup_and_visit_content_item('countryside-stewardship-grants')

within shared_component_selector("document_footer") do
# Flatten top level / "other" args, for consistent hash access
component_args = JSON.parse(page.text)
history = component_args.fetch("history")

assert_equal history.first["note"], @content_item["details"]["change_history"].last["note"]
assert_equal history.last["note"], @content_item["details"]["change_history"].first["note"]
assert_equal history.size, @content_item["details"]["change_history"].size
end
end

test "renders a contents list" do
setup_and_visit_content_item('aaib-reports')

Expand Down
22 changes: 22 additions & 0 deletions test/presenters/specialist_document_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ class PresentedSpecialistDocument < SpecialistDocumentTestCase
assert presented_item('aaib-reports').is_a?(ContentsList)
end

test 'presents the published date using the oldest date in the change history' do
example = schema_item('aaib-reports')
example["first_published_at"] = "2001-01-01"
example["details"]["change_history"] = [
{
"note" => "Newer",
"public_timestamp" => "2003-03-03"
},
{
"note" => "Oldest",
"public_timestamp" => "2002-02-02"
},
{
"note" => "More recent",
"public_timestamp" => "2013-03-03"
},
]

presented = present_example(example)
assert DateTime.parse(presented.published) == DateTime.parse("2002-02-02")
end

test 'has title without context' do
assert presented_item('aaib-reports').is_a?(TitleAndContext)
title_component_params = {
Expand Down
48 changes: 48 additions & 0 deletions test/presenters/updatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,54 @@ def content_item
assert_empty @updatable.history
end

test '#history returns updates when first_public_at does not match public_updated_at' do
class << @updatable
def content_item
{
'public_updated_at' => '2002-02-02',
'details' => {
'first_public_at' => '2001-01-01',
'change_history' => [
{
'note' => 'note',
'public_timestamp' => '2002-02-02'
}
]
}
}
end

def display_date(date)
date
end
end

assert @updatable.history.any?
assert_equal @updatable.updated, '2002-02-02'
end

test '#history returns no updates when first_public_at matches public_updated_at' do
class << @updatable
def content_item
{
'public_updated_at' => '2002-02-02',
'details' => {
'first_public_at' => '2002-02-02',
'change_history' => [
{
'note' => 'note',
'public_timestamp' => '2002-02-02'
}
]
}
}
end
end

assert @updatable.history.empty?
refute @updatable.updated
end

test '#history returns an array of hashes when there is change history' do
class << @updatable
def any_updates?
Expand Down

0 comments on commit e848191

Please sign in to comment.