From 4c65b393cd85b66bc256d04363cf3e3a97c8fd64 Mon Sep 17 00:00:00 2001 From: Mitsuhiro Shibuya Date: Sun, 4 Feb 2024 17:48:06 +0900 Subject: [PATCH] Fix assigning a file with the same name not marking the column as changed Fixes #2719 --- lib/carrierwave/uploader/proxy.rb | 4 ++-- spec/mount_multiple_spec.rb | 2 +- spec/orm/activerecord_spec.rb | 29 +++++++++++++++++++++++++---- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/carrierwave/uploader/proxy.rb b/lib/carrierwave/uploader/proxy.rb index 75383d372..0e938b40e 100644 --- a/lib/carrierwave/uploader/proxy.rb +++ b/lib/carrierwave/uploader/proxy.rb @@ -40,10 +40,10 @@ def identifier # # === Returns # - # [String] a temporary_identifier, by default @original_filename is used + # [String] a temporary_identifier, by default the value of #cache_name is used # def temporary_identifier - @original_filename || @identifier + cache_name || @identifier end ## diff --git a/spec/mount_multiple_spec.rb b/spec/mount_multiple_spec.rb index 08e425d57..0b9a6ee9d 100644 --- a/spec/mount_multiple_spec.rb +++ b/spec/mount_multiple_spec.rb @@ -887,8 +887,8 @@ def monkey after { instance.write_images_identifier } it "writes to the column" do - expect(instance).to receive(:write_uploader).with(:images, [test_file_name]).at_least(:once) instance.images = [test_file_stub] + expect(instance).to receive(:write_uploader).with(:images, [test_file_name]).at_least(:once) instance.write_images_identifier end diff --git a/spec/orm/activerecord_spec.rb b/spec/orm/activerecord_spec.rb index 3ce31f23f..716569389 100644 --- a/spec/orm/activerecord_spec.rb +++ b/spec/orm/activerecord_spec.rb @@ -449,6 +449,15 @@ def monkey expect(@event.changed_for_autosave?).to be_truthy end + it "should mark image as changed when saving a new image with same file name" do + @event.image = stub_file("test.jpeg") + @event.save + @event.image = stub_tempfile("bork.txt", nil, "test.jpeg") + expect(@event.image_changed?).to be_truthy + @event.save + expect(@event.reload.image.read).to match /^bork/ + end + it "should not update image when save with select" do @event.image = stub_file('test.jpeg') @event.save! @@ -632,8 +641,9 @@ def filename describe '#changes' do it "should be generated" do + allow(CarrierWave).to receive(:generate_cache_id).and_return('1369894322-345-1234-2255') @event.image = stub_file('test.jpeg') - expect(@event.changes).to eq({'image' => [nil, 'test.jpeg']}) + expect(@event.changes).to eq({'image' => [nil, '1369894322-345-1234-2255/test.jpeg']}) end it "shouldn't be generated when the attribute value is unchanged" do @@ -676,8 +686,9 @@ def filename describe '#changes' do it "should be generated" do + allow(CarrierWave).to receive(:generate_cache_id).and_return('1369894322-345-1234-2255') @event.image = stub_file('test.jpeg') - expect(@event.changes).to eq({'image' => [nil, 'test.jpeg']}) + expect(@event.changes).to eq({'image' => [nil, '1369894322-345-1234-2255/test.jpeg']}) end it "shouldn't be generated after second save" do @@ -1470,7 +1481,7 @@ def monkey expect(@event.images_identifiers[0]).to eq('test.jpeg') end - it "should mark images as changed when saving a new images" do + it "should mark images as changed when saving new images" do expect(@event.images_changed?).to be_falsey @event.images = [stub_file("test.jpeg")] expect(@event.images_changed?).to be_truthy @@ -1482,6 +1493,15 @@ def monkey expect(@event.changed_for_autosave?).to be_truthy end + it "should mark images as changed when saving new images with same file name" do + @event.images = [stub_file("test.jpeg")] + @event.save + @event.images = [stub_tempfile("bork.txt", nil, "test.jpeg")] + expect(@event.images_changed?).to be_truthy + @event.save + expect(@event.reload.images[0].read).to match /^bork/ + end + it "should not update image when save with select" do @event.images = [stub_file('test.jpeg')] @event.save! @@ -1629,8 +1649,9 @@ def filename describe '#changes' do it "should be generated" do + allow(CarrierWave).to receive(:generate_cache_id).and_return('1369894322-345-1234-2255') @event.images = [stub_file('test.jpeg')] - expect(@event.changes).to eq({'images' => [nil, ['test.jpeg']]}) + expect(@event.changes).to eq({'images' => [nil, ['1369894322-345-1234-2255/test.jpeg']]}) end it "shouldn't be generated when the attribute value is unchanged" do