Merge pull request #4442 from consul/user_search
Improve user search by email/name
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(
|
||||||
@@ -310,7 +310,10 @@ class User < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.search(term)
|
def self.search(term)
|
||||||
term.present? ? where("email = ? OR username ILIKE ?", term, "%#{term}%") : none
|
return none if term.blank?
|
||||||
|
|
||||||
|
search = term.strip
|
||||||
|
where("email = ? OR username ILIKE ?", search, "%#{search}%")
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.username_max_length
|
def self.username_max_length
|
||||||
|
|||||||
@@ -413,23 +413,61 @@ 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
|
||||||
it "find users by email" do
|
describe "find users" do
|
||||||
user1 = create(:user, email: "larry@consul.dev")
|
let!(:larry) { create(:user, email: "larry@consul.dev", username: "Larry Bird") }
|
||||||
create(:user, email: "bird@consul.dev")
|
|
||||||
search = User.search("larry@consul.dev")
|
|
||||||
|
|
||||||
expect(search).to eq [user1]
|
before { create(:user, email: "robert@consul.dev", username: "Robert Parish") }
|
||||||
end
|
|
||||||
|
|
||||||
it "find users by name" do
|
it "by email" do
|
||||||
user1 = create(:user, username: "Larry Bird")
|
expect(User.search("larry@consul.dev")).to eq [larry]
|
||||||
create(:user, username: "Robert Parish")
|
end
|
||||||
search = User.search("larry")
|
|
||||||
|
|
||||||
expect(search).to eq [user1]
|
it "by email with whitespaces" do
|
||||||
|
expect(User.search(" larry@consul.dev ")).to eq [larry]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "by name" do
|
||||||
|
expect(User.search("larry")).to eq [larry]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "by name with whitespaces" do
|
||||||
|
expect(User.search(" larry ")).to eq [larry]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns no results if no search term provided" do
|
it "returns no results if no search term provided" do
|
||||||
|
|||||||
Reference in New Issue
Block a user