Class: Lines::User

Inherits:
ApplicationRecord show all
Defined in:
app/models/lines/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#reset_tokenObject

Returns the value of attribute reset_token.



21
22
23
# File 'app/models/lines/user.rb', line 21

def reset_token
  @reset_token
end

Class Method Details

.digest(string) ⇒ Object

Returns the hash digest of the given string.



54
55
56
57
# File 'app/models/lines/user.rb', line 54

def User.digest(string)
  cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
  BCrypt::Password.create(string, cost: cost)
end

.generate_tokenObject

Generate a random token.



49
50
51
# File 'app/models/lines/user.rb', line 49

def User.generate_token
  SecureRandom.urlsafe_base64
end

Instance Method Details

#authenticated?(attribute, token) ⇒ Boolean

Returns true if the given token matches the digest.

Returns:

  • (Boolean)


41
42
43
44
45
# File 'app/models/lines/user.rb', line 41

def authenticated?(attribute, token)
  digest = send("#{attribute}_digest")
  return false if digest.nil?
  BCrypt::Password.new(digest).is_password?(token)
end

#create_reset_digestObject

Sets rest_digest and reset_sent_at for password reset.



29
30
31
32
33
# File 'app/models/lines/user.rb', line 29

def create_reset_digest
  self.reset_token = Lines::User.generate_token
  update_attribute(:reset_digest,  Lines::User.digest(reset_token))
  update_attribute(:reset_sent_at, Time.zone.now)
end

#password_reset_expired?Boolean

Returns true if a password reset has expired.

Returns:

  • (Boolean)


60
61
62
# File 'app/models/lines/user.rb', line 60

def password_reset_expired?
  reset_sent_at < 2.hours.ago
end

#send_password_reset_emailObject

Sends email with instructions how to reset password.



36
37
38
# File 'app/models/lines/user.rb', line 36

def send_password_reset_email
  UserMailer.password_reset(self).deliver_now
end