diff --git a/app/models/user.rb b/app/models/user.rb index 0bb7e62a3..e1470ae07 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -113,8 +113,8 @@ class User < ApplicationRecord joins(:comments).where("comments.commentable": commentables).distinct end scope :by_username_email_or_document_number, ->(search_string) do - string = "%#{search_string}%" - where("username ILIKE ? OR email ILIKE ? OR document_number ILIKE ?", string, string, string) + search = "%#{search_string.strip}%" + where("username ILIKE ? OR email ILIKE ? OR document_number ILIKE ?", search, search, search) end scope :between_ages, ->(from, to) do where( diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index f0bf8afd3..2eb0eae8a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -413,6 +413,38 @@ describe User do expect(User.erased).not_to include(user3) 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 describe "self.search" do