2012-07-17 56 views
0

我试图omniauth身份集成到我的应用程序。根据自述文件,我们只需要写:omniauth身份AUTH_KEY

class Identity < OmniAuth::Identity::Models::ActiveRecord 
    # Add whatever you like! 
end 

但是我想添加唯一性验证。所以简单的方法是此验证添加到Identity类:

validates_uniqueness_of :email, :case_sensitive => false 

但后来当我浏览过的宝石源代码,我看到OmniAuth::Identity::Models::ActiveRecord这个auth_key=方法如下:

def self.auth_key=(key) 
     super 
     validates_uniqueness_of key, :case_sensitive => false 
    end 

而且因为我讨厌重复的代码,所以我想用现有的方法而不是另外编写一行代码。所以我改变了身份类到

class Identity < OmniAuth::Identity::Models::ActiveRecord 
    # Add whatever you like! 
    auth_key :email 
end 

但我仍然遇到重复的电子邮件(它看起来像验证不起作用)。因此,我尝试了以下Identity.auth_key = 'my_key',它给了我错误NoMethodError: super: no superclass method auth_key ='为#'

任何想法我在这里做错了吗?当然我可以将auth_key =方法定义更改为OmniAuth :: Identity :: Models :: ActiveRecord中的auth_key,但我讨厌这样做,因为我认为我在这里丢失了某些东西。

谢谢

+0

我的答案在下面有帮助吗? – Jurgen 2012-09-27 21:00:32

回答

3

你几乎没有......你提供Identity类和OmniAuth::Identity::Models::ActiveRecord继承,并指定列OmniAuth标识应使用定位记录,只需使用auth_key setter方法。任何验证应包括在你的Identity类,因为它们通常会与其他任何ActiveRecord模型。

auth_key仅仅是您选择定位进行身份验证时记录的列的getter/setter(虚拟属性),它是不是列本身,除非您选择创建您的身份模型的auth_key

还要注意,通过OmniAuth身份所寻求的默认方法是#email属性(https://github.com/intridea/omniauth-identity/blob/master/lib/omniauth/identity/model.rb#L41)所以设置auth_key是多余的,如果你选择坚持与#email属性。

# app/models/identity.rb 
class Identity < OmniAuth::Identity::Models::ActiveRecord 
    belongs_to :user 
    attr_accessible :email, :password, :password_confirmation, :user_id 

    validates :email, :presence => true, :uniqueness => true, :case_sensitive => false 
    validates :password, :presence => true, :confirmation => true 
    validates :password_confirmation, :presence => true 
end 

# db/migrate/xxxxxxxxxxxxxx_create_identities.rb 
class CreateIdentities < ActiveRecord::Migration 
    def change 
    create_table :identities, :force => true do |t| 
     t.column :email, :string, :null => false 
     t.column :password_digest, :string 
     t.column :user_id, :integer, :null => false 
    end 

    change_table :identities do |t| 
     t.index :email, { :unique => true } 
     t.index :user_id 
    end 
    end 
end 

# config/initializers/omniauth.rb 
use OmniAuth::Builder do 
    provider :identity, :fields => [:email] 
end 

如果您决定更改AUTH_KEY列到别的东西,例如#username,那么您需要使用auth_key setter方法如下:

# app/models/identity.rb 
class Identity < OmniAuth::Identity::Models::ActiveRecord 
    auth_key 'username' 
    belongs_to :user 
    attr_accessible :password, :password_confirmation, :username, :user_id 

    validates :password, :presence => true, :confirmation => true 
    validates :password_confirmation, :presence => true 
    validates :username, :presence => true, :uniqueness => true 
end 

# db/migrate/xxxxxxxxxxxxxx_create_identities.rb 
class CreateIdentities < ActiveRecord::Migration 
    def change 
    create_table :identities, :force => true do |t| 
     t.column :password_digest, :string 
     t.column :username, :string, :null => false 
     t.column :user_id, :integer, :null => false 
    end 

    change_table :identities do |t| 
     t.index :username, { :unique => true } 
     t.index :user_id 
    end 
    end 
end 

# config/initializers/omniauth.rb 
use OmniAuth::Builder do 
    provider :identity, :fields => [:username] 
end 

注意,auth_key方法接受一个字符串参数和不是象attr_accessible这样的符号。

OmniAuth身份是非常灵活,你可以充分利用,以满足现有项目其他几个自定义。您可以为您的身份模型设置自定义类,并且可以自定义在验证时找到匹配记录的方式。见https://github.com/intridea/omniauth-identity/blob/master/README.markdown

我希望这一切可以帮助,我知道这让我困惑了一段时间,我不得不钻研和理解OmniAuth身份的源代码。

+0

我无法找到auth_key方法为我的生活,谢谢。 – 2013-06-20 23:01:10

相关问题