Sha256: 521e4c89e3e6ea4aa08c26148712b71dc86ca3e228b5ed2fb49c436d654889f5

Contents?: true

Size: 782 Bytes

Versions: 10

Compression:

Stored size: 782 Bytes

Contents

class SingleUseLink < ActiveRecord::Base
  validate :expiration_date_cannot_be_in_the_past
  validate :cannot_be_destroyed

  after_initialize :set_defaults

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

  def expired?
    DateTime.current > expires
  end

  def to_param
    downloadKey
  end

  protected

    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.downloadKey ||= (Digest::SHA2.new << rand(1_000_000_000).to_s).to_s
    end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
hyrax-1.1.1 app/models/single_use_link.rb
hyrax-1.1.0 app/models/single_use_link.rb
hyrax-1.0.5 app/models/single_use_link.rb
hyrax-1.0.4 app/models/single_use_link.rb
hyrax-1.0.3 app/models/single_use_link.rb
hyrax-1.0.2 app/models/single_use_link.rb
hyrax-1.0.1 app/models/single_use_link.rb
hyrax-1.0.0.rc2 app/models/single_use_link.rb
hyrax-1.0.0.rc1 app/models/single_use_link.rb
test_hyrax-0.0.1.alpha app/models/single_use_link.rb