Class: ProxES::User

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

Instance Method Summary collapse

Methods included from Base

#for_json

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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



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

def after_create
  check_roles
end

#check_rolesObject



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

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



34
35
36
37
# File 'lib/proxes/models/user.rb', line 34

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

#index_prefixObject



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

def index_prefix
  email
end

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

Returns:

  • (Boolean)


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

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

#role?(check) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/proxes/models/user.rb', line 18

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

#usernameObject



60
61
62
# File 'lib/proxes/models/user.rb', line 60

def username
  identity_dataset.first.username
end

#validateObject



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

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