All pastes #67398 Raw Edit

Mine

public ruby v1 · immutable
#67398 ·published 2006-06-19 21:51 UTC
rendered paste body
class Document < ActiveRecord::Base    attr_accessor :uploaded_file    def after_create    if !File.exists?(File.dirname(self.file_path))      Dir.mkdir(File.dirname(self.file_path))    end        if @uploaded_file.instance_of?(Tempfile)      FileUtils.copy(@uploaded_file.local_path, self.file_path)    else      File.open(self.file_path, "wb") { |f| f.write(@uploaded_file.read) }    end  end    def after_destroy    if File.exists?(self.file_path)      File.delete(self.file_path)    end  end    def file=(uploaded_file)    @uploaded_file = uploaded_file    self.file_path = sanitize_filename(@uploaded_file.original_filename)    self.content_type = @uploaded_file.content_type  end    private  def new_filename(file_name)    idx = file_name.rindex "."    self.name.gsub(/[^\w\.\_]/, '_') + file_name[idx, file_name.length - idx]  end    def sanitize_filename(file_name)    File.expand_path("#{RAILS_ROOT}/public/documents/#{new_filename(file_name)}")  end  endclass GlobalDocument < Document  validates_uniqueness_of :name, :scope => :type    private  def sanitize_filename(file_name)    File.expand_path("#{RAILS_ROOT}/public/documents/global/#{new_filename(file_name)}")  end  end