Class: ConsumerToken

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps, Oauth::Models::Consumers::Token
Defined in:
lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb,
lib/generators/active_record/oauth_consumer_templates/consumer_token.rb

Class Method Summary collapse

Methods included from Oauth::Models::Consumers::Token

included

Class Method Details

.find_or_create_from_access_token(user, access_token) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb', line 22

def self.find_or_create_from_access_token(user,access_token)
  secret = access_token.respond_to?(:secret) ? access_token.secret : nil

  if user
    user.consumer_tokens.where(:_type=>self.to_s,:token=>access_token.token).first ||
      self.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>secret, :user=>user)
  else
    user = User.where("consumer_tokens._type"=>self.to_s,"consumer_tokens.token"=>access_token.token).first
    if user
      user.consumer_tokens.detect{|t| t.token==access_token.token && t.is_a?(self)}
    else
      user = User.new
      self.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>secret, :user=>user)
      user.save!
      user.consumer_tokens.last
    end
  end
end