Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve file after store #489

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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