diff --git a/lib/carrierwave/mounter.rb b/lib/carrierwave/mounter.rb index cdc924125..cf2655893 100644 --- a/lib/carrierwave/mounter.rb +++ b/lib/carrierwave/mounter.rb @@ -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) @@ -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 || {}) diff --git a/spec/mount_multiple_spec.rb b/spec/mount_multiple_spec.rb index 5c3335dbf..6a679b02f 100644 --- a/spec/mount_multiple_spec.rb +++ b/spec/mount_multiple_spec.rb @@ -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 @@ -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"] } diff --git a/spec/mount_single_spec.rb b/spec/mount_single_spec.rb index f134e6c2e..1391b97d3 100644 --- a/spec/mount_single_spec.rb +++ b/spec/mount_single_spec.rb @@ -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 @@ -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