/
mongoid_spec.rb
905 lines (733 loc) · 28.9 KB
/
mongoid_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
# encoding: utf-8
require 'spec_helper'
def reset_mongo_class(uploader = MongoUploader)
define_mongo_class('MongoUser') do
include Mongoid::Document
store_in :collection => 'users'
field :folder, :default => ''
mount_uploader :image, uploader
end
end
def define_mongo_class(class_name, &block)
Object.send(:remove_const, class_name) rescue nil
klass = Object.const_set(class_name, Class.new)
klass.class_eval(&block)
klass
end
class MongoUploader < CarrierWave::Uploader::Base; end
class AnotherMongoUploader < CarrierWave::Uploader::Base; end
class IntegrityErrorUploader < CarrierWave::Uploader::Base
process :monkey
def monkey
raise CarrierWave::IntegrityError
end
def extension_white_list
%w(jpg)
end
end
class ProcessingErrorUploader < CarrierWave::Uploader::Base
process :monkey
def monkey
raise CarrierWave::ProcessingError
end
def extension_white_list
%w(jpg)
end
end
describe CarrierWave::Mongoid do
after do
MongoUser.collection.drop if MongoUser.count > 0
end
describe '#image' do
context "when nothing is assigned" do
before do
mongo_user_klass = reset_mongo_class
@document = mongo_user_klass.new
end
it "returns a blank uploader" do
expect(@document.image).to be_blank
end
end
context "when an empty string is assigned" do
before do
mongo_user_klass = reset_mongo_class
@document = mongo_user_klass.new(:image => "")
@document.save
end
it "returns a blank uploader" do
@saved_doc = MongoUser.first
expect(@saved_doc.image).to be_blank
end
end
context "when a filename is saved in the database" do
before do
mongo_user_klass = reset_mongo_class
@document = mongo_user_klass.new
# should retrieve a file from the storage if a value is stored in the database
@document[:image] = "test.jpg" # NOT @document.image = 'text.jpg'
@document.save
@doc = MongoUser.first
end
it "returns an uploader" do
expect(@doc.image).to be_an_instance_of(MongoUploader)
end
it "sets the path to the store directory" do
expect(@doc.image.current_path).to eq public_path('uploads/test.jpg')
end
it "should return valid JSON when to_json is called when image is nil" do
@doc[:image] = nil
hash = JSON.parse(@doc.to_json)
expect(hash.keys).to include("image")
expect(hash["image"].keys).to include("url")
expect(hash["image"]["url"]).to be_nil
end
it "should return valid JSON when to_json is called when image is present" do
@doc[:image] = 'test.jpeg'
@doc.save!
@doc.reload
expect(JSON.parse(@doc.to_json)["image"]).to eq("url" => "/uploads/test.jpeg")
end
it "should return valid JSON when to_json is called on a collection containing uploader from a model" do
@doc[:image] = 'test.jpeg'
@doc.save!
@doc.reload
expect(JSON.parse({:data => @doc.image}.to_json)).to eq("data"=>{"image"=>{"url"=>"/uploads/test.jpeg"}})
end
it "should respect options[:only] when passed to to_json for the serializable hash" do
@doc[:image] = 'test.jpeg'
@doc.save!
@doc.reload
expect(JSON.parse(@doc.to_json({:only => [:_id]}))).to eq("_id" => @doc.id.as_json)
end
it "should respect options[:except] when passed to to_json for the serializable hash" do
@doc[:image] = 'test.jpeg'
@doc.save!
@doc.reload
expect(JSON.parse(@doc.to_json({:except => [:_id, :image]}))).to eq("folder" => "")
end
end
end
describe '#image=' do
before do
mongo_user_klass = reset_mongo_class
@doc = mongo_user_klass.new
end
context "when nil is assigned" do
it "does not set the value" do
@doc.image = nil
expect(@doc.image).to be_blank
end
end
context "when an empty string is assigned" do
it "does not set the value" do
@doc.image = ''
expect(@doc.image).to be_blank
end
end
context "when a file is assigned" do
it "should cache a file" do
@doc.image = stub_file('test.jpeg')
expect(@doc.image).to be_an_instance_of(MongoUploader)
end
it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
expect(@doc[:image]).to be_nil
end
it "should copy a file into into the cache directory" do
@doc.image = stub_file('test.jpeg')
expect(@doc.image.current_path).to match /^#{public_path('uploads\/tmp')}/
end
end
context 'when validating integrity' do
before do
mongo_user_klass = reset_mongo_class(IntegrityErrorUploader)
@doc = mongo_user_klass.new
@doc.image = stub_file('test.jpg')
end
it "should make the document invalid when an integrity error occurs" do
expect(@doc).to_not be_valid
end
it "should use I18n for integrity error messages" do
translations = { mongoid: { errors: { messages: { carrierwave_integrity_error: 'is not of an allowed file type' }}}}
change_locale_and_store_translations(:en, translations) do
@doc.valid?
expect(@doc.errors[:image]).to eq ['is not of an allowed file type']
end
translations = { mongoid: { errors: { messages: { carrierwave_integrity_error: 'tipo de imagem não permitido.' }}}}
change_locale_and_store_translations(:pt, translations) do
@doc.valid?
expect(@doc.errors[:image]).to eq ['tipo de imagem não permitido.']
end
end
end
context 'when validating processing' do
before do
mongo_user_klass = reset_mongo_class(ProcessingErrorUploader)
@doc = mongo_user_klass.new
@doc.image = stub_file('test.jpg')
end
it "should make the document invalid when a processing error occurs" do
expect(@doc).to_not be_valid
end
it "should use I18n for processing error messages" do
translations = { mongoid: { errors: { messages: { carrierwave_processing_error: 'failed to be processed' }}}}
change_locale_and_store_translations(:en, translations) do
@doc.valid?
expect(@doc.errors[:image]).to eq ['failed to be processed']
end
translations = { mongoid: { errors: { messages: { carrierwave_processing_error: 'falha ao processar imagem.' }}}}
change_locale_and_store_translations(:pt, translations) do
@doc.valid?
expect(@doc.errors[:image]).to eq ['falha ao processar imagem.']
end
end
end
end
describe '#remove_image=' do
before do
mongo_user_klass = reset_mongo_class
@doc = mongo_user_klass.new
end
it "treats true argument such that attribute is marked as changed" do
@doc.remove_image = true
expect(@doc).to be_image_changed
end
it "treats '1' argument such that attribute is marked as changed" do
@doc.remove_image = '1'
expect(@doc).to be_image_changed
end
it "treats false argument such that attribute is not marked as changed" do
@doc.remove_image = false
expect(@doc).to_not be_image_changed
end
it "treats nil argument such that attribute is not marked as changed" do
@doc.remove_image = nil
expect(@doc).to_not be_image_changed
end
it "treats '0' argument such that attribute is not marked as changed" do
@doc.remove_image = '0'
expect(@doc).to_not be_image_changed
end
end
describe "#save" do
it "after it was initialized with params" do
doc = reset_mongo_class.new(:image => stub_file('test.jpg'))
expect(doc.save).to be_truthy
expect(doc.image).to be_an_instance_of(MongoUploader)
expect(doc.image.current_path).to eq public_path('uploads/test.jpg')
end
before do
mongo_user_klass = reset_mongo_class
@doc = mongo_user_klass.new
end
context "when no file is assigned" do
it "image is blank" do
expect(@doc.save).to be_truthy
expect(@doc.image).to be_blank
end
end
context "when a file is assigned" do
it "copies the file to the upload directory" do
@doc.image = stub_file('test.jpg')
expect(@doc.save).to be_truthy
expect(@doc.image).to be_an_instance_of(MongoUploader)
expect(@doc.image.current_path).to eq public_path('uploads/test.jpg')
end
it "saves the filename in the database" do
@doc.image = stub_file('test.jpg')
expect(@doc.save).to be_truthy
expect(@doc[:image]).to eq 'test.jpg'
expect(@doc.image_identifier).to eq 'test.jpg'
end
context "when remove_image? is true" do
it "removes the image" do
@doc.image = stub_file('test.jpeg')
@doc.save
@doc.remove_image = true
expect(@doc.save).to be_truthy
@doc.reload
expect(@doc.image).to be_blank
expect(@doc.image_identifier).to be_blank
end
end
it "should mark image as changed when saving a new image" do
expect(@doc.image_changed?).to be_falsey
@doc.image = stub_file("test.jpeg")
expect(@doc.image_changed?).to be_truthy
@doc.save
@doc.reload
expect(@doc.image_changed?).to be_falsey
@doc.image = stub_file("test.jpg")
expect(@doc.image_changed?).to be_truthy
end
end
end
describe '#update' do
before do
mongo_user_klass = reset_mongo_class
@doc = mongo_user_klass.new
@doc.image = stub_file('test.jpeg')
@doc.save
@doc.reload
end
it "replaced it by a file with the same name" do
@doc.image = stub_file('test.jpeg')
@doc.save
@doc.reload
expect(@doc[:image]).to eq 'test.jpeg'
expect(@doc.image_identifier).to eq 'test.jpeg'
end
end
describe '#destroy' do
before do
mongo_user_klass = reset_mongo_class
@doc = mongo_user_klass.new
end
describe "when file assigned" do
it "removes the file from the filesystem" do
@doc.image = stub_file('test.jpeg')
expect(@doc.save).to be_truthy
expect(File.exist?(public_path('uploads/test.jpeg'))).to be_truthy
expect(@doc.image).to be_an_instance_of(MongoUploader)
expect(@doc.image.current_path).to eq public_path('uploads/test.jpeg')
@doc.destroy
expect(File.exist?(public_path('uploads/test.jpeg'))).to be_falsey
end
end
describe "when file is not assigned" do
it "deletes the instance of MongoUser after save" do
@doc.save
expect(MongoUser.count).to eq(1)
@doc.destroy
end
it "deletes the instance of MongoUser after save and then re-looking up the instance" do
@doc.save
expect(MongoUser.count).to eq(1)
@doc = MongoUser.first
@doc.destroy
end
end
end
describe '#mount_uploader removing old files' do
before do
@uploader = Class.new(MongoUploader)
@class = reset_mongo_class(@uploader)
@class.field :foo
@doc = @class.new
@doc.image = stub_file('old.jpeg')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
end
after do
FileUtils.rm_rf(file_path("uploads"))
end
describe 'normally' do
it "should remove old file if old file had a different path" do
@doc.image = stub_file('new.jpeg')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_falsey
end
it "should not remove old file if old file had a different path but config is false" do
allow(@uploader).to receive(:remove_previously_stored_files_after_update).and_return(false)
@doc.image = stub_file('new.jpeg')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
end
it "should not remove file if old file had the same path" do
@doc.image = stub_file('old.jpeg')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
end
it "should not remove file if validations fail on save" do
@class.validate { |r| r.errors.add :textfile, "FAIL!" }
@doc.image = stub_file('new.jpeg')
expect(@doc.save).to be_falsey
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
end
end
describe 'with an overriden filename' do
before do
@uploader.class_eval do
def filename
model.foo + File.extname(super)
end
end
@doc.image = stub_file('old.jpeg')
@doc.foo = "test"
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/test.jpeg'))).to be_truthy
expect(@doc.image.read).to eq "this is stuff"
end
it "should not remove file if old file had the same dynamic path" do
@doc.image = stub_file('test.jpeg')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/test.jpeg'))).to be_truthy
end
it "should remove old file if old file had a different dynamic path" do
@doc.foo = "new"
@doc.image = stub_file('test.jpeg')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/test.jpeg'))).to be_falsey
end
end
shared_examples "embedded documents" do
it "should remove old file if old file had a different path" do
@embedded_doc.image = stub_file('new.jpeg')
expect(@embedded_doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_falsey
end
it "should not remove old file if old file had a different path but config is false" do
allow(@embedded_doc.image).to receive(:remove_previously_stored_files_after_update).and_return(false)
@embedded_doc.image = stub_file('new.jpeg')
expect(@embedded_doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
end
it "should not remove file if old file had the same path" do
@embedded_doc.image = stub_file('old.jpeg')
expect(@embedded_doc.save).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
end
it "should not remove file if validations fail on save" do
@embedded_doc_class.validate { |r| r.errors.add :textfile, "FAIL!" }
@embedded_doc.image = stub_file('new.jpeg')
expect(@embedded_doc.save).to be_falsey
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
end
it "should not touch parent's dirty attributes" do
@class.field :title
@doc.title = "Title"
@embedded_doc.image = stub_file('new.jpeg')
expect(@embedded_doc.save).to be_truthy
expect(@doc.title).to eq "Title"
end
end
shared_examples "double embedded documents" do
it "should remove old file if old file had a different path" do
@double_embedded_doc.image = stub_file('new.jpeg')
expect(@double_embedded_doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_falsey
end
it "should not remove old file if old file had a different path but config is false" do
allow(@double_embedded_doc.image).to receive(:remove_previously_stored_files_after_update).and_return(false)
@double_embedded_doc.image = stub_file('new.jpeg')
expect(@double_embedded_doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
end
it "should not remove file if old file had the same path" do
@double_embedded_doc.image = stub_file('old.jpeg')
expect(@double_embedded_doc.save).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
end
it "should not remove file if validations fail on save" do
@double_embedded_doc_class.validate { |r| r.errors.add :textfile, "FAIL!" }
@double_embedded_doc.image = stub_file('new.jpeg')
expect(@double_embedded_doc.save).to be_falsey
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
end
end
describe 'with document embedded as embeds_one' do
before do
@embedded_doc_class = define_mongo_class('MongoLocation') do
include Mongoid::Document
mount_uploader :image, @uploader
embedded_in :mongo_user
end
@class.class_eval do
embeds_one :mongo_location
end
@doc = @class.new
@embedded_doc = @doc.build_mongo_location
@embedded_doc.image = stub_file('old.jpeg')
expect(@embedded_doc.save).to be_truthy
end
include_examples "embedded documents"
end
describe 'with document embedded as embeds_one and parent document not matched the default scope' do
before do
@embedded_doc_class = define_mongo_class('MongoLocation') do
include Mongoid::Document
mount_uploader :image, @uploader
embedded_in :mongo_user
end
@class.class_eval do
embeds_one :mongo_location
default_scope ->{ where(:always_false => false) }
end
@doc = @class.new
@embedded_doc = @doc.build_mongo_location
@embedded_doc.image = stub_file('old.jpeg')
expect(@embedded_doc.save).to be_truthy
end
include_examples "embedded documents"
end
describe 'with embedded documents' do
before do
@embedded_doc_class = define_mongo_class('MongoLocation') do
include Mongoid::Document
mount_uploader :image, @uploader
embedded_in :mongo_user
end
@class.class_eval do
embeds_many :mongo_locations, cascade_callbacks: true
accepts_nested_attributes_for :mongo_locations
end
@doc = @class.new
@embedded_doc = @doc.mongo_locations.build
@embedded_doc.image = stub_file('old.jpeg')
expect(@embedded_doc.save).to be_truthy
end
include_examples "embedded documents"
it "attaches a new file to an existing document that had no file at first" do
doc = @class.new
doc.mongo_locations.build
expect(doc.save).to be_truthy
doc.reload
doc.mongo_locations.first.image = stub_file('test.jpeg')
expect(doc.save).to be_truthy
doc.reload
expect(doc.mongo_locations.first[:image]).to eq 'test.jpeg'
end
it "changes the file" do
@doc.update_attributes mongo_locations_attributes: { '0' => { _id: @embedded_doc._id, image: stub_file('test.jpeg') } }
@doc.reload
expect(@doc.mongo_locations.first[:image]).to eq 'test.jpeg'
end
it "removes a file" do
@doc.update_attributes mongo_locations_attributes: { '0' => { _id: @embedded_doc._id, remove_image: "1" } }
@doc.reload
expect(@doc.mongo_locations.first[:image]).to_not be_present
end
describe 'with double embedded documents' do
before do
@double_embedded_doc_class = define_mongo_class('MongoItem') do
include Mongoid::Document
mount_uploader :image, @uploader
embedded_in :mongo_location
end
@embedded_doc_class.class_eval do
embeds_many :mongo_items
end
@doc = @class.new
@embedded_doc = @doc.mongo_locations.build
@embedded_doc.image = stub_file('old.jpeg')
expect(@embedded_doc.save).to be_truthy
@double_embedded_doc = @embedded_doc.mongo_items.build
@double_embedded_doc.image = stub_file('old.jpeg')
expect(@double_embedded_doc.save).to be_truthy
end
include_examples "double embedded documents"
end
end
describe 'with embedded documents and parent document not matched the default scope' do
before do
@embedded_doc_class = define_mongo_class('MongoLocation') do
include Mongoid::Document
mount_uploader :image, @uploader
embedded_in :mongo_user
end
@class.class_eval do
embeds_many :mongo_locations
default_scope ->{ where(:always_false => false) }
end
@doc = @class.new
@embedded_doc = @doc.mongo_locations.build
@embedded_doc.image = stub_file('old.jpeg')
expect(@embedded_doc.save).to be_truthy
end
include_examples "embedded documents"
describe 'with double embedded documents' do
before do
@double_embedded_doc_class = define_mongo_class('MongoItem') do
include Mongoid::Document
mount_uploader :image, @uploader
embedded_in :mongo_location
end
@embedded_doc_class.class_eval do
embeds_many :mongo_items
end
@doc = @class.new
@embedded_doc = @doc.mongo_locations.build
@embedded_doc.image = stub_file('old.jpeg')
expect(@embedded_doc.save).to be_truthy
@double_embedded_doc = @embedded_doc.mongo_items.build
@double_embedded_doc.image = stub_file('old.jpeg')
expect(@double_embedded_doc.save).to be_truthy
end
include_examples "double embedded documents"
end
end
describe 'with embedded documents and nested attributes' do
before do
@embedded_doc_class = define_mongo_class('MongoLocation') do
include Mongoid::Document
mount_uploader :image, @uploader
embedded_in :mongo_user
end
@class.class_eval do
embeds_many :mongo_locations, cascade_callbacks: true
accepts_nested_attributes_for :mongo_locations
end
@doc = @class.new(mongo_locations_attributes: [{image: stub_file("old.jpeg")}])
expect(@doc.save).to be_truthy
@embedded_doc = @doc.mongo_locations.first
end
it "should set the image on a save" do
@doc.reload
expect(@doc.mongo_locations.first.image.path).to match(/old\.jpeg$/)
expect(@embedded_doc.image.path).to match(/old\.jpeg$/)
end
it "should update the image on update_attributes" do
expect(@doc.update_attributes(mongo_locations_attributes: [{id: @embedded_doc.id, image: stub_file("new.jpeg")}])).to be_truthy
@doc.reload
expect(@doc.mongo_locations.first.image.path).to match(/new\.jpeg$/)
expect(@embedded_doc.image.path).to match(/new\.jpeg$/)
end
end
end
describe '#mount_uploader removing old files with versions' do
before do
@uploader = Class.new(MongoUploader)
@uploader.version :thumb
@class = reset_mongo_class(@uploader)
@doc = @class.new
@doc.image = stub_file('old.jpeg')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/thumb_old.jpeg'))).to be_truthy
end
after do
FileUtils.rm_rf(file_path("uploads"))
end
it "should remove old file if old file had a different path" do
@doc.image = stub_file('new.jpeg')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/thumb_new.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_falsey
expect(File.exists?(public_path('uploads/thumb_old.jpeg'))).to be_falsey
end
it "should not remove file if old file had the same path" do
@doc.image = stub_file('old.jpeg')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/thumb_old.jpeg'))).to be_truthy
end
end
describe '#mount_uploader removing old files with multiple uploaders' do
before do
@uploader = Class.new(MongoUploader)
@class = reset_mongo_class(@uploader)
@uploader1 = Class.new(CarrierWave::Uploader::Base)
@class.mount_uploader(:textfile, @uploader1)
@doc = @class.new
@doc.image = stub_file('old.jpeg')
@doc.textfile = stub_file('old.txt')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/old.txt'))).to be_truthy
end
after do
FileUtils.rm_rf(file_path("uploads"))
end
it "should remove old file1 and file2 if old file1 and file2 had a different paths" do
@doc.image = stub_file('new.jpeg')
@doc.textfile = stub_file('new.txt')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_falsey
expect(File.exists?(public_path('uploads/new.txt'))).to be_truthy
expect(File.exists?(public_path('uploads/old.txt'))).to be_falsey
end
it "should remove old file1 but not file2 if old file1 had a different path but old file2 has the same path" do
@doc.image = stub_file('new.jpeg')
@doc.textfile = stub_file('old.txt')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_falsey
expect(File.exists?(public_path('uploads/old.txt'))).to be_truthy
end
it "should not remove file1 or file2 if file1 and file2 have the same paths" do
@doc.image = stub_file('old.jpeg')
@doc.textfile = stub_file('old.txt')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/old.txt'))).to be_truthy
end
end
describe '#mount_uploader removing old files with mount_on' do
before do
@class = reset_mongo_class
@uploader1 = Class.new(CarrierWave::Uploader::Base)
@class.mount_uploader(:avatar, @uploader1, :mount_on => :another_image)
@doc = @class.new
@doc.avatar = stub_file('old.jpeg')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
end
after do
FileUtils.rm_rf(file_path("uploads"))
end
it "should remove old file if old file had a different path" do
@doc.avatar = stub_file('new.jpeg')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_falsey
end
it "should not remove file if old file had the same path" do
@doc.avatar = stub_file('old.jpeg')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/old.jpeg'))).to be_truthy
end
end
# Mongoid::Paranoia support is only part of Mongoid 3.x. It was removed from
# Mongoid 4.x.
if defined?(Mongoid::Paranoia)
describe "with paranoia enabled" do
before do
@class = reset_mongo_class
@class.collection.drop
@class.class_eval do
include Mongoid::Paranoia
end
@doc = @class.new(image: stub_file("old.jpeg"))
expect(@doc.save).to be_truthy
end
it "should not remove underlying image after #destroy" do
expect(@doc.destroy).to be_truthy
expect(@class.count).to eql(0)
expect(@class.deleted.count).to eql(1)
expect(File.exist?(public_path('uploads/old.jpeg'))).to be_truthy
end
it "should remove underlying image after #destroy!" do
expect(@doc.destroy!).to be_truthy
expect(@class.count).to eql(0)
expect(@class.deleted.count).to eql(0)
expect(File.exist?(public_path('uploads/old.jpeg'))).to be_falsey
end
end
end
context "JSON serialization with multiple uploaders" do
before do
@class = reset_mongo_class
@class.send(:mount_uploader, :textfile,AnotherMongoUploader)
@event = @class.new
@event.image = stub_file('old.jpeg')
@event.textfile = stub_file('old.txt')
end
it "serializes the correct values" do
expect(@event.serializable_hash["image"]["url"]).to match(/old\.jpeg$/)
expect(@event.serializable_hash["textfile"]["url"]).to match(/old\.txt$/)
end
it "should have JSON for each uploader" do
parsed = JSON.parse(@event.to_json)
expect(parsed["image"]["url"]).to match(/old\.jpeg$/)
expect(parsed["textfile"]["url"]).to match(/old\.txt$/)
end
end
end