ruby on rails - Devise `find_first_by_auth_conditions` method explanation -
my 2 methods using devise:
method1
def self.find_first_by_auth_conditions(warden_conditions) conditions = warden_conditions.dup where(conditions).where(["lower(username) = :value or lower(email) = :value", {:value => signin.downcase }]).first end method2
def self.find_for_database_authentication(warden_conditions) conditions = warden_conditions.dup login = conditions.delete(:signin) where(conditions).where(["lower(username) = :value or lower(email) = :value", {:value => login.strip.downcase }]).first end my questions:
- what code perform/do?
login = conditions.delete(:signin) - without above code error
undefined local variable or method signin
the following answers question 1)—specifically a) , b) below. following code example , not mirror actual methods or arguments generated devise:
here: hash contains :signin key-value pair , other valid activerecord's #where syntax http://api.rubyonrails.org/classes/activerecord/querymethods.html#method-i-where
devise_conditions = {:signin => "cool@gmail.com", :deleted => false, :role => 'basic'} #=> {:signin=>"cool@gmail.com", :deleted => false, :role => 'basic'} this duplicates original argument prevent modification in order use in subsequent methods or queries http://ruby-doc.org/core-1.9.3/object.html#method-i-dup
conditions = devise_conditions.dup #=> {:signin=>"cool@gmail.com", :deleted => false, :role => 'basic'} here, code: a) deletes :signin key-pair hash; , b) assigns new variable signin value of :signin key-pair hash http://www.ruby-doc.org/core-1.9.3/hash.html#method-i-delete
signin = conditions.delete(:signin) #=> "cool@gmail.com" the above code rewritten clarify both operations using additional "element reference" of hash http://www.ruby-doc.org/core-1.9.3/hash.html#method-i-5b-5d
signin = conditions[:signin] #=> "cool@gmail.com" conditions.delete(:signin) #=> "cool@gmail.com" # deleted value hash returned conditions #=> {:deleted => false, :role => 'basic'} the method's original argument has been preserved using dup
devise_conditions #=> {:signin=>"cool@gmail.com", :deleted => false, :role => 'basic'} the following answers question 2):
method1 not create variable signin. undefined local variable or method signin results no signin variable being created when code creates removed.
method2 creates variable login has value original hash named conditions key :signin.
Comments
Post a Comment