Class: ProxES::User

Inherits:
Base
  • Object
show all
Defined in:
lib/proxes/models/user.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/proxes/models/user.rb', line 20

def method_missing(method_sym, *arguments, &block)
  if method_sym.to_s[-1] == '?'
    role?(method_sym[0..-2])
  else
    super
  end
end

Instance Method Details

#after_createObject

Add the basic roles and identity



45
46
47
# File 'lib/proxes/models/user.rb', line 45

def after_create
  check_roles
end

#check_rolesObject



49
50
51
52
# File 'lib/proxes/models/user.rb', line 49

def check_roles
  add_role Role.find_or_create(name: 'user') unless role?('user')
  add_role Role.find_or_create(name: 'super_admin') if id == 1 && ENV['RACK_ENV'] != 'test' && !role?('super_admin')
end

#gravatarObject



32
33
34
35
# File 'lib/proxes/models/user.rb', line 32

def gravatar
  hash = Digest::MD5.hexdigest(email.downcase)
  "https://www.gravatar.com/avatar/#{hash}"
end

#index_prefixObject



54
55
56
# File 'lib/proxes/models/user.rb', line 54

def index_prefix
  email
end

#respond_to_missing?(name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/proxes/models/user.rb', line 28

def respond_to_missing?(name, _include_private = false)
  name[-1] == '?'
end

#role?(check) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/proxes/models/user.rb', line 16

def role?(check)
  !roles_dataset.first(name: check).nil?
end

#usernameObject



58
59
60
# File 'lib/proxes/models/user.rb', line 58

def username
  identity_dataset.first.username
end

#validateObject



37
38
39
40
41
42
# File 'lib/proxes/models/user.rb', line 37

def validate
  validates_presence :email
  return if email.blank?
  validates_unique :email
  validates_format(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :email)
end