From 1e9de14e5f5aee6c3379268e872d4813021250d7 Mon Sep 17 00:00:00 2001 From: Richard Pattinson Date: Wed, 6 Sep 2023 16:38:04 +0100 Subject: [PATCH] Remove leading and trailing whitespace from filters --- app/lib/filters/email.rb | 2 +- app/lib/filters/name.rb | 5 ++++- app/lib/filters/reference.rb | 2 +- spec/lib/filters/email_spec.rb | 34 ++++++++++++++++++++++++++++++ spec/lib/filters/name_spec.rb | 30 ++++++++++++++++++++++++++ spec/lib/filters/reference_spec.rb | 22 +++++++++++++++++++ 6 files changed, 92 insertions(+), 3 deletions(-) diff --git a/app/lib/filters/email.rb b/app/lib/filters/email.rb index 95bbd6648b..01dfcfd220 100644 --- a/app/lib/filters/email.rb +++ b/app/lib/filters/email.rb @@ -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 diff --git a/app/lib/filters/name.rb b/app/lib/filters/name.rb index 6d9dd06f70..582e26ddc5 100644 --- a/app/lib/filters/name.rb +++ b/app/lib/filters/name.rb @@ -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 diff --git a/app/lib/filters/reference.rb b/app/lib/filters/reference.rb index 650aad04b4..0ab021df88 100644 --- a/app/lib/filters/reference.rb +++ b/app/lib/filters/reference.rb @@ -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 diff --git a/spec/lib/filters/email_spec.rb b/spec/lib/filters/email_spec.rb index b55deaa503..f06ee63c23 100644 --- a/spec/lib/filters/email_spec.rb +++ b/spec/lib/filters/email_spec.rb @@ -31,6 +31,40 @@ it { is_expected.to contain_exactly(included) } end + + context "with trailing whitespace" do + let(:params) { { email: "Jane.Smith@example.com " } } + let(:scope) { ApplicationForm.all } + let!(:included) { create(:application_form, teacher:) } + + before do + create( + :application_form, + teacher: create(:teacher, email: "jane.jones@example.com"), + ) + end + + it "returns a filtered scope" do + expect(subject).to contain_exactly(included) + end + end + + context "with leading whitespace" do + let(:params) { { email: " Jane.Smith@example.com" } } + let(:scope) { ApplicationForm.all } + let!(:included) { create(:application_form, teacher:) } + + before do + create( + :application_form, + teacher: create(:teacher, email: "jane.jones@example.com"), + ) + end + + it "returns a filtered scope" do + expect(subject).to contain_exactly(included) + end + end end context "the params don't include email" do diff --git a/spec/lib/filters/name_spec.rb b/spec/lib/filters/name_spec.rb index 8beb51f701..1b7bb4c0f3 100644 --- a/spec/lib/filters/name_spec.rb +++ b/spec/lib/filters/name_spec.rb @@ -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" } } diff --git a/spec/lib/filters/reference_spec.rb b/spec/lib/filters/reference_spec.rb index c2ff36178a..c69e683fd7 100644 --- a/spec/lib/filters/reference_spec.rb +++ b/spec/lib/filters/reference_spec.rb @@ -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