Module: Oauned::Models::Authorization

Defined in:
lib/oauned/models/authorization.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/oauned/models/authorization.rb', line 2

def self.included(klass)
  Oauned::Models['authorization'] = klass

  klass.class_eval do
    before_validation    :set_default,
      :on => :create

    def expires_in
      (expires_at - Time.now).to_i
    end
    def expired?
      expires_in <= 0
    end

    def tokenize!
      self.destroy
      Oauned::Models['connection'].create!(:user_id => user_id, :application_id => application_id)
    end

    private
    def set_default
      self.code = SecureRandom.hex(20)
      self.expires_at = 1.hour.from_now
    end
  end
end