0

omn​​iauth-identity をアプリケーションに統合しようとしています。README ファイルによると、次のように書くだけです。

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

ただし、一意性の検証を追加したいと思います。簡単な方法は、この検証を Identity クラスに追加することです。

validates_uniqueness_of :email, :case_sensitive => false

しかし、gem のソース コードを参照すると、このauth_key=メソッドがOmniAuth::Identity::Models::ActiveRecord次のように表示されます。

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

また、コードの重複が嫌いなので、1 行追加するのではなく、既存のメソッドを使用したいと考えています。そこで、Identity クラスを次のように変更しました

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

しかし、まだメールが重複しています (検証が機能していないようです)。したがって、次のことを試してみたところ、#` に対して auth_key=' というIdentity.auth_key = 'my_key'エラーが発生しましたNoMethodError: super: no superclass method

ここで何が間違っていたのか分かりますか?もちろん、OmniAuth::Identity::Models::ActiveRecord で auth_key= メソッド定義を auth_key に変更することもできますが、ここに何かが欠けていると思うので、変更するのは嫌いです。

ありがとうございました

4

1 に答える 1

3

もう少しです...Identityクラスを指定して から継承しOmniAuth::Identity::Models::ActiveRecord、OmniAuth ID がレコードを見つけるために使用する列を指定するには、auth_keysetter メソッドを使用するだけです。Identity通常、他のActiveRecordモデルと同様に、すべての検証をクラスに含める必要があります。

これauth_keyは、認証時にレコードを見つけるために選択した列のゲッター/セッター (仮想属性) であり、ID モデルで列を作成することを選択しない限り、列自体ではありません。auth_key

また、OmniAuth Identity が求めるデフォルトのメソッドは#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

# 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 ID は非常に柔軟で、既存のプロジェクトに合わせて利用できるカスタマイズが他にもいくつかあります。ID モデルのカスタム クラスを設定し、認証時に一致するレコードを見つける方法をカスタマイズできます。https://github.com/intridea/omniauth-identity/blob/master/README.markdownを参照してください。

しばらく混乱していたので、OmniAuth ID のソース コードを詳しく調べて理解する必要がありました。

于 2012-08-31T19:57:13.917 に答える