ruby on rails - Devise `find_first_by_auth_conditions` explanation -


this question has answer here:

i having tough time understanding code devise, though i've read documentation , done research.

def self.find_first_by_auth_conditions(warden_conditions)   conditions = warden_conditions.dup   signin = conditions.delete(:signin)   where(conditions).where(["lower(username) = :value or lower(email) =     :value", {:value => signin.downcase }]).first end 

please explain components of portion of above method:

where(conditions).where(["lower(username) = :value or lower(email) =   :value", {:value => signin.downcase }]).first 

# arg hash, assign variable , downcase x = warden_conditions[:signin].downcase  # create duplicate preserve orig c = warden_conditions.dup  # delete `:signin` c.delete(:signin)  # if email, search email if x =~ /^[a-za-z0-9._-]+@[a-za-z0-9.-]+\.[a-za-z]{2,4}$/   y = self.where(c).where(:email => x) # self implied, optional--so use here clarity   # if not email, search name else   y = self.where(c).where(:username => x) end  # y array, should 1 ar obj or empty array considering unique # `array#first` return `nil` or ar obj return y.first 

regex via: validate email regex jquery

the above code considers previous records columns email , username stored lowercase follows:

before_save :downcase_fields  def downcase_fields   self.email.downcase   self.username.downcase end 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -