module JamRuby
  class UserAuthorization < ActiveRecord::Base

    attr_accessible :provider, :uid, :token, :token_expiration, :secret, :user

    self.table_name = "user_authorizations"

    self.primary_key = 'id'

    belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
    validates :provider, :uid, :user, :presence => true
    validates_uniqueness_of :uid, scope: :provider
    # token, secret, token_expiration can be missing

    def self.google_auth(user)
      self
        .where(:user_id => user.id)
        .where(:provider => 'google_login')
        .where(['token_expiration IS NULL OR token_expiration > ?', Time.now])
        .limit(1)
    end

  end
end
