-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.rb
60 lines (42 loc) · 1.55 KB
/
user.rb
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
class User < ApplicationRecord
## ATTRIBUTES:
# - name:string
# - username:string
# - email:string
# - password_digest:string
## RELATIONSHIP:
has_many :own_conversations, class_name: 'Conversation', foreign_key: 'user_one_id', dependent: :destroy
has_many :guest_conversations, class_name: 'Conversation', foreign_key: 'user_two_id', dependent: :destroy
has_many :messages, dependent: :destroy
has_many :contacts, class_name: 'Contact', foreign_key: 'user_id', dependent: :destroy
## MODEL HELPERS
has_secure_password
has_one_attached :image
# MODEL VALIDATIONS
validates :name,
presence: { message: "Name must filled!" }
validates :username,
presence: { message: "Username must filled!" },
uniqueness: { message: "This username already taken!" },
format: { without: /\s/ }
validates :email,
presence: { message: "Email must be filled!" },
uniqueness: { message: "This email already used!" },
format: { with: URI::MailTo::EMAIL_REGEXP, message: "Email format must be valid!" }
validates :password_digest,
presence: { message: "Password must filled!" }
# MODEL FUNCTION
# fetch all user conversations
def conversation_list
Conversation.user_conversation(self.id)
end
def contact_list
Contact.where('user_id = ?', self.id)
end
def self.search term
where('lower(name) LIKE lower(?) or lower(username) LIKE lower(?)', "%#{ term }%", "%#{ term }%").first
end
def self.find_by_credential(credential)
where('username = ? or email = ?', credential, credential).first
end
end