-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathuser.rb
60 lines (45 loc) · 1.35 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
# frozen_string_literal: true
module Spree
class User < Spree::Base
include UserMethods
devise :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable, :encryptable
devise :confirmable if Spree::Auth::Config[:confirmable]
if defined?(Spree::SoftDeletable)
include Spree::SoftDeletable
else
acts_as_paranoid
include Spree::ParanoiaDeprecations
include Discard::Model
self.discard_column = :deleted_at
end
after_destroy :scramble_email_and_password
after_discard :scramble_email_and_password
def password=(new_password)
generate_spree_api_key if new_password.present? && spree_api_key.present?
super
end
scope :admin, -> { includes(:spree_roles).where("#{Role.table_name}.name" => "admin") }
def self.admin_created?
User.admin.count > 0
end
def admin?
has_spree_role?('admin')
end
def confirmed?
!!confirmed_at
end
protected
def password_required?
!persisted? || password.present? || password_confirmation.present?
end
private
def scramble_email_and_password
return true if destroyed?
self.email = SecureRandom.uuid + "@example.net"
self.password = SecureRandom.hex(8)
self.password_confirmation = password
save
end
end
end