This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 349
/
Copy pathuser.rb
53 lines (42 loc) · 1.56 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
class User < ActiveRecord::Base
# FIXME - DRY up, repeated in Story model
JSON_ATTRIBUTES = ["id", "name", "initials", "email"]
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
# Flag used to identify if the user was found or created from find_or_create
attr_accessor :was_created
has_and_belongs_to_many :projects, -> { uniq }
before_validation :set_random_password_if_blank
validates :name, :presence => true
validates :initials, :presence => true
def password_required?
# Password is required if it is being set, but not for new records
if !persisted?
false
else
!password.nil? || !password_confirmation.nil?
end
end
def to_s
"#{name} (#{initials}) <#{email}>"
end
def set_random_password_if_blank
if new_record? && self.password.blank? && self.password_confirmation.blank?
self.password = self.password_confirmation = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--")[0,8]
end
end
# Sets :reset_password_token encrypted by Devise
# returns the raw token to pass into mailer
def set_reset_password_token
raw, enc = Devise.token_generator.generate(self.class, :reset_password_token)
self.reset_password_token = enc
self.reset_password_sent_at = Time.now.utc
self.save(:validate => false)
raw
end
def as_json(options = {})
super(:only => JSON_ATTRIBUTES)
end
end