我在这个Railscast之后实现了omniauth-identity。
所以我最终得到了这样的结果:
身份.rb:
class Identity < OmniAuth::Identity::Models::ActiveRecord
attr_accessible :name, :email, :password, :password_confirmation
validates_presence_of :name
validates_uniqueness_of :email
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
end
用户.rb:
class User < ActiveRecord::Base
def self.from_omniauth(auth)
find_by_provider_and_uid(auth["provider"], auth["uid"]) || create_with_omniauth(auth)
end
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
user.email = auth["info"]["email"]
end
end
架构.rb:
create_table "identities", :force => true do |t|
t.string "name"
t.string "email"
t.string "password_digest"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "admin", :default => false
t.string "uid"
t.string "provider"
t.string "name"
t.string "email"
end
现在,我不太确定如何使用FactoryGirl创建用户,因为密码正在由Identity
模型处理。
如果我省略密码,我可以创建用户,但如果没有密码,我将无法登录。
我真的不知道如何使这件事发挥作用。有一个通用的解决方案吗?