Class: Ditty::User

Inherits:
Sequel::Model
  • Object
show all
Includes:
Base
Defined in:
lib/ditty/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



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

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



46
47
48
# File 'lib/ditty/models/user.rb', line 46

def after_create
  check_roles
end

#check_rolesObject



50
51
52
53
# File 'lib/ditty/models/user.rb', line 50

def check_roles
  return if role?('anonymous')
  add_role Role.find_or_create(name: 'user') unless role?('user')
end

#gravatarObject



33
34
35
36
# File 'lib/ditty/models/user.rb', line 33

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

#index_prefixObject



55
56
57
# File 'lib/ditty/models/user.rb', line 55

def index_prefix
  email
end

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

Returns:

  • (Boolean)


29
30
31
# File 'lib/ditty/models/user.rb', line 29

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

#role?(check) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/ditty/models/user.rb', line 17

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

#usernameObject



59
60
61
# File 'lib/ditty/models/user.rb', line 59

def username
  identity_dataset.first.username
end

#validateObject



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

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