Allow scope :by_username_email_or_document_number search users with whitespaces
This commit is contained in:
@@ -113,8 +113,8 @@ class User < ApplicationRecord
|
|||||||
joins(:comments).where("comments.commentable": commentables).distinct
|
joins(:comments).where("comments.commentable": commentables).distinct
|
||||||
end
|
end
|
||||||
scope :by_username_email_or_document_number, ->(search_string) do
|
scope :by_username_email_or_document_number, ->(search_string) do
|
||||||
string = "%#{search_string}%"
|
search = "%#{search_string.strip}%"
|
||||||
where("username ILIKE ? OR email ILIKE ? OR document_number ILIKE ?", string, string, string)
|
where("username ILIKE ? OR email ILIKE ? OR document_number ILIKE ?", search, search, search)
|
||||||
end
|
end
|
||||||
scope :between_ages, ->(from, to) do
|
scope :between_ages, ->(from, to) do
|
||||||
where(
|
where(
|
||||||
|
|||||||
@@ -413,6 +413,38 @@ describe User do
|
|||||||
expect(User.erased).not_to include(user3)
|
expect(User.erased).not_to include(user3)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
describe "self.search" do
|
describe "self.search" do
|
||||||
|
|||||||
Reference in New Issue
Block a user