-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathuser.rb
More file actions
83 lines (71 loc) · 2.22 KB
/
user.rb
File metadata and controls
83 lines (71 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# frozen_string_literal: true
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# first_name :string default(""), not null
# last_name :string default(""), not null
# email :string default(""), not null
# encrypted_password :string default("")
# reset_password_token :string
# reset_password_sent_at :datetime
# remember_created_at :datetime
# sign_in_count :integer default("0"), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string
# last_sign_in_ip :string
# confirmation_token :string
# confirmed_at :datetime
# confirmation_sent_at :datetime
# unconfirmed_email :string
# created_at :datetime
# updated_at :datetime
# organization_id :integer
# slug :string
# invitation_token :string
# invitation_created_at :datetime
# invitation_sent_at :datetime
# invitation_accepted_at :datetime
# invitation_limit :integer
# invited_by_id :integer
# invited_by_type :string
# invitations_count :integer default("0")
#
class User < ApplicationRecord
include Sluggable
devise :database_authenticatable,
:recoverable,
:rememberable,
:trackable,
:validatable,
:confirmable,
:invitable
validates_presence_of :first_name, :last_name
validate :unique_email_address?
has_one :account, foreign_key: "owner_id", inverse_of: :owner
belongs_to :organization, class_name: "Account", inverse_of: :users
has_many :hours
has_many :mileages
has_many :projects, -> { uniq }, through: :hours
scope :by_name, -> { order("lower(last_name)") }
def full_name
"#{first_name} #{last_name}"
end
alias slug_source full_name
alias label full_name
alias name full_name
def email_domain
email.split("@").last
end
def color
(first_name + last_name).pastel_color
end
def acronyms
first_name[0] + last_name[0]
end
def unique_email_address?
return false if User.where(email: self.email).any?
end
end