Skip to content

Commit

Permalink
AccessToken functions
Browse files Browse the repository at this point in the history
  • Loading branch information
iTakeshi committed Jul 18, 2014
1 parent a4a7742 commit 3cc12a7
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/models/access_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,35 @@
#

class AccessToken < ActiveRecord::Base
TERM_OF_VALIDITY = 2.weeks

belongs_to :user

validates(:user_id) { presence }
validates(:token) { presence; uniqueness }

before_create :generate_token, :update_last_activity_time

def self.delete_expired
self
.where
.not(last_activity_at: (Time.now - TERM_OF_VALIDITY)..Time.now)
.delete_all
end

def expired?
Time.now - TERM_OF_VALIDITY > self.last_activity_at
end

def update_last_activity_time
self.last_activity_at = Time.now
end

private

def generate_token
begin
self.token = SecureRandom.hex(32)
end while self.class.find_by(token: self.token)
end
end

0 comments on commit 3cc12a7

Please sign in to comment.