-
Notifications
You must be signed in to change notification settings - Fork 0
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 #144 from alphagov/filter-class
Extract filtering into a class of its own
- Loading branch information
Showing
4 changed files
with
60 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module DiscoveryEngine | ||
class Filters | ||
def initialize(query_params) | ||
@query_params = query_params | ||
end | ||
|
||
def filter_expression | ||
reject_links_filter | ||
end | ||
|
||
private | ||
|
||
attr_reader :query_params | ||
|
||
def reject_links_filter | ||
return nil if query_params[:reject_link].blank? | ||
|
||
reject_links = Array(query_params[:reject_link]).map { "\"#{_1}\"" }.join(",") | ||
"NOT link: ANY(#{reject_links})" | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
RSpec.describe DiscoveryEngine::Filters do | ||
describe "#filter_expression" do | ||
subject(:filter_expression) { described_class.new(query_params).filter_expression } | ||
|
||
context "when no relevant query params are present" do | ||
let(:query_params) { {} } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context "with a single reject_link parameter" do | ||
let(:query_params) { { q: "garden centres", reject_link: "/foo" } } | ||
|
||
it "calls the client with the expected parameters" do | ||
expect(filter_expression).to eq('NOT link: ANY("/foo")') | ||
end | ||
end | ||
|
||
context "with several reject_link parameter" do | ||
let(:query_params) { { q: "garden centres", reject_link: ["/foo", "/bar"] } } | ||
|
||
it "calls the client with the expected parameters" do | ||
expect(filter_expression).to eq('NOT link: ANY("/foo","/bar")') | ||
end | ||
end | ||
end | ||
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