Skip to content

Commit 22e8005

Browse files
committed
Fix passing empty string to #image_cache= causes existing ones deleted. Fixes #2412
Refs. 8f18a95
1 parent 96a0b03 commit 22e8005

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

lib/carrierwave/mounter.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ def cache_names
7272
end
7373

7474
def cache_names=(cache_names)
75+
cache_names = cache_names.reject(&:blank?)
7576
return if cache_names.blank?
7677
clear_unstaged
77-
cache_names.map do |cache_name|
78+
cache_names.each do |cache_name|
7879
begin
7980
uploader = blank_uploader
8081
uploader.retrieve_from_cache!(cache_name)
@@ -91,7 +92,7 @@ def remote_urls=(urls)
9192
@remote_urls = urls
9293

9394
clear_unstaged
94-
urls.zip(remote_request_headers || []).map do |url, header|
95+
urls.zip(remote_request_headers || []).each do |url, header|
9596
handle_error do
9697
uploader = blank_uploader
9798
uploader.download!(url, header || {})

spec/mount_multiple_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,18 @@ def monkey
487487
expect(instance.images.map(&:cache_name)).to eq(['1369894322-123-0123-1234/test.jpg'])
488488
end
489489
end
490+
491+
context "when an empty string is assigned" do
492+
before do
493+
instance.images = [test_file_stub]
494+
instance.store_images!
495+
instance.images_cache = [''].to_json
496+
end
497+
498+
it "does not write over a previously stored file" do
499+
expect(instance.images[0].current_path).to match(/test.jpg$/)
500+
end
501+
end
490502
end
491503

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

580+
context "when an empty string is assigned" do
581+
subject { images[0].current_path }
582+
583+
let(:remote_images_url) { [""] }
584+
585+
before do
586+
instance.images = [stub_file("portrait.jpg")]
587+
instance.store_images!
588+
instance.remote_images_urls = remote_images_url
589+
end
590+
591+
it "does not write over a previously stored file" do
592+
is_expected.to match(/portrait.jpg$/)
593+
end
594+
end
595+
568596
context "if a file fails to be downloaded" do
569597
let(:remote_images_url) { ["http://www.example.com/test.txt", "http://www.example.com/test.jpg"] }
570598

spec/mount_single_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,13 @@ def default_url
288288
@instance.image_cache = '1369894322-123-0123-1234/monkey.jpg'
289289
expect(@instance.image.current_path).to match(/test.jpg$/)
290290
end
291+
292+
it "should not clear a previously stored file when an empty string is assigned" do
293+
@instance.image = stub_file('test.jpg')
294+
@instance.image.store!
295+
@instance.image_cache = ''
296+
expect(@instance.image.current_path).to match(/test.jpg$/)
297+
end
291298
end
292299

293300
describe "#remote_image_url" do
@@ -354,6 +361,13 @@ def default_url
354361

355362
expect(@instance.image.current_path).to match(/portrait.jpg$/)
356363
end
364+
365+
it "does not clear a previously stored file when an empty string is assigned" do
366+
@instance.remote_image_url = "http://www.example.com/test.jpg"
367+
@instance.image.store!
368+
@instance.remote_image_url = ""
369+
expect(@instance.image.current_path).to match(/test.jpg$/)
370+
end
357371
end
358372

359373
describe '#store_image!' do

0 commit comments

Comments
 (0)