30 lines
481 B
Ruby
30 lines
481 B
Ruby
class ApiToken < ActiveRecord::Base
|
|
|
|
ApiTokenTakenError = Class.new(StandardError)
|
|
|
|
belongs_to :user
|
|
|
|
validates :user, :token, presence: true
|
|
validates :token, uniqueness: true
|
|
|
|
attr_accessible :token
|
|
|
|
def self.for_token(token)
|
|
ApiToken.where(token: token).first
|
|
end
|
|
|
|
def reassign_to(target_user)
|
|
return if target_user == user
|
|
raise ApiTokenTakenError if taken?
|
|
|
|
user.merge_to(target_user)
|
|
end
|
|
|
|
private
|
|
|
|
def taken?
|
|
!user.dummy?
|
|
end
|
|
|
|
end
|