Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/chatgpt_assistant/models.rb

Overview

User model

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



23
24
25
# File 'lib/chatgpt_assistant/models.rb', line 23

def password
  @password
end

Instance Method Details

#chat_by_title(title) ⇒ Object



75
76
77
# File 'lib/chatgpt_assistant/models.rb', line 75

def chat_by_title(title)
  chats.find_by(title: title)
end

#chat_historyObject



79
80
81
# File 'lib/chatgpt_assistant/models.rb', line 79

def chat_history
  current_chat.messages.last(10).map { |m| "#{m.role}: #{m.content}\nat: #{m.created_at}" }
end

#confirm_account(hash) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/chatgpt_assistant/models.rb', line 56

def (hash)
  confirmation = { email: email, password_hash: password_hash, time: created_at }
  secret = BCrypt::Engine.hash_secret(JSON.parse(confirmation.to_json), password_salt)
  return false unless secret == hash

  self.token = secret
  self.active = true
  save
  true
end

#current_chatObject



67
68
69
# File 'lib/chatgpt_assistant/models.rb', line 67

def current_chat
  chats.find(current_chat_id)
end

#encrypt_passwordObject



40
41
42
43
44
45
# File 'lib/chatgpt_assistant/models.rb', line 40

def encrypt_password
  return if password.nil?

  self.password_salt = BCrypt::Engine.generate_salt
  self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end

#encrypt_tokenObject



47
48
49
50
# File 'lib/chatgpt_assistant/models.rb', line 47

def encrypt_token
  confirmation = { email: email, password_hash: password_hash, time: created_at }
  BCrypt::Engine.hash_secret(JSON.parse(confirmation.to_json), password_salt)
end

#last_chatObject



71
72
73
# File 'lib/chatgpt_assistant/models.rb', line 71

def last_chat
  chats.last
end

#valid_password?(password) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/chatgpt_assistant/models.rb', line 52

def valid_password?(password)
  BCrypt::Engine.hash_secret(password, password_salt) == password_hash
end