Add missing files :(
This commit is contained in:
parent
b69cd9df2a
commit
4182608f39
3 changed files with 69 additions and 0 deletions
45
app/models/bulk_email_session.rb
Normal file
45
app/models/bulk_email_session.rb
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'securerandom'
|
||||
|
||||
class BulkEmailSession < ApplicationRecord
|
||||
before_validation :generate_token, on: :create
|
||||
before_validation :set_expiration, on: :create
|
||||
|
||||
validates :token, presence: true, uniqueness: true
|
||||
validates :recipient_emails, presence: true
|
||||
|
||||
serialize :recipient_emails, type: Array
|
||||
|
||||
# Sessions expire after 1 hour to prevent database bloat
|
||||
EXPIRATION_TIME = 1.hour
|
||||
|
||||
scope :active, -> { where('expires_at > ?', Time.current) }
|
||||
scope :expired, -> { where('expires_at <= ?', Time.current) }
|
||||
|
||||
def self.cleanup_expired!
|
||||
expired.delete_all
|
||||
end
|
||||
|
||||
def expired?
|
||||
expires_at <= Time.current
|
||||
end
|
||||
|
||||
def recipient_count
|
||||
recipient_emails&.size || 0
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_token
|
||||
return if token.present?
|
||||
|
||||
self.token = SecureRandom.hex(16)
|
||||
end
|
||||
|
||||
def set_expiration
|
||||
return if expires_at.present?
|
||||
|
||||
self.expires_at = EXPIRATION_TIME.from_now
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue