Sha256: f47732503149f519c56c6a6b56c4bc1829f504ed21d6f8a09c2948d6253b1344

Contents?: true

Size: 952 Bytes

Versions: 25

Compression:

Stored size: 952 Bytes

Contents

# frozen_string_literal: true
class SingleUseLink < ActiveRecord::Base
  validate :expiration_date_cannot_be_in_the_past
  validate :cannot_be_destroyed

  alias_attribute :downloadKey, :download_key
  alias_attribute :itemId, :item_id

  after_initialize :set_defaults

  def self.generate_download_key
    (Digest::SHA2.new << rand(1_000_000_000).to_s).to_s
  end

  def create_for_path(path)
    self.class.create(item_id: item_id, path: path)
  end

  def expired?
    DateTime.current > expires
  end

  def to_param
    download_key
  end

  private

  def expiration_date_cannot_be_in_the_past
    errors.add(:expires, "can't be in the past") if expired?
  end

  def cannot_be_destroyed
    errors[:base] << "Single Use Link has already been used" if destroyed?
  end

  def set_defaults
    return unless new_record?
    self.expires ||= DateTime.current.advance(hours: 24)
    self.download_key ||= self.class.generate_download_key
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
hyrax-5.0.1 app/models/single_use_link.rb
hyrax-5.0.0 app/models/single_use_link.rb
hyrax-5.0.0.rc3 app/models/single_use_link.rb
hyrax-5.0.0.rc2 app/models/single_use_link.rb
hyrax-5.0.0.rc1 app/models/single_use_link.rb
hyrax-3.6.0 app/models/single_use_link.rb
hyrax-4.0.0 app/models/single_use_link.rb
hyrax-4.0.0.rc3 app/models/single_use_link.rb
hyrax-4.0.0.rc2 app/models/single_use_link.rb
hyrax-4.0.0.rc1 app/models/single_use_link.rb
hyrax-3.5.0 app/models/single_use_link.rb
hyrax-4.0.0.beta2 app/models/single_use_link.rb
hyrax-3.4.2 app/models/single_use_link.rb
hyrax-4.0.0.beta1 app/models/single_use_link.rb
hyrax-3.4.1 app/models/single_use_link.rb
hyrax-3.4.0 app/models/single_use_link.rb
hyrax-3.3.0 app/models/single_use_link.rb
hyrax-3.2.0 app/models/single_use_link.rb
hyrax-3.1.0 app/models/single_use_link.rb
hyrax-3.0.2 app/models/single_use_link.rb