Skip to content

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
25 changes: 25 additions & 0 deletions app/models/concerns/metadata.rb
Original file line number Diff line number Diff line change
@@ -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
63 changes: 63 additions & 0 deletions spec/models/concerns/metadata_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4b79f76

Please sign in to comment.