@@ -140,6 +140,7 @@ def self.model_name
140140
141141 describe 'model#remove_uploaders=' do
142142 before do
143+ model . images = [ stub_file ( 'test.jpg' ) ]
143144 model . save
144145 end
145146
@@ -208,7 +209,7 @@ def self.model_name
208209
209210 before do
210211 model . save!
211- model . collection . update_one ( { _id : model . id } , { images : identifiers } )
212+ model . collection . update_one ( { _id : model . id } , '$set' => { images : identifiers } )
212213 end
213214
214215 it 'returns an array of uploaders' do
@@ -239,10 +240,6 @@ def self.model_name
239240 expect ( model . images . map ( &:identifier ) ) . to match_array ( identifiers )
240241 end
241242
242- it 'does not write anything to the database, in order to prevent overridden filenames to fail because of unassigned attributes' do
243- expect ( model [ :images ] ) . to match_array ( [ ] )
244- end
245-
246243 it 'copies a file into into the cache directory' do
247244 expect ( model . images . first . current_path ) . to match ( /^#{ Regexp . escape ( public_path ( 'uploads/tmp' ) ) } / )
248245 end
@@ -260,10 +257,6 @@ def self.model_name
260257 expect ( model . images . map ( &:identifier ) ) . to match_array ( [ 'portrait.jpg' , 'test.jpeg' ] )
261258 end
262259
263- it 'does not write anything to the database, in order to prevent overridden filenames to fail because of unassigned attributes' do
264- expect ( model [ :images ] ) . to match_array ( [ 'portrait.jpg' ] )
265- end
266-
267260 it 'copies a file into into the cache directory' do
268261 expect ( model . images . map ( &:current_path ) ) . to all ( match ( /^#{ Regexp . escape ( public_path ( 'uploads/tmp' ) ) } / ) )
269262 end
@@ -301,10 +294,6 @@ def self.model_name
301294 expect ( model . images ) . to all ( be_an_instance_of ( uploader_class ) )
302295 end
303296
304- it 'does not write to the database' do
305- expect ( model [ :images ] ) . to be_empty
306- end
307-
308297 it 'copies a file into into the cache directory' do
309298 expect ( model . images . map ( &:current_path ) ) . to all ( start_with ( public_path ( 'uploads/tmp' ) ) )
310299 end
@@ -393,15 +382,20 @@ def munge
393382
394383 before do
395384 model_class . create! ( images : files )
396- end
397-
398- it 'replaced it by a file with the same name' do
399385 record . update! ( images : [ stub_file ( 'test.jpeg' ) ] )
400-
401386 record . reload
387+ end
402388
403- expect ( record [ :images ] ) . to match_array ( [ 'test.jpeg' ] )
404- expect ( record . images_identifiers ) . to match_array ( [ 'test.jpeg' ] )
389+ if Gem ::Version . new ( CarrierWave ::VERSION ) >= Gem ::Version . new ( "3.0.beta" )
390+ it "performs deduplication" do
391+ expect ( record [ :images ] ) . to match_array ( [ 'test(2).jpeg' ] )
392+ expect ( record . images_identifiers ) . to match_array ( [ 'test(2).jpeg' ] )
393+ end
394+ else
395+ it 'replaced it by a file with the same name' do
396+ expect ( record [ :images ] ) . to match_array ( [ 'test.jpeg' ] )
397+ expect ( record . images_identifiers ) . to match_array ( [ 'test.jpeg' ] )
398+ end
405399 end
406400 end
407401
@@ -492,12 +486,14 @@ def munge
492486 expect ( model . save ) . to be_truthy
493487 expect ( File ) . to exist ( public_path ( 'uploads/new.jpeg' ) )
494488 expect ( File ) . to exist ( public_path ( 'uploads/old.jpeg' ) )
489+ ensure
490+ uploader_class . remove_previously_stored_files_after_update = true
495491 end
496492
497- it 'does not remove file if old file had the same path' do
493+ it 'does not remove new file if both of files had the same path' do
498494 model . images = [ stub_file ( 'old.jpeg' ) ]
499495 expect ( model . save ) . to be_truthy
500- expect ( File ) . to exist ( public_path ( 'uploads/old.jpeg' ) )
496+ expect ( File ) . to exist ( model . images [ 0 ] . path )
501497 end
502498
503499 it 'does not remove file if validations fail on save' do
@@ -519,13 +515,13 @@ def filename
519515
520516 let! ( :model ) { model_class . create! ( name : 'Mike' , images : [ stub_file ( 'old.jpeg' ) ] ) }
521517
522- it 'does not remove file if old file had the same dynamic path' do
518+ it 'does not remove new file if both of files had the same path' do
523519 expect ( File ) . to exist ( public_path ( 'uploads/Mike.jpeg' ) )
524520 expect ( model . images . first . read ) . to eq 'this is stuff'
525521
526522 model . update! ( images : [ stub_file ( 'test.jpeg' ) ] )
527523
528- expect ( File ) . to exist ( public_path ( 'uploads/Mike.jpeg' ) )
524+ expect ( File ) . to exist ( model . images [ 0 ] . path )
529525 end
530526
531527 it 'removes old file if old file had a different dynamic path' do
@@ -553,12 +549,14 @@ def filename
553549 expect ( embedded_model . save ) . to be_truthy
554550 expect ( File ) . to exist ( public_path ( 'uploads/new.jpeg' ) )
555551 expect ( File ) . to exist ( public_path ( 'uploads/old.jpeg' ) )
552+ ensure
553+ uploader_class . remove_previously_stored_files_after_update = true
556554 end
557555
558- it 'does not remove file if old file had the same path' do
556+ it 'should not remove new file if both of files had the same path' do
559557 embedded_model . images = [ stub_file ( 'old.jpeg' ) ]
560558 expect ( embedded_model . save ) . to be_truthy
561- expect ( File ) . to exist ( public_path ( 'uploads/old.jpeg' ) )
559+ expect ( File ) . to exist ( embedded_model . images [ 0 ] . path )
562560 end
563561
564562 it 'does not remove file if validations fail on save' do
@@ -592,12 +590,14 @@ def filename
592590 expect ( double_embedded_model . save ) . to be_truthy
593591 expect ( File ) . to exist ( public_path ( 'uploads/new.jpeg' ) )
594592 expect ( File ) . to exist ( public_path ( 'uploads/old.jpeg' ) )
593+ ensure
594+ uploader_class . remove_previously_stored_files_after_update = true
595595 end
596596
597- it 'does not remove file if old file had the same path' do
597+ it 'should not remove new file if both of files had the same path' do
598598 double_embedded_model . images = [ stub_file ( 'old.jpeg' ) ]
599599 expect ( double_embedded_model . save ) . to be_truthy
600- expect ( File ) . to exist ( public_path ( 'uploads/old.jpeg' ) )
600+ expect ( File ) . to exist ( double_embedded_model . images [ 0 ] . path )
601601 end
602602
603603 it 'does not remove file if validations fail on save' do
@@ -878,14 +878,14 @@ def filename
878878 expect ( File ) . not_to exist ( public_path ( 'uploads/thumb_old.jpeg' ) )
879879 end
880880
881- it 'does not remove file if old file had the same path' do
881+ it 'does not remove new file if both of files had the same path' do
882882 expect ( File ) . to exist ( public_path ( 'uploads/old.jpeg' ) )
883883 expect ( File ) . to exist ( public_path ( 'uploads/thumb_old.jpeg' ) )
884884
885885 model . update! ( images : [ stub_file ( 'old.jpeg' ) ] )
886886
887- expect ( File ) . to exist ( public_path ( 'uploads/old.jpeg' ) )
888- expect ( File ) . to exist ( public_path ( 'uploads/thumb_old.jpeg' ) )
887+ expect ( File ) . to exist ( model . images [ 0 ] . path )
888+ expect ( File ) . to exist ( model . images [ 0 ] . thumb . path )
889889 end
890890 end
891891
@@ -926,25 +926,25 @@ def self.model_name
926926 expect ( File ) . not_to exist ( public_path ( 'uploads/old.txt' ) )
927927 end
928928
929- it 'removes old file1 but not file2 if old file1 had a different path but old file2 has the same path' do
929+ it 'removes old file1 but not new file2 if old file1 had a different path but old file2 has the same path' do
930930 expect ( File ) . to exist ( public_path ( 'uploads/old.jpeg' ) )
931931 expect ( File ) . to exist ( public_path ( 'uploads/old.txt' ) )
932932
933933 model . update! ( images : [ stub_file ( 'new.jpeg' ) ] , textfiles : [ stub_file ( 'old.txt' ) ] )
934934
935935 expect ( File ) . to exist ( public_path ( 'uploads/new.jpeg' ) )
936936 expect ( File ) . not_to exist ( public_path ( 'uploads/old.jpeg' ) )
937- expect ( File ) . to exist ( public_path ( 'uploads/old.txt' ) )
937+ expect ( File ) . to exist ( model . textfiles [ 0 ] . path )
938938 end
939939
940- it 'does not remove file1 or file2 if file1 and file2 have the same paths' do
940+ it 'does not remove new files if each pair of files has the same paths' do
941941 expect ( File ) . to exist ( public_path ( 'uploads/old.jpeg' ) )
942942 expect ( File ) . to exist ( public_path ( 'uploads/old.txt' ) )
943943
944944 model . update! ( images : [ stub_file ( 'old.jpeg' ) ] , textfiles : [ stub_file ( 'old.txt' ) ] )
945945
946- expect ( File ) . to exist ( public_path ( 'uploads/old.jpeg' ) )
947- expect ( File ) . to exist ( public_path ( 'uploads/old.txt' ) )
946+ expect ( File ) . to exist ( model . images [ 0 ] . path )
947+ expect ( File ) . to exist ( model . textfiles [ 0 ] . path )
948948 end
949949 end
950950
@@ -981,12 +981,12 @@ def self.model_name
981981 expect ( File ) . not_to exist ( public_path ( 'uploads/old.jpeg' ) )
982982 end
983983
984- it 'does not remove file if old file had the same path' do
984+ it 'does not remove new file if both of files had the same path' do
985985 expect ( File ) . to exist ( public_path ( 'uploads/old.jpeg' ) )
986986
987987 model . update! ( avatars : [ stub_file ( 'old.jpeg' ) ] )
988988
989- expect ( File ) . to exist ( public_path ( 'uploads/old.jpeg' ) )
989+ expect ( File ) . to exist ( model . avatars [ 0 ] . path )
990990 end
991991 end
992992 end
0 commit comments