class Password

Public Instance Methods

days_old() click to toggle source
# File app/models/password.rb, line 9
def days_old
  (Time.now.to_datetime - self.created_at.to_datetime).to_i
end
days_remaining() click to toggle source
# File app/models/password.rb, line 13
def days_remaining
  [(self.expire_after_days - self.days_old), 0].max
end
to_param() click to toggle source
# File app/models/password.rb, line 5
def to_param
  self.url_token.to_s
end
validate!() click to toggle source

validate!

Run basic validations on the password. Expire the password if it's limits have been reached (time or views)

# File app/models/password.rb, line 27
def validate!
  return if expired

  # Range checking
  self.expire_after_days  ||= EXPIRE_AFTER_DAYS_DEFAULT
  self.expire_after_views ||= EXPIRE_AFTER_VIEWS_DEFAULT

  unless self.expire_after_days.between?(EXPIRE_AFTER_DAYS_MIN, EXPIRE_AFTER_DAYS_MAX)
    self.expire_after_days = EXPIRE_AFTER_DAYS_DEFAULT
  end

  unless self.expire_after_views.between?(EXPIRE_AFTER_VIEWS_MIN, EXPIRE_AFTER_VIEWS_MAX)
    self.expire_after_views = EXPIRE_AFTER_VIEWS_DEFAULT
  end

  unless self.new_record?
    if (self.days_old >= self.expire_after_days) or (self.views.count >= self.expire_after_views)
      # This password has hit max age or max views - expire it
      self.expired = true
      self.payload = nil
      self.save
    end
  end
end
views_remaining() click to toggle source
# File app/models/password.rb, line 17
def views_remaining
  [(self.expire_after_views - self.views.count), 0].max
end