Allow scope :by_username_email_or_document_number search users with whitespaces

This commit is contained in:
taitus
2021-04-12 11:36:40 +02:00
parent 65f734bf3d
commit 85aba8830a
2 changed files with 34 additions and 2 deletions

View File

@@ -113,8 +113,8 @@ class User < ApplicationRecord
joins(:comments).where("comments.commentable": commentables).distinct
end
scope :by_username_email_or_document_number, ->(search_string) do
string = "%#{search_string}%"
where("username ILIKE ? OR email ILIKE ? OR document_number ILIKE ?", string, string, string)
search = "%#{search_string.strip}%"
where("username ILIKE ? OR email ILIKE ? OR document_number ILIKE ?", search, search, search)
end
scope :between_ages, ->(from, to) do
where(

View File

@@ -413,6 +413,38 @@ describe User do
expect(User.erased).not_to include(user3)
end
end
describe ".by_username_email_or_document_number" do
let!(:larry) do
create(:user, email: "larry@consul.dev", username: "Larry Bird", document_number: "12345678Z")
end
before { create(:user, email: "robert@consul.dev", username: "Robert Parish") }
it "finds users by email" do
expect(User.by_username_email_or_document_number("larry@consul.dev")).to eq [larry]
end
it "finds users by email with whitespaces" do
expect(User.by_username_email_or_document_number(" larry@consul.dev ")).to eq [larry]
end
it "finds users by name" do
expect(User.by_username_email_or_document_number("larry")).to eq [larry]
end
it "finds users by name with whitespaces" do
expect(User.by_username_email_or_document_number(" larry ")).to eq [larry]
end
it "finds users by document_number" do
expect(User.by_username_email_or_document_number("12345678Z")).to eq [larry]
end
it "finds users by document_number with whitespaces" do
expect(User.by_username_email_or_document_number(" 12345678Z ")).to eq [larry]
end
end
end
describe "self.search" do