Skip to content

Commit

Permalink
Fix assigning a file with the same name not marking the column as cha…
Browse files Browse the repository at this point in the history
…nged

Fixes #2719
  • Loading branch information
mshibuya committed Mar 2, 2024
1 parent 63113e9 commit 4c65b39
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/carrierwave/uploader/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand Down
2 changes: 1 addition & 1 deletion spec/mount_multiple_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 25 additions & 4 deletions spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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!
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4c65b39

Please sign in to comment.