Skip to content

Commit

Permalink
Add timeline helpers to the HostContentUpdateEvent
Browse files Browse the repository at this point in the history
Working out what event “belongs” to an edition is tricky, as Publishing
API doesn’t have the concept of editions. With this in mind we have a
bunch of logic:

- If an edition is superseded and the event happened after the edition
was superseded, it’s for a newer edition
- If an edition is published, and the event happened after the edition
was published AND the event is not for a newer edition, it’s for the
current edition
- If neither of the above is true, it’s for an older edition
  • Loading branch information
pezholio committed Dec 19, 2024
1 parent d99f6db commit 8d4a784
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app/models/host_content_update_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ def self.all_for_date_window(document:, from:, to:)
end
end

private
def is_for_newer_edition?(edition)
edition.superseded? && created_at.after?(edition.superseded_at)
end

def is_for_current_edition?(edition)
edition.published_at && created_at.after?(edition.published_at) && !is_for_newer_edition?(edition)
end

def is_for_older_edition?(edition)
!is_for_newer_edition?(edition) && !is_for_current_edition?(edition)
end

def self.get_user_for_uuid(uuid)
User.find_by(uid: uuid)
Expand Down
85 changes: 85 additions & 0 deletions test/unit/app/models/host_content_update_event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,89 @@ class HostContentUpdateEventTest < ActiveSupport::TestCase
assert_equal result.second.content_title, "Another exciting piece of content"
end
end

describe "Timeline helpers" do
let(:created_at) { Time.zone.now - 1.month }
let(:host_content_update_event) { build(:host_content_update_event, created_at:) }

describe "when the edition is published" do
let(:edition) { build(:edition) }

before do
edition.stubs(:published_at).returns(created_at - 12.months)
end

describe "when the event occurred after the edition was superseded" do
before do
edition.stubs(:superseded?).returns(true)
edition.stubs(:superseded_at).returns(created_at - 2.days)
end

describe "#is_for_newer_edition?" do
it "returns true" do
assert host_content_update_event.is_for_newer_edition?(edition)
end
end

describe "#is_for_current_edition?" do
it "returns false" do
assert_not host_content_update_event.is_for_current_edition?(edition)
end
end

describe "#is_for_older_edition?" do
it "returns false" do
assert_not host_content_update_event.is_for_older_edition?(edition)
end
end
end

describe "when the event occurred before the edition was superseded" do
before do
edition.stubs(:superseded?).returns(true)
edition.stubs(:superseded_at).returns(created_at + 2.days)
end

describe "#is_for_newer_edition?" do
it "returns false" do
assert_not host_content_update_event.is_for_newer_edition?(edition)
end
end

describe "#is_for_current_edition?" do
it "returns true" do
assert host_content_update_event.is_for_current_edition?(edition)
end
end

describe "#is_for_older_edition?" do
it "returns false" do
assert_not host_content_update_event.is_for_older_edition?(edition)
end
end
end
end

describe "when the edition is draft" do
let(:edition) { build(:edition, :draft) }

describe "#is_for_newer_edition?" do
it "returns false" do
assert_not host_content_update_event.is_for_newer_edition?(edition)
end
end

describe "#is_for_current_edition?" do
it "returns false" do
assert_not host_content_update_event.is_for_current_edition?(edition)
end
end

describe "#is_for_older_edition?" do
it "returns true" do
assert host_content_update_event.is_for_older_edition?(edition)
end
end
end
end
end

0 comments on commit 8d4a784

Please sign in to comment.