Skip to content

Commit

Permalink
Merge pull request #2314 from alphagov/memoise-filtered-editions-pres…
Browse files Browse the repository at this point in the history
…enter-editions

Memoize FilteredEditionsPresenter.editions
  • Loading branch information
mtaylorgds authored Nov 20, 2024
2 parents eb43db7 + 7da288c commit f7729ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions app/presenters/filtered_editions_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def assignees
end

def editions
@editions ||= query_editions
end

private

def query_editions
result = editions_by_content_type
result = apply_states_filter(result)
result = apply_assigned_to_filter(result)
Expand All @@ -68,8 +74,6 @@ def editions
result.order_by(%w[updated_at desc]).page(@page).per(ITEMS_PER_PAGE)
end

private

def available_users
User.enabled.alphabetized
end
Expand Down
8 changes: 8 additions & 0 deletions test/unit/presenters/filtered_editions_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ def a_gds_user
assert_equal(middle, filtered_editions[1])
assert_equal(oldest, filtered_editions[2])
end

should "only query the database once, regardless of how many times it is called" do
presenter = FilteredEditionsPresenter.new(a_gds_user)
presenter.expects(:query_editions).once.returns([])

presenter.editions
presenter.editions
end
end

context "#assignees" do
Expand Down

0 comments on commit f7729ad

Please sign in to comment.