Allow search User through name with whitespaces
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user