设计`find_first_by_auth_conditions`方法解释 [英] Devise `find_first_by_auth_conditions` method explanation
问题描述
我使用 Devise 的两种方法:
My two methods using Devise:
方法一
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
方法二
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
我的问题:
- 这段代码执行/做什么?
login = conditions.delete(:signin)
- 没有上面的代码,我得到一个错误
undefined local variable or method signin
推荐答案
以下回答问题1)
——特别是A)
和B)
下面.以下代码是一个示例,并不反映 Devise 生成的实际方法或参数:
The following answers question 1)
—specifically A)
and B)
below. The following code is an example and does not mirror the actual methods or arguments generated by Devise:
这里:Hash
包含 :signin
键值对和其他有效的 ActiveRecord
的 #where
语法http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-where
Here: the Hash
contains :signin
key-value pair and 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'}
这会复制原始参数以防止修改以便在后续方法或查询中使用它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'}
这里,代码:A) 从 Hash
中删除 :signin
密钥对;和B) 使用 :signin
密钥对的值从 Hash
分配新变量 signin
http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-delete
Here, the code: A) deletes the :signin
key-pair from the Hash
; and
B) assigns new variable signin
with value of :signin
key-pair from Hash
http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-delete
signin = conditions.delete(:signin)
#=> "cool@gmail.com"
可以使用 Hash
的附加元素引用"重写上面的代码以阐明这两个操作http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-5B-5D
The immediately above code could be rewritten to 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 from Hash is returned
conditions
#=> {:deleted => false, :role => 'basic'}
使用dup
devise_conditions
#=> {:signin=>"cool@gmail.com", :deleted => false, :role => 'basic'}
<小时>
以下回答问题2)
:
Method1 不会创建变量 signin
.未定义的局部变量或方法登录
是由于创建它的代码被删除时没有创建signin
变量.
Method1 does not create a variable signin
. undefined local variable or method signin
results from no signin
variable being created when the code which creates it is removed.
Method2 创建一个变量 login
,该变量具有来自名为 conditions
的原始 Hash
的值,键为 <代码>:登录代码>.
Method2 creates a variable login
which has the value from the original Hash
named conditions
with the key :signin
.
这篇关于设计`find_first_by_auth_conditions`方法解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!