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



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

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



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

def after_create
  check_roles
end

#check_rolesObject



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

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



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

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

#index_prefixObject



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

def index_prefix
  email
end

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

Returns:

  • (Boolean)


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

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

#role?(check) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#usernameObject



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

def username
  identity_dataset.first.username
end

#validateObject



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

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