Class: PolymorphicUploader

Inherits:
CarrierWave::Uploader::Base
  • Object
show all
Defined in:
app/uploaders/polymorphic_uploader.rb

Overview

CarrierWave

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ PolymorphicUploader

Returns a new instance of PolymorphicUploader


98
99
100
# File 'app/uploaders/polymorphic_uploader.rb', line 98

def initialize(*args, &block)
  super
end

Class Method Details

.cache_dirObject

This is where files are stored on upload. We are using callbacks to empty it after upload


109
110
111
# File 'app/uploaders/polymorphic_uploader.rb', line 109

def self.cache_dir
  "#{Rails.root}/tmp/uploads"
end

Instance Method Details

#cache_dirObject


113
114
115
# File 'app/uploaders/polymorphic_uploader.rb', line 113

def cache_dir
  self.class.cache_dir
end

#delete_tmp_dir(_new_file) ⇒ Object


125
126
127
128
129
130
# File 'app/uploaders/polymorphic_uploader.rb', line 125

def delete_tmp_dir(_new_file)
  # make sure we don't delete other things accidentally by checking the name pattern
  if @cache_id_was.present? && @cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/
    FileUtils.rm_rf(File.join(cache_dir, @cache_id_was))
  end
end

#exists?Boolean

Returns:

  • (Boolean)

102
103
104
# File 'app/uploaders/polymorphic_uploader.rb', line 102

def exists?
  @column.blank?
end

#remember_cache_id(_new_file) ⇒ Object

store! nils the cache_id after it finishes so we need to remember it for deletion


121
122
123
# File 'app/uploaders/polymorphic_uploader.rb', line 121

def remember_cache_id(_new_file)
  @cache_id_was = cache_id
end