diff --git a/app/models/host_content_update_event.rb b/app/models/host_content_update_event.rb index 6b7f953ea95..1265f5e15d6 100644 --- a/app/models/host_content_update_event.rb +++ b/app/models/host_content_update_event.rb @@ -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) diff --git a/test/unit/app/models/host_content_update_event_test.rb b/test/unit/app/models/host_content_update_event_test.rb index 80afb3b528f..dafa2b696ec 100644 --- a/test/unit/app/models/host_content_update_event_test.rb +++ b/test/unit/app/models/host_content_update_event_test.rb @@ -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