Class: Login::User

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.provider(auth, provider_id) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/login/user.rb', line 14

def self.provider auth, provider_id
  raise "Not enough information provided" unless auth.info.nickname.present? || auth.info.email.present?
  find_hash = { email:auth.info.email }
  find_hash = { github:auth.info.nickname } if provider_id == :github
  find_hash = { twitter:auth.info.nickname } if provider_id == :twitter
  user = where(find_hash).first
  twitter, github = "", ""
  twitter = auth.info.nickname if provider_id == :twitter
  github = auth.info.nickname if provider_id == :github
  first_name = auth.info.first_name.presence || auth.info.name.split.first || ""
  last_name = auth.info.last_name.presence || auth.info.name.split.last || ""
  transaction do
    if user
      user.update_attribute :twitter, twitter unless user.twitter.present?
      user.update_attribute :first_name, first_name unless user.first_name.present?
      user.update_attribute :last_name, last_name unless user.last_name.present?
    else
      create_hash = { github: github, twitter: twitter, first_name: first_name, last_name: last_name, password: Devise.friendly_token[0,20] }
      create_hash[:email] = auth.info.email.presence
      user = create! create_hash
    end
    user.providers.find_or_create auth
  end
end

Instance Method Details

#display_nameObject



39
40
41
42
43
44
# File 'app/models/login/user.rb', line 39

def display_name
  return screen_name if screen_name.present?
  return "@#{github}" if github.present?
  return "@#{twitter}" if twitter.present?
  return email.gsub(/@(.).*$/,'@\1')
end