0

我有一个使用 Omni-Auth GitHub gem 的 Rails 应用程序。通过 GitHub 创建和登录用户帐户完美无缺!

这是我的用户模型:

  def self.from_omniauth(auth)
    find_by_github_uid(auth["uid"]) || create_from_omniauth(auth)
  end

  def self.create_from_omniauth(auth)
    create! do |user|
      user.github_uid = auth["uid"]
      user.github_token = auth["credentials"]["token"]
      user.username = auth["info"]["nickname"]
      user.email = auth["info"]["email"]
      user.full_name = auth["info"]["name"]
      user.gravatar_id = auth["extra"]["raw_info"]["gravatar_id"]
      user.blog_url = auth["extra"]["raw_info"]["blog"]
      user.company = auth["extra"]["raw_info"]["company"]
      user.location = auth["extra"]["raw_info"]["location"]
      user.hireable = auth["extra"]["raw_info"]["hireable"]
      user.bio = auth["extra"]["raw_info"]["bio"]
    end
  end

但有时用户会更改他们的简历或公司,或者他们是否想被雇用,所以我不必删除旧帐户,我认为如果在他们更新帐户后及时更新它会很好。

执行此操作的最佳做​​法是什么?如何使用现有的 OmniAuth 代码更新用户信息?

4

1 回答 1

5
  def self.from_omniauth(auth)
    user = find_by_github_uid(auth["uid"]) || User.new
    user.assign_from_omniauth(auth).save!
  end

  def assign_from_omniauth(auth)
      self.github_uid ||= auth["uid"]
      self.github_token ||= auth["credentials"]["token"]
      self.username ||= auth["info"]["nickname"]
      self.email ||= auth["info"]["email"]
      self.full_name = auth["info"]["name"]
      self.gravatar_id = auth["extra"]["raw_info"]["gravatar_id"]
      self.blog_url = auth["extra"]["raw_info"]["blog"]
      self.company = auth["extra"]["raw_info"]["company"]
      self.location = auth["extra"]["raw_info"]["location"]
      self.hireable = auth["extra"]["raw_info"]["hireable"]
      self.bio = auth["extra"]["raw_info"]["bio"]
  end
于 2013-04-17T06:44:17.290 回答