Skip to content

Commit

Permalink
Use new methods in PaginatedTimeline
Browse files Browse the repository at this point in the history
This calls the new methods on each entry, simplifying the code, and
allowing us to stub the response, rather than relying on the objects
set up in `seed_document_event_history`
  • Loading branch information
pezholio committed Dec 19, 2024
1 parent 8d4a784 commit 090f1d6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 39 deletions.
24 changes: 3 additions & 21 deletions app/models/document/paginated_timeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,15 @@ def query
end

def entries_on_newer_editions(edition)
@entries_on_newer_editions ||= entries.select do |entry|
if entry.is_a?(EditorialRemark)
entry.edition_id > edition.id
else
entry.item_id > edition.id
end
end
entries.select { |e| e.is_for_newer_edition?(edition) }
end

def entries_on_current_edition(edition)
@entries_on_current_edition ||= entries.select do |entry|
if entry.is_a?(EditorialRemark)
entry.edition_id == edition.id
else
entry.item_id == edition.id
end
end
entries.select { |e| e.is_for_current_edition?(edition) }
end

def entries_on_previous_editions(edition)
@entries_on_previous_editions ||= entries.select do |entry|
if entry.is_a?(EditorialRemark)
entry.edition_id < edition.id
else
entry.item_id < edition.id
end
end
entries.select { |e| e.is_for_older_edition?(edition) }
end

def total_count
Expand Down
81 changes: 63 additions & 18 deletions test/unit/app/models/document/paginated_timeline_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,30 +263,75 @@ class PaginatedTimelineTest < ActiveSupport::TestCase
end
end

describe "#entries_on_newer_editions" do
it "returns entries on newer editions than the one passed in" do
timeline = Document::PaginatedTimeline.new(document: @document, page: 1)
expected_entries = timeline.entries.slice(1, 2)
describe "filtering entries" do
let(:entries_for_newer_editions) do
2.times.map do
stub("entry", is_for_newer_edition?: true, is_for_current_edition?: false, is_for_older_edition?: false)
end
end

assert_equal expected_entries, timeline.entries_on_newer_editions(@first_edition)
let(:entries_for_current_edition) do
4.times.map do
stub("entry", is_for_newer_edition?: false, is_for_current_edition?: true, is_for_older_edition?: false)
end
end
end

describe "#entries_on_current_edition" do
it "returns entries for the edition passed in" do
timeline = Document::PaginatedTimeline.new(document: @document, page: 1)
expected_entries = timeline.entries - timeline.entries.slice(1, 2)

assert_equal expected_entries, timeline.entries_on_current_edition(@first_edition)
let(:entries_for_older_editions) do
3.times.map do
stub("entry", is_for_newer_edition?: false, is_for_current_edition?: false, is_for_older_edition?: true)
end
end
end

describe "#entries_on_previous_editions" do
it "returns entries on previous editions" do
timeline = Document::PaginatedTimeline.new(document: @document, page: 1)
expected_entries = timeline.entries - timeline.entries.slice(1, 2)
let(:all_entries) do
[*entries_for_newer_editions, *entries_for_current_edition, *entries_for_older_editions]
end

let(:timeline) { Document::PaginatedTimeline.new(document: @document, page: 1) }

before do
timeline.stubs(:entries).returns(all_entries)
end

describe "#entries_on_newer_editions" do
it "returns entries on newer editions than the one passed in" do
assert_equal entries_for_newer_editions, timeline.entries_on_newer_editions(@first_edition)
end

it "calls the entries with the expected edition" do
all_entries.each do |entry|
entry.expects(:is_for_newer_edition?).with(@first_edition)
end

timeline.entries_on_newer_editions(@first_edition)
end
end

describe "#entries_on_current_edition" do
it "returns entries for the edition passed in" do
assert_equal entries_for_current_edition, timeline.entries_on_current_edition(@first_edition)
end

it "calls the entries with the expected edition" do
all_entries.each do |entry|
entry.expects(:is_for_current_edition?).with(@first_edition)
end

assert_equal expected_entries, timeline.entries_on_previous_editions(@newest_edition)
timeline.entries_on_current_edition(@first_edition)
end
end

describe "#entries_on_previous_editions" do
it "returns entries on previous editions" do
assert_equal entries_for_older_editions, timeline.entries_on_previous_editions(@newest_edition)
end

it "calls the entries with the expected edition" do
all_entries.each do |entry|
entry.expects(:is_for_older_edition?).with(@first_edition)
end

timeline.entries_on_previous_editions(@first_edition)
end
end
end

Expand Down

0 comments on commit 090f1d6

Please sign in to comment.