forked from moneyadviceservice/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.rb
95 lines (77 loc) · 2.69 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class User < ActiveRecord::Base
VALID_AGE_RANGES = ['0-15', '16-17', '18-20', '21-24', '25-34', '35-44', '45-54', '55-64', '65-74', '75+']
def field_order
[:first_name, :email, :password, :post_code]
end
devise :registerable,
:database_authenticatable,
:encryptable,
:lockable,
:trackable,
:timeoutable,
:recoverable
# :confirmable,
# :invitable,
before_validation :uppercase_post_code
validates_with Validators::Email, attributes: [:email]
validates_with Validators::DateOfBirth, attributes: [:date_of_birth]
validates :email, uniqueness: true
validates :post_code, presence: true,
format: { with: /\A[A-Z]{1,2}\d{1,2}[A-NP-Z]? ?\d[A-Z]{2}\z/, if: 'post_code.present?' },
on: :create
validates :password, presence: true, on: :create
validates :password, length: 7..128, allow_blank: true
validates :first_name, presence: true,
format: { with: /\A[A-Za-z '-\.]+\z/, if: 'last_name.present?' },
length: { maximum: 24 }
validates :last_name, format: { with: /\A[A-Za-z '-\.]+\z/ },
allow_blank: true,
length: { maximum: 24 }
validates :gender, inclusion: { in: %w(female male) }, allow_nil: true
validates :age_range, inclusion: { in: VALID_AGE_RANGES }, allow_nil: true
before_save :fake_send_confirmation_email
before_create :create_to_crm
after_update :update_to_crm
def valid_for_authentication?
super && active? && Core::Registry::Repository[:customer].valid_for_authentication?(customer_id)
end
def to_customer
Converters::UserToCustomer.new(self).call
end
def registered?
!!accept_terms_conditions
end
def invitation_sent?
invitation_sent_at.present?
end
def invited_by
invited_by_id.present?
end
def update_goal!(statement, deadline)
self[:goal_statement] = statement
self[:goal_deadline] = deadline
save!
end
def send_devise_notification(notification, *args)
devise_mailer.delay(queue: 'frontend_email').send(notification, self, *args)
end
def data_for?(tool_name)
method = "data_for_#{tool_name}?".to_sym
send(method) if respond_to?(method)
end
private
def create_to_crm
Core::Interactors::Customer::Creator.new(self).call
end
def update_to_crm
Core::Interactors::Customer::Updater.new(self).call
end
def uppercase_post_code
post_code.upcase! if post_code
end
def fake_send_confirmation_email
# Temporary fix to trick Devise into thinking an email confirmation is sent to the user
# so they can sign in desktop site.
self.confirmation_sent_at = DateTime.now
end
end