Advertisement
Guest User

job.rb

a guest
Oct 26th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.03 KB | None | 0 0
  1. $ cat job.rb
  2. class Job < ActiveRecord::Base
  3.   attr_accessor :file_data
  4.  
  5.   before_validation :read_file
  6.   after_create :copy_file
  7.  
  8.   validates :filename, :presence => true
  9.   validates_format_of :filename, :with => %r{\.foo$}i, :if => "!filename.nil?", :message => "lorem ipsum"
  10.  
  11.   def real_path
  12.     "#{Rails.root}/public/uploads/foo/job_#{self.id}#{File.extname(self.filename)}"
  13.   end
  14.  
  15.   def update_status!(status, observations = '')
  16.     self.status = status
  17.     self.observations = observations
  18.     self.save
  19.   end
  20.  
  21.  private
  22.   def read_file
  23.     return if self.file_data.nil?
  24.    
  25.     input = self.file_data  
  26.     self.filename = input.original_filename
  27.     #self.content_type = input.content_type.chomp
  28.     #self.size = input.size
  29.   end
  30.  
  31.   def copy_file
  32.     return if self.file_data.nil?
  33.  
  34.     input = self.file_data
  35.  
  36.     file_path = real_path
  37.     dir_path = File.dirname(file_path)
  38.     FileUtils.makedirs(dir_path)   if !File.exist?(dir_path)
  39.     File.open(file_path, "wb"){ |f| f.write(input.read) }
  40.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement