Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
ApiHandling, FriendlyId
Defined in:
app/models/user.rb

Defined Under Namespace

Classes: DuplicateNickname

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiHandling

expose_api

Class Method Details

.authenticated_with_token(id, stored_salt) ⇒ Object


94
95
96
97
# File 'app/models/user.rb', line 94

def authenticated_with_token(id, stored_salt)
  user = find_by_id(id)
  user && user.salt == stored_salt ? user : nil
end

.find_by_session_or_cookies(session, cookies) ⇒ Object


99
100
101
# File 'app/models/user.rb', line 99

def find_by_session_or_cookies(session, cookies)
  find_by_id(session[:user_id]) || authenticated_with_token(*(cookies.signed[:remember_me] || ['', '']))
end

.find_or_create_from_hash!(hash) ⇒ Object


84
85
86
87
88
89
90
91
92
# File 'app/models/user.rb', line 84

def find_or_create_from_hash!(hash)
  provider = hash['provider']
  nickname = hash['info']['nickname']
  user = self.find_or_initialize_by(provider => nickname)
  if !user.persisted? && self.find_by_nickname(nickname)
    raise DuplicateNickname.new(nickname)
  end
  user.update_from_auth! hash
end

Instance Method Details

#handle_github_attributes(hash) ⇒ Object


60
61
62
63
64
65
66
67
68
69
# File 'app/models/user.rb', line 60

def handle_github_attributes(hash)
  self.nickname     = hash['info']['nickname'] unless self.nickname
  self.github       = hash['info']['nickname']
  self.email        = hash['info']['email'] unless self.email
  self.name         = hash['info']['name'].blank? ? hash['info']['nickname'] : hash['info']['name']
  self.image        = hash['info']['image']
  self.url          = hash['info']['urls']['Blog'] || hash['info']['urls']['GitHub']
  self.description  = hash['extra']['raw_info']['bio'] unless self.description
  self.location     = hash['extra']['raw_info']['location']
end

#handle_twitter_attributes(hash) ⇒ Object


50
51
52
53
54
55
56
57
58
# File 'app/models/user.rb', line 50

def handle_twitter_attributes(hash)
  self.nickname     = hash['info']['nickname'] unless self.nickname
  self.twitter      = hash['info']['nickname']
  self.name         = hash['info']['name']
  self.image        = hash['info']['image']
  self.url          = hash['info']['urls']['Website']
  self.description  = hash['info']['description'] unless self.description
  self.location     = hash['info']['location']
end

#participates?(event) ⇒ Boolean


27
28
29
# File 'app/models/user.rb', line 27

def participates?(event)
  participants.any? { |participant| participant.event_id == event.id }
end

#participation(event) ⇒ Object


31
32
33
# File 'app/models/user.rb', line 31

def participation(event)
  participants.where(event_id: event.id).first
end

#saltObject


40
41
42
# File 'app/models/user.rb', line 40

def salt
  authorizations.first.uid
end

#update_from_auth!(hash) ⇒ Object


44
45
46
47
48
# File 'app/models/user.rb', line 44

def update_from_auth!(hash)
  send :"handle_#{hash['provider']}_attributes", hash
  save! if changed?
  self
end

#urlObject


35
36
37
38
# File 'app/models/user.rb', line 35

def url
  return unless url = read_attribute(:url)
  url =~ /\Ahttps?:\/\/.+/ ? url : "http://#{url}"
end

#with_provider?(provider) ⇒ Boolean


71
72
73
# File 'app/models/user.rb', line 71

def with_provider?(provider)
  authorizations.map(&:provider).include?("#{provider}")
end