Module: CarrierWave::SafeRemove

Included in:
Mounter
Defined in:
lib/locomotive/carrierwave/patches.rb

Overview

FIXME: The carrierwave store_dir of the ContentEntry model was not correctly set up.

The consequence is the following bug:

  • context: a content entry has 2 file fields with 2 uploaded files sharing the same filename

  • action: we delete one of the 2 files.

  • result: the second file will be erased too.

The solution is to not delete a file if inside the same model, we find another file field sharing the same file identifier.

Instance Method Summary collapse

Instance Method Details

#remove!Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/locomotive/carrierwave/patches.rb', line 106

def remove!
  record.class.uploaders.each do |_column, _|
    next if _column == column

    _mounter    = self.record.send(:_mounter, _column)
    _uploader   = self.record.send(_column)
    _identifier = _uploader.identifier

    # different uploaders, same file identifiers, we have to know if this file was aimed to be deleted too
    # if not, we disable the deletion of the original uploader
    if self.identifiers.include?(_identifier) && !_mounter.remove?
      # no idea why there might be more than one uploader
      uploaders.reject(&:blank?).each do |uploader|
        uploader.instance_variable_set(:@file, nil)
        uploader.instance_variable_set(:@cache_id, nil)
      end

      return false
    end
  end

  super
end