From 0f4b3007bd8fc4ab191974a8a22ad8ac3833d42f Mon Sep 17 00:00:00 2001 From: Yohta Kimura Date: Wed, 13 Sep 2023 08:17:18 +0900 Subject: [PATCH 1/3] add failing test cases for https://github.com/carrierwaveuploader/carrierwave/issues/2700 dup should not prevent uploading --- spec/orm/activerecord_spec.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/orm/activerecord_spec.rb b/spec/orm/activerecord_spec.rb index cd74f7305..32846c4e6 100644 --- a/spec/orm/activerecord_spec.rb +++ b/spec/orm/activerecord_spec.rb @@ -1919,6 +1919,10 @@ def reload(*) Event.mount_uploader(:image, @uploader) end + after do + FileUtils.rm_rf(public_path("uploads")) + end + it "caches the existing file into the new model" do @event.image = stub_file('test.jpeg') @event.save @@ -1956,6 +1960,15 @@ def reload(*) expect { @event.dup }.not_to change { @event[:image] } end + it "can upload a file" do + @event.image = stub_file('test.jpeg') + @event.dup + + expect(@event.save).to be_truthy + expect(@event.image.path).to eq public_path('uploads/test.jpeg') + expect(File.exist?(@event.image.path)).to be_truthy + end + context "with more than one mount" do before do @uploader1 = Class.new(CarrierWave::Uploader::Base) @@ -2013,6 +2026,20 @@ def store_dir expect(@event.save).to be_truthy expect(@event.image.current_path).to eq public_path("uploads/event/image/#{@event.id}/test.jpeg") end + + it "upload files to appropriate paths" do + @event.image = stub_file('test.jpeg') + new_event = @event.dup + expect(new_event).not_to be @event + + expect(@event.save).to be_truthy + expect(@event.image.path).to eq public_path("uploads/event/image/#{@event.id}/test.jpeg") + expect(File.exist?(@event.image.path)).to be_truthy + + expect(new_event.save).to be_truthy + expect(new_event.image.path).to eq public_path("uploads/event/image/#{new_event.id}/test.jpeg") + expect(File.exist?(new_event.image.path)).to be_truthy + end end context 'when #initialize_dup is overridden in the model' do From 9b0dabf9952ff10f0bd60dc80a32899ec0cd4b71 Mon Sep 17 00:00:00 2001 From: Yohta Kimura Date: Wed, 13 Sep 2023 18:53:08 +0900 Subject: [PATCH 2/3] reorder to assign nil after @_mounters dup --- lib/carrierwave/orm/activerecord.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/carrierwave/orm/activerecord.rb b/lib/carrierwave/orm/activerecord.rb index 6c51ef0d3..d9cd93275 100644 --- a/lib/carrierwave/orm/activerecord.rb +++ b/lib/carrierwave/orm/activerecord.rb @@ -43,8 +43,8 @@ def reload(*) # Reset cached mounter on record dup def initialize_dup(other) old_uploaders = _mounter(:"#{column}").uploaders - @_mounters[:"#{column}"] = nil super + @_mounters[:"#{column}"] = nil # The attribute needs to be cleared to prevent it from picked up as identifier write_attribute(_mounter(:#{column}).serialization_column, nil) _mounter(:"#{column}").cache(old_uploaders) From 54c97f267ab55d1246927a0971dcce160de22201 Mon Sep 17 00:00:00 2001 From: Yohta Kimura <38206553+rajyan@users.noreply.github.com> Date: Sat, 16 Sep 2023 21:36:55 +0900 Subject: [PATCH 3/3] Update spec/orm/activerecord_spec.rb Co-authored-by: Mitsuhiro Shibuya --- spec/orm/activerecord_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/orm/activerecord_spec.rb b/spec/orm/activerecord_spec.rb index 32846c4e6..c6b2ada19 100644 --- a/spec/orm/activerecord_spec.rb +++ b/spec/orm/activerecord_spec.rb @@ -1960,7 +1960,7 @@ def reload(*) expect { @event.dup }.not_to change { @event[:image] } end - it "can upload a file" do + it "allows the original object to store a file" do @event.image = stub_file('test.jpeg') @event.dup