Class: SimpleUser::User

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_new_auth(auth) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/simple_user/user.rb', line 19

def self.build_new_auth(auth)
  email = ""

  if auth['provider'] == 'facebook'
    user = User.find_by_email(auth['extra']['raw_info']['email'])
  end

  user ||= User.new
  user.apply_omniauth(auth)
  return user
end

Instance Method Details

#apply_omniauth(auth) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/simple_user/user.rb', line 31

def apply_omniauth(auth)  
  #Generates random usernames and passwords for the first time
  if self.id.nil?
    tmp_password = generate_random(8)
    tmp_username = generate_random(6)
  end

  #Gets AUTH information
  if auth['provider'] == 'facebook'
    #Saves username and email for the first time
    if self.id.nil?
      tmp_username = auth['extra']['raw_info']['username'] rescue generate_random(6)
      self.email = auth['extra']['raw_info']['email']
    end

    self.first_name = auth['extra']['raw_info']['first_name'] if self.first_name.nil?
    self.last_name = auth['extra']['raw_info']['last_name'] if self.last_name.nil?
  end

  #Check if the username exists in the database and if it exists replace it with a new one, for the first time
  if self.id.nil?
    invalid_username = true

    while invalid_username
      tmp_user = User.find_by_username(tmp_username) rescue nil
      if tmp_user.nil?
        invalid_username = false
      else
        tmp_username = generate_random(6)
      end
    end
  end

  #Sets main values for the first time
  if self.id.nil?
    self.username = tmp_username
    self.password = tmp_password 
    self.password_confirmation = tmp_password
  end

  #Saves the authentication
  authentications.build(:provider => auth['provider'], :uid => auth['uid'], :token => auth['credentials']['token'])
end

#banned?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/simple_user/user.rb', line 79

def banned?
  !active
end

#full_nameObject



75
76
77
# File 'app/models/simple_user/user.rb', line 75

def full_name
  first_name.to_s + " " + last_name.to_s
end

#social_picture(provider = "facebook", width = 160, height = 129) ⇒ Object



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

def social_picture(provider = "facebook", width = 160, height = 129)
  auth = authentications.where(:provider => provider).first rescue nil

  tmp_image = case provider
        when "facebook"
            auth.nil? ? "" : "http://graph.facebook.com/#{auth.uid}/picture?width=#{width}&height=#{height}"
        else
            ""
  end

  return tmp_image
end