class User < ActiveRecord::Base
  devise :omniauthable, :api_token_authenticatable

  has_many :tokens

  attr_accessible :name, :login, :email, :github_id, :github_oauth_token

  after_create :create_a_token

  class << self
    def find_for_github_oauth(user_hash)
      user_details = user_data_from_github_data(user_hash)

      if user = User.find_by_github_id(user_details['uid'])
        user.update_attributes(user_details)
        user
      else
        create!(user_details)
      end
    end

    def user_data_from_github_data(user_hash)
      user_info = user_hash['user_info']
      {
        'name'  => user_info['name'],
        'email' => user_info['email'],
        'login' => user_info['nickname'],
        'github_id' => user_hash['uid'],
        'github_oauth_token' => user_hash['credentials']['token']
      }
    end
  end

  def profile_image_hash
    self.email? ? Digest::MD5.hexdigest(self.email) : '00000000000000000000000000000000'
  end

  private

  def create_a_token
    self.tokens.create!
  end
end

class User < ActiveRecord::Base
  devise :omniauthable, :api_token_authenticatable

  has_many :tokens

  attr_accessible :name, :login, :email, :github_id, :github_oauth_token

  after_create :create_a_token

  class << self
    def find_for_github_oauth(user_hash)
      user_details = user_data_from_github_data(user_hash)

      if user = User.find_by_github_id(user_details['github_uid'])
        user.update_attributes(user_details)
        user
      else
        create!(user_details)
      end
    end

    def user_data_from_github_data(user_hash)
      user_info = user_hash['user_info']
      {
        'name'  => user_info['name'],
        'email' => user_info['email'],
        'login' => user_info['nickname'],
        'github_id' => user_hash['uid'],
        'github_oauth_token' => user_hash['credentials']['token']
      }
    end
  end

  def profile_image_hash
    self.email? ? Digest::MD5.hexdigest(self.email) : '00000000000000000000000000000000'
  end

  private

  def create_a_token
    self.tokens.create!
  end
end

Revert "a small typo => 500 error"
Revert "a small typo => 500 error"

This reverts commit 60a80d41ff88850333f29026b7310c7219a0bc5f.
