Class: Authorization

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/authorization.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_from_hash(hash, existing_user = nil) ⇒ Object



18
19
20
21
22
23
# File 'app/models/authorization.rb', line 18

def self.create_from_hash(hash, existing_user = nil)
  create do |authorization|
    authorization.(hash)
    authorization.find_or_create_or_associate_user(existing_user)
  end
end

.find_from_hash(hash) ⇒ Object



25
26
27
# File 'app/models/authorization.rb', line 25

def self.find_from_hash(hash)
  find_by_provider_and_uid(hash['provider'], hash['uid'])
end

.find_or_create_from_hash(hash, existing_user = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'app/models/authorization.rb', line 8

def self.find_or_create_from_hash(hash, existing_user = nil)
  if (auth = find_from_hash(hash))
    auth.(hash)
    auth.save
    auth
  else
    create_from_hash(hash, existing_user)
  end
end

Instance Method Details

#allow_destroy?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'app/models/authorization.rb', line 40

def allow_destroy?
  if user.authorizations.count.eql?(1)
    errors.add(:base, "You must have at least one authorization provider.")
    raise ActiveRecord::Rollback
  end
end

#assign_account_info(auth_hash) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/authorization.rb', line 47

def (auth_hash)
  self.uid                 = auth_hash['uid']
  self.provider            = auth_hash['provider']
  self.nickname            = auth_hash['info']['nickname']
  self.email               = auth_hash['info']['email']
  self.picture_url         = auth_hash['info']['image']
  self.name                = auth_hash['info']['name']
  if auth_hash['credentials']
    self.access_token        = auth_hash['credentials']['token']
    self.access_token_secret = auth_hash['credentials']['secret']
  end
end

#find_or_create_or_associate_user(existing_user = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'app/models/authorization.rb', line 29

def find_or_create_or_associate_user(existing_user = nil)
  if existing_user
    self.user = existing_user
  elsif self.user
    self.user
  else
    self.user = User.find_or_create_from_authorization(self)
  end
end