class MOdrFile < ApplicationRecord
  belongs_to :m_odr_rep
  belongs_to :m_odr_file_type
  
  mount_uploader :file, OdrUploader
  
  validates :file, :presence => true
  
  
  before_create { generate_token() }
  
  def generate_token()
    begin
      self[:token] = SecureRandom.urlsafe_base64
    end while MOdrFile.exists?(:token => self[:token])
  end
    
  
  
  def rotate(degrees=90)
    versions = [self.file.path]
    
    versions.each do |v|
      image = Magick::ImageList.new(v)
      image = image.rotate(degrees)
      image.write(v)
    end
    
    self.file.recreate_versions!(:secure, :secure_thumb)
    
    
    self.updated_at = Time.now
    self.save
  end
  
  
end
