From 4b79f76c3ee437472f3d4b3ca8c0f81cb9e2e034 Mon Sep 17 00:00:00 2001 From: George Schena Date: Mon, 4 Nov 2024 20:57:59 +0000 Subject: [PATCH] Add metadata concern and tests Commit audit trail: - https://github.com/alphagov/government-frontend/blob/a938e4bee78b794e4e5303a1bde1dfd05e94b287/app/presenters/content_item/metadata.rb - https://github.com/alphagov/government-frontend/blob/a938e4bee78b794e4e5303a1bde1dfd05e94b287/test/presenters/content_item/metadata_test.rb --- app/models/concerns/metadata.rb | 25 +++++++++++ spec/models/concerns/metadata_spec.rb | 63 +++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 app/models/concerns/metadata.rb create mode 100644 spec/models/concerns/metadata_spec.rb diff --git a/app/models/concerns/metadata.rb b/app/models/concerns/metadata.rb new file mode 100644 index 0000000000..5684d44aeb --- /dev/null +++ b/app/models/concerns/metadata.rb @@ -0,0 +1,25 @@ +module Metadata + extend ActiveSupport::Concern + + def details_display_date + content_store_hash.dig("details", "display_date") + end + + def pending_stats_announcement? + details_display_date.present? && Time.zone.parse(details_display_date).future? + end + + def publisher_metadata + metadata = { + from:, + first_published: first_published_at, + last_updated: updated, + } + + unless pending_stats_announcement? + metadata[:see_updates_link] = true + end + + metadata + end +end diff --git a/spec/models/concerns/metadata_spec.rb b/spec/models/concerns/metadata_spec.rb new file mode 100644 index 0000000000..9ae557c04f --- /dev/null +++ b/spec/models/concerns/metadata_spec.rb @@ -0,0 +1,63 @@ +class DummyContentItem + include Metadata + attr_accessor :content_item + + def content_store_hash + @content_item + end + + def initialize + @content_item = { + "details" => { + "display_date" => nil, + }, + } + end +end + +RSpec.describe Metadata, type: :model do + let(:item) { DummyContentItem.new } + + describe "#details_display_date" do + it "returns the display date from the content store hash" do + item.content_item["details"]["display_date"] = "2023-10-01" + expect(item.details_display_date).to eq("2023-10-01") + end + end + + describe "#pending_stats_announcement?" do + it "returns true if display date is in the future" do + item.content_item["details"]["display_date"] = (Time.zone.now + 1.day).iso8601 + expect(item.pending_stats_announcement?).to be true + end + end + + describe "#publisher_metadata" do + before do + allow(item).to receive(:from).and_return("from_value") + allow(item).to receive(:first_published_at).and_return("2023-01-01") + allow(item).to receive(:updated).and_return("2023-10-01") + end + + it "includes see_updates_link if not a pending stats announcement" do + item.content_item["details"]["display_date"] = (Time.zone.now - 1.day).iso8601 + expected_metadata = { + from: "from_value", + first_published: "2023-01-01", + last_updated: "2023-10-01", + see_updates_link: true, + } + expect(item.publisher_metadata).to eq(expected_metadata) + end + + it "does not include see_updates_link if a pending stats announcement" do + item.content_item["details"]["display_date"] = (Time.zone.now + 1.day).iso8601 + expected_metadata = { + from: "from_value", + first_published: "2023-01-01", + last_updated: "2023-10-01", + } + expect(item.publisher_metadata).to eq(expected_metadata) + end + end +end