Skip to content

Commit

Permalink
Introduce User#lower_email for case insensitive email lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Jan 7, 2021
1 parent c30555a commit 668e834
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
hide_attribute :lower_email

def lower_userid
userid&.downcase
end
Expand Down

0 comments on commit 668e834

Please sign in to comment.