Allow search User through name with whitespaces

This commit is contained in:
taitus
2021-03-25 10:43:53 +01:00
parent 3796ccc874
commit 95503f5811
2 changed files with 12 additions and 1 deletions

View File

@@ -302,7 +302,10 @@ class User < ApplicationRecord
end
def self.search(term)
term.present? ? where("email = ? OR username ILIKE ?", term.strip, "%#{term}%") : none
return none if term.blank?
search = term.strip
where("email = ? OR username ILIKE ?", search, "%#{search}%")
end
def self.username_max_length

View File

@@ -440,6 +440,14 @@ describe User do
expect(search).to eq [user1]
end
it "find users by name with whitespaces" do
user1 = create(:user, username: "Larry Bird")
create(:user, username: "Robert Parish")
search = User.search(" larry ")
expect(search).to eq [user1]
end
it "returns no results if no search term provided" do
expect(User.search(" ")).to be_empty
end