Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ cat job.rb
- class Job < ActiveRecord::Base
- attr_accessor :file_data
- before_validation :read_file
- after_create :copy_file
- validates :filename, :presence => true
- validates_format_of :filename, :with => %r{\.foo$}i, :if => "!filename.nil?", :message => "lorem ipsum"
- def real_path
- "#{Rails.root}/public/uploads/foo/job_#{self.id}#{File.extname(self.filename)}"
- end
- def update_status!(status, observations = '')
- self.status = status
- self.observations = observations
- self.save
- end
- private
- def read_file
- return if self.file_data.nil?
- input = self.file_data
- self.filename = input.original_filename
- #self.content_type = input.content_type.chomp
- #self.size = input.size
- end
- def copy_file
- return if self.file_data.nil?
- input = self.file_data
- file_path = real_path
- dir_path = File.dirname(file_path)
- FileUtils.makedirs(dir_path) if !File.exist?(dir_path)
- File.open(file_path, "wb"){ |f| f.write(input.read) }
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement