From 3796ccc8744af354a50eced5ad3ad2ec831c7ead Mon Sep 17 00:00:00 2001 From: taitus Date: Thu, 25 Mar 2021 10:41:27 +0100 Subject: [PATCH] Allow search User through email with whitespaces --- app/models/user.rb | 2 +- spec/models/user_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index f20db4bea..35dd762cc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -302,7 +302,7 @@ class User < ApplicationRecord end def self.search(term) - term.present? ? where("email = ? OR username ILIKE ?", term, "%#{term}%") : none + term.present? ? where("email = ? OR username ILIKE ?", term.strip, "%#{term}%") : none end def self.username_max_length diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index f10eefed7..0b7eb3c20 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -424,6 +424,14 @@ describe User do expect(search).to eq [user1] end + it "find users by email with whitespaces" do + user1 = create(:user, email: "larry@consul.dev") + create(:user, email: "bird@consul.dev") + search = User.search(" larry@consul.dev ") + + expect(search).to eq [user1] + end + it "find users by name" do user1 = create(:user, username: "Larry Bird") create(:user, username: "Robert Parish")