Skip to content

Commit

Permalink
Fix CarrierWave file retrieval after store
Browse files Browse the repository at this point in the history
  • Loading branch information
David Liu authored Apr 5, 2022
1 parent ddde239 commit a55748c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/cloudinary/carrier_wave/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,16 @@ def format
format = Cloudinary::PreloadedFile.split_format(original_filename || "").last
return format || "" if resource_type == "raw"
format = requested_format || format || default_format

format = format.to_s.downcase
Cloudinary::FORMAT_ALIASES[format] || format
end

def store!(new_file=nil)
super

column = model.send(:_mounter, mounted_as).send(:serialization_column)
identifier = model.send(:attribute, column)
retrieve_from_store!(identifier) unless identifier.nil?
end
end
34 changes: 34 additions & 0 deletions spec/carriewave.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'
require 'cloudinary'

module CarrierWave
module Storage
class Abstract
def initialize(uploader)
@uploader = uploader
end

attr_accessor :uploader
end
end
class SanitizedFile; end
end

RSpec.describe Cloudinary::CarrierWave do
describe '#store!' do
let(:column) { 'example_field' }
let(:identifier) { 'identifier' }
let(:model) { double(:model, _mounter: mount) }
let(:mount) { double(:mount, serialization_column: column) }
let(:uploader) { spy(:uploader, model: model, mounted_as: :example).tap { |u| u.extend(Cloudinary::CarrierWave) } }

subject { uploader.store! }

it 'triggers `#retrieve_from_store!` after `#store!` executed to populate @file and @identifier' do
expect(model).to receive(:attribute).with(column).and_return(identifier)
expect(uploader).to receive(:retrieve_from_store!).with(identifier)

subject
end
end
end

0 comments on commit a55748c

Please sign in to comment.