Class: User

Inherits:
ApplicationModel show all
Defined in:
lib/scaffold/app/models/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ModelBase

all, #attribute_values, #attributes, columns, #destroy, finalize!, find, first, #initialize, last, parse_all, #save, #save!, table_name, table_name=, #update, #update_attributes

Methods included from Searchable

#find_by, #joins, #left_joins, #select, #where

Methods included from Associatable

#assoc_options, #belongs_to, #has_many, #has_many_through, #has_one

Methods included from ModelCallbacks

#after_init, #before_valid, included

Methods included from Validations

#errors, #valid?

Constructor Details

This class inherits a constructor from ModelBase

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



13
14
15
# File 'lib/scaffold/app/models/user.rb', line 13

def password
  @password
end

Class Method Details

.find_by_credentials(username, password) ⇒ Object



15
16
17
18
# File 'lib/scaffold/app/models/user.rb', line 15

def self.find_by_credentials(username, password)
  user = User.find_by(username: username)
  user && user.is_password?(password) ? user : nil
end

.generate_tokenObject



20
21
22
# File 'lib/scaffold/app/models/user.rb', line 20

def self.generate_token
  SecureRandom::urlsafe_base64
end

Instance Method Details

#ensure_tokenObject



29
30
31
# File 'lib/scaffold/app/models/user.rb', line 29

def ensure_token
  self.session_token ||= User.generate_token
end

#is_password?(password) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/scaffold/app/models/user.rb', line 39

def is_password?(password)
  BCrypt::Password.new(self.password_digest).is_password?(password)
end

#reset_token!Object



33
34
35
36
37
# File 'lib/scaffold/app/models/user.rb', line 33

def reset_token!
  self.session_token = User.generate_token
  self.save!
  self.session_token
end