From f6e507366930ae7fe72ac605ba053fbd166279af Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Tue, 8 Dec 2020 20:30:18 -0500 Subject: [PATCH] Introduce User#lower_email for case insensitive email lookups --- app/models/user.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index a0b3d82a494..1d0e21d65d8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -109,12 +109,19 @@ def self.lookup_by_email(email) # often we have the most probably user object onhand. so use that if possible def self.lookup_by_lower_email(email, cache = []) email = email.downcase - Array.wrap(cache).detect { |u| u.email.try(:downcase) == email } || find_by(['lower(email) = ?', email]) + Array.wrap(cache).detect { |u| u.lower_email == email } || find_by(:lower_email => email) end singleton_class.send(:alias_method, :find_by_lower_email, :lookup_by_lower_email) Vmdb::Deprecation.deprecate_methods(singleton_class, :find_by_lower_email => :lookup_by_lower_email) + def lower_email + email&.downcase + end + + virtual_attribute :lower_email, :string, :arel => ->(t) { t.grouping(t[:email].lower) } + hidden_attribute :lower_email + def lower_userid userid&.downcase end