-
Notifications
You must be signed in to change notification settings - Fork 9
/
user.rb
54 lines (38 loc) · 1.36 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
# frozen_string_literal: true
class User < ApplicationRecord
has_secure_password
has_many :memberships, dependent: :destroy
has_many :accounts, through: :memberships
has_many :task_lists, through: :accounts
has_many :task_items, through: :task_lists
has_one :ownership, -> { owner }, class_name: "Membership", inverse_of: :user, dependent: nil
has_one :account, through: :ownership
has_one :inbox, through: :account
has_one :token, class_name: "UserToken", dependent: :destroy
with_options presence: true do
validates :password, confirmation: true, length: {minimum: 8}, if: -> { new_record? || password.present? }
validates :email, format: {with: URI::MailTo::EMAIL_REGEXP}, uniqueness: true
end
normalizes :email, with: -> { _1.strip.downcase }
generates_token_for :reset_password, expires_in: 15.minutes do
password_salt&.last(10)
end
generates_token_for :email_confirmation, expires_in: 24.hours do
email
end
after_create do
account = Account.create!(uuid: SecureRandom.uuid)
account.memberships.create!(user: self, role: :owner)
account.task_lists.inbox.create!
create_token!
end
after_create_commit do
UserMailer.with(
user: self,
token: generate_token_for(:email_confirmation)
).email_confirmation.deliver_later
end
before_destroy prepend: true do
account.destroy!
end
end