Skip to content

Commit

Permalink
Merge pull request #1668 from DFE-Digital/2241-trim-whitespace-from-f…
Browse files Browse the repository at this point in the history
…ilters

Remove leading and trailing whitespace from filters
  • Loading branch information
richardpattinson authored Sep 14, 2023
2 parents 34f3df4 + 1e9de14 commit c150561
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/lib/filters/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Filters
class Email < Base
def apply
if email.present?
scope.joins(:teacher).where("teachers.email = ?", email.downcase)
scope.joins(:teacher).where("teachers.email = ?", email.strip.downcase)
else
scope
end
Expand Down
5 changes: 4 additions & 1 deletion app/lib/filters/name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ class Name < Base
def apply
return scope if name.blank?

scope.where("CONCAT(given_names, ' ', family_name) ilike ?", "%#{name}%")
scope.where(
"CONCAT(given_names, ' ', family_name) ilike ?",
"%#{name.strip}%",
)
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/lib/filters/reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Filters
class Reference < Base
def apply
if reference.present?
scope.where("reference ILIKE ?", "%#{reference}%")
scope.where("reference ILIKE ?", "%#{reference.strip}%")
else
scope
end
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/filters/email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,40 @@

it { is_expected.to contain_exactly(included) }
end

context "with trailing whitespace" do
let(:params) { { email: "[email protected] " } }
let(:scope) { ApplicationForm.all }
let!(:included) { create(:application_form, teacher:) }

before do
create(
:application_form,
teacher: create(:teacher, email: "[email protected]"),
)
end

it "returns a filtered scope" do
expect(subject).to contain_exactly(included)
end
end

context "with leading whitespace" do
let(:params) { { email: " [email protected]" } }
let(:scope) { ApplicationForm.all }
let!(:included) { create(:application_form, teacher:) }

before do
create(
:application_form,
teacher: create(:teacher, email: "[email protected]"),
)
end

it "returns a filtered scope" do
expect(subject).to contain_exactly(included)
end
end
end

context "the params don't include email" do
Expand Down
30 changes: 30 additions & 0 deletions spec/lib/filters/name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,36 @@
end
end

context "with trailing whitespace" do
let(:params) { { name: "Jane Smith " } }
let(:scope) { ApplicationForm.all }
let!(:included) do
create(:application_form, given_names: "Jane", family_name: "Smith")
end
let!(:excluded) do
create(:application_form, given_names: "Tom", family_name: "Bombadil")
end

it "returns a filtered scope" do
expect(subject).to contain_exactly(included)
end
end

context "with leading whitespace" do
let(:params) { { name: " Jane Smith" } }
let(:scope) { ApplicationForm.all }
let!(:included) do
create(:application_form, given_names: "Jane", family_name: "Smith")
end
let!(:excluded) do
create(:application_form, given_names: "Tom", family_name: "Bombadil")
end

it "returns a filtered scope" do
expect(subject).to contain_exactly(included)
end
end

context "match with different case" do
let(:params) { { name: "daVe" } }

Expand Down
22 changes: 22 additions & 0 deletions spec/lib/filters/reference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,26 @@

it { is_expected.to eq(scope) }
end

context "with trailing whitespace" do
let(:params) { { reference: "ABCDEF " } }
let(:scope) { ApplicationForm.all }
let!(:included) { create(:application_form, reference: "ABCDEF") }
let!(:excluded) { create(:application_form, reference: "QRHGF") }

it "returns a filtered scope" do
expect(subject).to contain_exactly(included)
end
end

context "with leading whitespace" do
let(:params) { { reference: " ABCDEF" } }
let(:scope) { ApplicationForm.all }
let!(:included) { create(:application_form, reference: "ABCDEF") }
let!(:excluded) { create(:application_form, reference: "QRHGF") }

it "returns a filtered scope" do
expect(subject).to contain_exactly(included)
end
end
end

0 comments on commit c150561

Please sign in to comment.