-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2264 from alphagov/205_filter-publications
Backend filtering support for the new publications page (root controller)
- Loading branch information
Showing
10 changed files
with
296 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
class FilteredEditionsPresenter | ||
def initialize(states_filter: [], assigned_to_filter: nil, format_filter: nil, title_filter: nil) | ||
@states_filter = states_filter || [] | ||
@assigned_to_filter = assigned_to_filter | ||
@format_filter = format_filter | ||
@title_filter = title_filter | ||
end | ||
|
||
def available_users | ||
User.enabled.alphabetized | ||
end | ||
|
||
def editions | ||
Edition.all | ||
result = editions_by_format | ||
result = apply_states_filter(result) | ||
result = apply_assigned_to_filter(result) | ||
apply_title_filter(result) | ||
end | ||
|
||
private | ||
|
||
def editions_by_format | ||
return Edition.all unless format_filter && format_filter != "all" | ||
|
||
Edition.where(_type: "#{format_filter.camelcase}Edition") | ||
end | ||
|
||
def apply_states_filter(editions) | ||
return editions if states_filter.empty? | ||
|
||
editions.in_states(states_filter) | ||
end | ||
|
||
def apply_assigned_to_filter(editions) | ||
return editions unless assigned_to_filter | ||
|
||
if assigned_to_filter == "nobody" | ||
editions = editions.assigned_to(nil) | ||
else | ||
begin | ||
assigned_user = User.find(assigned_to_filter) | ||
editions = editions.assigned_to(assigned_user) if assigned_user | ||
rescue Mongoid::Errors::DocumentNotFound | ||
Rails.logger.warn "An attempt was made to filter by an unknown user ID: '#{assigned_to_filter}'" | ||
end | ||
end | ||
editions | ||
end | ||
|
||
def apply_title_filter(editions) | ||
return editions if title_filter.blank? | ||
|
||
editions.title_contains(title_filter) | ||
end | ||
|
||
attr_reader :states_filter, :assigned_to_filter, :format_filter, :title_filter | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
test/unit/presenters/filtered_editions_presenter_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class FilteredEditionsPresenterTest < ActiveSupport::TestCase | ||
context "#editions" do | ||
should "return all editions when no filters are specified" do | ||
draft_guide = FactoryBot.create(:guide_edition, state: "draft") | ||
published_guide = FactoryBot.create(:guide_edition, state: "published") | ||
|
||
filtered_editions = FilteredEditionsPresenter.new.editions | ||
|
||
assert_equal(2, filtered_editions.count) | ||
assert_equal(draft_guide, filtered_editions[0]) | ||
assert_equal(published_guide, filtered_editions[1]) | ||
end | ||
|
||
should "filter by state" do | ||
draft_guide = FactoryBot.create(:guide_edition, state: "draft") | ||
FactoryBot.create(:guide_edition, state: "published") | ||
|
||
filtered_editions = FilteredEditionsPresenter.new(states_filter: %w[draft]).editions | ||
|
||
assert_equal(1, filtered_editions.count) | ||
assert_equal(draft_guide, filtered_editions[0]) | ||
end | ||
|
||
should "filter by 'assigned to'" do | ||
anna = FactoryBot.create(:user, name: "Anna") | ||
assigned_to_anna = FactoryBot.create(:guide_edition, assigned_to: anna.id) | ||
FactoryBot.create(:guide_edition) | ||
|
||
filtered_editions = FilteredEditionsPresenter.new(assigned_to_filter: anna.id).editions.to_a | ||
|
||
assert_equal([assigned_to_anna], filtered_editions) | ||
end | ||
|
||
should "filter by 'not assigned'" do | ||
anna = FactoryBot.create(:user, name: "Anna") | ||
FactoryBot.create(:guide_edition, assigned_to: anna.id) | ||
not_assigned = FactoryBot.create(:guide_edition) | ||
|
||
filtered_editions = FilteredEditionsPresenter.new(assigned_to_filter: "nobody").editions.to_a | ||
|
||
assert_equal([not_assigned], filtered_editions) | ||
end | ||
|
||
should "ignore invalid 'assigned to'" do | ||
anna = FactoryBot.create(:user, name: "Anna") | ||
FactoryBot.create(:guide_edition, assigned_to: anna.id) | ||
FactoryBot.create(:guide_edition) | ||
|
||
filtered_editions = | ||
FilteredEditionsPresenter.new(assigned_to_filter: "not a valid user id").editions | ||
|
||
assert_equal(2, filtered_editions.count) | ||
end | ||
|
||
should "filter by format" do | ||
guide = FactoryBot.create(:guide_edition) | ||
FactoryBot.create(:completed_transaction_edition) | ||
|
||
filtered_editions = FilteredEditionsPresenter.new(format_filter: "guide").editions | ||
|
||
assert_equal([guide], filtered_editions) | ||
end | ||
|
||
should "return all formats when specified by the format filter" do | ||
FactoryBot.create(:guide_edition) | ||
FactoryBot.create(:completed_transaction_edition) | ||
|
||
filtered_editions = FilteredEditionsPresenter.new(format_filter: "all").editions | ||
|
||
assert_equal(2, filtered_editions.count) | ||
end | ||
|
||
should "filter by a partially-matching title" do | ||
guide_fawkes = FactoryBot.create(:guide_edition, title: "Guide Fawkes") | ||
FactoryBot.create(:guide_edition, title: "Hitchhiker's Guide") | ||
|
||
filtered_editions = FilteredEditionsPresenter.new(title_filter: "Fawkes").editions | ||
|
||
assert_equal([guide_fawkes], filtered_editions) | ||
end | ||
end | ||
|
||
context "#available_users" do | ||
should "return users in alphabetical order" do | ||
bob = FactoryBot.create(:user, name: "Bob") | ||
charlie = FactoryBot.create(:user, name: "Charlie") | ||
anna = FactoryBot.create(:user, name: "Anna") | ||
|
||
users = FilteredEditionsPresenter.new.available_users.to_a | ||
|
||
assert_equal([anna, bob, charlie], users) | ||
end | ||
|
||
should "not include disabled users" do | ||
enabled_user = FactoryBot.create(:user, name: "enabled user") | ||
FactoryBot.create(:user, name: "disabled user", disabled: true) | ||
|
||
users = FilteredEditionsPresenter.new.available_users.to_a | ||
|
||
assert_equal(users, [enabled_user]) | ||
end | ||
end | ||
end |