Class: User

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.admin_roleObject


82
# File 'app/models/user.rb', line 82

def self.admin_role; "admin"; end

.fetch_email(username) ⇒ Object


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

def self.fetch_email(username)
  user = self.find_by_username(username) if username
  user.email if user
end

.fetch_unconfirmed_email(username) ⇒ Object


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

def self.fetch_unconfirmed_email(username)
  user = self.find_by_username(username) if username
  (user.unconfirmed_email.blank? ? user.email : user.unconfirmed_email) if user
end

.from_omniauth(auth) ⇒ Object


22
23
24
25
26
27
28
29
30
# File 'app/models/user.rb', line 22

def self.from_omniauth(auth)
  user = where(auth.slice(:provider, :uid)).first_or_create do |new_user|
    new_user.provider = auth.provider
    new_user.uid = auth.uid
    new_user.username = auth.info.nickname
  end
  user.image_url = auth.info.image # need to update in case image changed on provider's site
  user
end

.guest_roleObject


83
# File 'app/models/user.rb', line 83

def self.guest_role; "guest"; end

.manager_roleObject


84
# File 'app/models/user.rb', line 84

def self.manager_role; "manager"; end

.new_with_session(params, session) ⇒ Object


32
33
34
35
36
37
38
39
40
41
# File 'app/models/user.rb', line 32

def self.new_with_session(params, session)
  if session["devise.user_attributes"]
    new(session["devise.user_attributes"], without_protection: true) do |user|
      user.attributes = params
      user.valid?
    end
  else
    super
  end    
end

.role_collectionObject


86
87
88
# File 'app/models/user.rb', line 86

def self.role_collection
  [['guest', 'guest'],['user','user'],['manager','manager']]
end

.role_collection_with_adminObject


90
91
92
# File 'app/models/user.rb', line 90

def self.role_collection_with_admin
  [['guest', 'guest'],['user','user'],['manager','manager'],['admin','admin']]
end

.user_exists_but_email_is_unconfirmed?(username) ⇒ Boolean

end

Returns:

  • (Boolean)

59
60
61
62
63
# File 'app/models/user.rb', line 59

def self.user_exists_but_email_is_unconfirmed?(username)
  user = find_by_username(username)
  return user.email_unconfirmed? if user
  false
end

.user_roleObject

handle roles


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

def self.user_role; "user"; end

Instance Method Details

#email_unconfirmed?Boolean

Returns:

  • (Boolean)

65
66
67
# File 'app/models/user.rb', line 65

def email_unconfirmed?
  (!self.email.blank? && !self.confirmed?) || !self.unconfirmed_email.blank?
end

#is_admin?Boolean

Returns:

  • (Boolean)

105
106
107
# File 'app/models/user.rb', line 105

def is_admin?
  self.role == 'admin'
end

#password_required?Boolean

Returns:

  • (Boolean)

43
44
45
# File 'app/models/user.rb', line 43

def password_required?
  super && provider.blank?
end

#role?(role) ⇒ Boolean

Returns:

  • (Boolean)

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

def role?(role)
  return true if role == 'admin' && self.role == 'admin'
  return true if role == 'manager' && self.role == 'manager'
  return true if role == 'user' && self.role == 'user'
  return true if role == 'guest' && ( self.role.nil? || self.role == 'guest')
end

#role_access?(role) ⇒ Boolean

Returns:

  • (Boolean)

109
110
111
112
113
114
115
116
# File 'app/models/user.rb', line 109

def role_access?(role)
  return false if (role.nil? || self.role.nil? || !valid_role?(self.role))
  return true if role == 'guest'
  return true if role == 'admin' && self.role == 'admin'
  return true if role == 'manager' && ['manager', 'admin'].include?(self.role)
  return true if role == 'user' && ['user', 'manager', 'admin'].include?(self.role)
  return false
end

#update_with_password(params, *options) ⇒ Object


47
48
49
50
51
52
53
# File 'app/models/user.rb', line 47

def update_with_password(params, *options)
  if encrypted_password.blank?
    update_attributes(params, *options)
  else
    super
  end
end

#valid_role?(role) ⇒ Boolean

Returns:

  • (Boolean)

101
102
103
# File 'app/models/user.rb', line 101

def valid_role?(role)
  ['guest','user', 'manager', 'admin'].include?(role)
end