mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 14:31:25 +00:00
* Initial sec * Update PII fields * FIX add tests * FIX safely read plaintext data on rake backfill * Update user.rb * FIX tests * encryption_ready? block * Test conditional to encryption on --------- Signed-off-by: Juan José Mata <juanjo.mata@gmail.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
33 lines
634 B
Ruby
33 lines
634 B
Ruby
class InviteCode < ApplicationRecord
|
|
include Encryptable
|
|
|
|
# Encrypt token if ActiveRecord encryption is configured
|
|
if encryption_ready?
|
|
encrypts :token, deterministic: true, downcase: true
|
|
end
|
|
|
|
before_validation :generate_token, on: :create
|
|
|
|
class << self
|
|
def claim!(token)
|
|
if invite_code = find_by(token: token&.downcase)
|
|
invite_code.destroy!
|
|
true
|
|
end
|
|
end
|
|
|
|
def generate!
|
|
create!.token
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def generate_token
|
|
loop do
|
|
self.token = SecureRandom.hex(4)
|
|
break token unless self.class.exists?(token: token)
|
|
end
|
|
end
|
|
end
|