mirror of
https://github.com/chenasraf/snpr.git
synced 2026-05-17 17:38:07 +00:00
48 lines
1.2 KiB
Ruby
48 lines
1.2 KiB
Ruby
require 'fileutils'
|
|
|
|
class Genotype < ActiveRecord::Base
|
|
belongs_to :user
|
|
has_many :user_snps
|
|
|
|
validates_presence_of :originalfilename, :message => "Please provide a genotyping file"
|
|
validates_presence_of :user
|
|
|
|
def fs_filename
|
|
return user.id.to_s+"."+filetype.to_s+"."+id.to_s
|
|
end
|
|
|
|
def data
|
|
if tmp_file_name
|
|
return File.open(::Rails.root.to_s+"/public/data/"+tmp_file_name).read
|
|
else
|
|
File.open(::Rails.root.to_s+"/public/data/"+id.to_s).read
|
|
end
|
|
end
|
|
|
|
def data=(filedata)
|
|
if tmp_file_name
|
|
File.open(::Rails.root.to_s+"/public/data/"+tmp_file_name, "wb") {|f| f.write(filedata)}
|
|
else
|
|
File.open(::Rails.root.to_s+"/public/data/", "wb") {|f| f.write(filedata)}
|
|
end
|
|
end
|
|
|
|
def move_file
|
|
FileUtils.move(::Rails.root.to_s+"/public/data/"+tmp_file_name, ::Rails.root.to_s+"/public/data/"+user.id.to_s+"."+filetype.to_s+"."+id.to_s)
|
|
end
|
|
|
|
def delete_file
|
|
FileUtils.rm(::Rails.root.to_s+"/public/data/"+user.id.to_s+"."+filetype.to_s+"."+id.to_s)
|
|
end
|
|
|
|
def download
|
|
return "/data/"+user.id.to_s+"."+filetype.to_s+"."+id.to_s
|
|
end
|
|
|
|
private
|
|
|
|
def tmp_file_name
|
|
@tmp_file_name ||= rand(999999).to_s
|
|
end
|
|
end
|