Skip to content

Commit

Permalink
Merge pull request #2706 from rajyan/dup-file-upload-failure-failing-…
Browse files Browse the repository at this point in the history
…test

Fix dup mounter cache behavior
  • Loading branch information
mshibuya committed Sep 17, 2023
2 parents 8815592 + 54c97f2 commit fac358c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/carrierwave/orm/activerecord.rb
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions spec/orm/activerecord_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -1956,6 +1960,15 @@ def reload(*)
expect { @event.dup }.not_to change { @event[:image] }
end

it "allows the original object to store 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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fac358c

Please sign in to comment.