Skip to content

Commit

Permalink
Fix passing empty string to #image_cache= causes existing ones deleted.
Browse files Browse the repository at this point in the history
Fixes #2412

Refs. 8f18a95
  • Loading branch information
mshibuya committed Aug 25, 2019
1 parent 96a0b03 commit 22e8005
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/carrierwave/mounter.rb
Expand Up @@ -72,9 +72,10 @@ def cache_names
end

def cache_names=(cache_names)
cache_names = cache_names.reject(&:blank?)
return if cache_names.blank?
clear_unstaged
cache_names.map do |cache_name|
cache_names.each do |cache_name|
begin
uploader = blank_uploader
uploader.retrieve_from_cache!(cache_name)
Expand All @@ -91,7 +92,7 @@ def remote_urls=(urls)
@remote_urls = urls

clear_unstaged
urls.zip(remote_request_headers || []).map do |url, header|
urls.zip(remote_request_headers || []).each do |url, header|
handle_error do
uploader = blank_uploader
uploader.download!(url, header || {})
Expand Down
28 changes: 28 additions & 0 deletions spec/mount_multiple_spec.rb
Expand Up @@ -487,6 +487,18 @@ def monkey
expect(instance.images.map(&:cache_name)).to eq(['1369894322-123-0123-1234/test.jpg'])
end
end

context "when an empty string is assigned" do
before do
instance.images = [test_file_stub]
instance.store_images!
instance.images_cache = [''].to_json
end

it "does not write over a previously stored file" do
expect(instance.images[0].current_path).to match(/test.jpg$/)
end
end
end

describe "#remote_images_urls" do
Expand Down Expand Up @@ -565,6 +577,22 @@ def monkey
it { is_expected.to match(/portrait.jpg$/) }
end

context "when an empty string is assigned" do
subject { images[0].current_path }

let(:remote_images_url) { [""] }

before do
instance.images = [stub_file("portrait.jpg")]
instance.store_images!
instance.remote_images_urls = remote_images_url
end

it "does not write over a previously stored file" do
is_expected.to match(/portrait.jpg$/)
end
end

context "if a file fails to be downloaded" do
let(:remote_images_url) { ["http://www.example.com/test.txt", "http://www.example.com/test.jpg"] }

Expand Down
14 changes: 14 additions & 0 deletions spec/mount_single_spec.rb
Expand Up @@ -288,6 +288,13 @@ def default_url
@instance.image_cache = '1369894322-123-0123-1234/monkey.jpg'
expect(@instance.image.current_path).to match(/test.jpg$/)
end

it "should not clear a previously stored file when an empty string is assigned" do
@instance.image = stub_file('test.jpg')
@instance.image.store!
@instance.image_cache = ''
expect(@instance.image.current_path).to match(/test.jpg$/)
end
end

describe "#remote_image_url" do
Expand Down Expand Up @@ -354,6 +361,13 @@ def default_url

expect(@instance.image.current_path).to match(/portrait.jpg$/)
end

it "does not clear a previously stored file when an empty string is assigned" do
@instance.remote_image_url = "http://www.example.com/test.jpg"
@instance.image.store!
@instance.remote_image_url = ""
expect(@instance.image.current_path).to match(/test.jpg$/)
end
end

describe '#store_image!' do
Expand Down

0 comments on commit 22e8005

Please sign in to comment.