Class: Ditty::User
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- Ditty::User
show all
- Includes:
- Base
- Defined in:
- lib/ditty/models/user.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Base
#display_id, #for_json
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &block) ⇒ Object
34
35
36
37
38
39
40
|
# File 'lib/ditty/models/user.rb', line 34
def method_missing(method_sym, *arguments, &block)
if respond_to_missing?(method_sym)
role?(method_sym[0..-2])
else
super
end
end
|
Class Method Details
.anonymous_user ⇒ Object
85
86
87
88
|
# File 'lib/ditty/models/user.rb', line 85
def anonymous_user
role = ::Ditty::Role.find_or_create(name: 'anonymous')
::Ditty::User.where(roles: role).first
end
|
.create_anonymous_user(email = '[email protected]') ⇒ Object
90
91
92
93
94
95
96
|
# File 'lib/ditty/models/user.rb', line 90
def create_anonymous_user(email = '[email protected]')
return if anonymous_user
user = ::Ditty::User.find_or_create(email: email)
user.remove_role ::Ditty::Role.find_or_create(name: 'user')
user.add_role ::Ditty::Role.find_or_create(name: 'anonymous') unless user.role?('anonymous')
end
|
Instance Method Details
#after_create ⇒ Object
Add the basic roles and identity
61
62
63
64
|
# File 'lib/ditty/models/user.rb', line 61
def after_create
super
check_roles
end
|
#all_roles ⇒ Object
28
29
30
31
32
|
# File 'lib/ditty/models/user.rb', line 28
def all_roles
roles.inject([]) do |memo, role|
memo + [role] + role.descendants
end.uniq
end
|
#check_roles ⇒ Object
66
67
68
69
70
71
|
# File 'lib/ditty/models/user.rb', line 66
def check_roles
return if roles_dataset.first(name: 'anonymous')
return if roles_dataset.first(name: 'user')
add_role Role.find_or_create(name: 'user')
end
|
#display_name ⇒ Object
80
81
82
|
# File 'lib/ditty/models/user.rb', line 80
def display_name
name || username
end
|
#gravatar ⇒ Object
46
47
48
49
|
# File 'lib/ditty/models/user.rb', line 46
def gravatar
hash = Digest::MD5.hexdigest(email.downcase)
"https://www.gravatar.com/avatar/#{hash}"
end
|
#respond_to_missing?(name, _include_private = false) ⇒ Boolean
42
43
44
|
# File 'lib/ditty/models/user.rb', line 42
def respond_to_missing?(name, _include_private = false)
name[-1] == '?'
end
|
#role?(check) ⇒ Boolean
18
19
20
21
22
23
24
25
26
|
# File 'lib/ditty/models/user.rb', line 18
def role?(check)
@roles ||= Hash.new do |h, k|
role_or_descendant = roles.find do |role|
role.name == k || role.descendants.map(&:name).include?(k)
end
h[k] = !role_or_descendant.nil?
end
@roles[check]
end
|
#username ⇒ Object
73
74
75
76
77
78
|
# File 'lib/ditty/models/user.rb', line 73
def username
identity = identity_dataset.first
return identity.username if identity
email
end
|
#validate ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'lib/ditty/models/user.rb', line 51
def validate
super
validates_presence :email
return if email.blank?
validates_unique :email
validates_format(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :email)
end
|