Merge pull request #4442 from consul/user_search

Improve user search by email/name
This commit is contained in:
Javi Martín
2021-04-13 18:31:34 +02:00
committed by GitHub
2 changed files with 55 additions and 14 deletions

View File

@@ -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(
@@ -310,7 +310,10 @@ class User < ApplicationRecord
end
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
def self.username_max_length