Sha256: 2717ad9f36f08dae0c548c9eecbb3f6456e004f8101696a10a2b01ddcd4e04fc

Contents?: true

Size: 785 Bytes

Versions: 10

Compression:

Stored size: 785 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.now > expires
  end


  def to_param
    downloadKey
  end

  protected

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

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

  def set_defaults
    if new_record?
      self.expires ||= DateTime.now.advance(hours:24)
      self.downloadKey ||= (Digest::SHA2.new << rand(1000000000).to_s).to_s
    end
  end

end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
sufia-4.0.0.beta2 sufia-models/app/models/single_use_link.rb
sufia-models-4.0.0.beta2 app/models/single_use_link.rb
sufia-4.0.0.beta1 sufia-models/app/models/single_use_link.rb
sufia-models-4.0.0.beta1 app/models/single_use_link.rb
sufia-models-3.7.2 app/models/single_use_link.rb
sufia-3.7.2 sufia-models/app/models/single_use_link.rb
sufia-models-3.7.1 app/models/single_use_link.rb
sufia-3.7.1 sufia-models/app/models/single_use_link.rb
sufia-models-3.7.0 app/models/single_use_link.rb
sufia-3.7.0 sufia-models/app/models/single_use_link.rb