@@ -541,6 +541,15 @@ def monkey
541
541
expect ( instance . images [ 0 ] . current_path ) . to match ( /test.jpg$/ )
542
542
end
543
543
end
544
+
545
+ context "when a file is already stored" do
546
+ before { allow ( instance ) . to receive ( :read_uploader ) . and_return ( [ 'bork.txt' ] ) }
547
+
548
+ it "marks the previously uploaded file as removed" do
549
+ instance . images_cache = [ '1369894322-123-0123-1234/test.jpg' ] . to_json
550
+ expect ( instance . send ( :_mounter , :images ) . instance_variable_get ( :@removed_uploaders ) . map ( &:identifier ) ) . to eq [ 'bork.txt' ]
551
+ end
552
+ end
544
553
end
545
554
546
555
describe "#remote_images_urls" do
@@ -565,25 +574,24 @@ def monkey
565
574
before do
566
575
stub_request ( :get , "http://www.example.com/#{ test_file_name } " ) . to_return ( body : File . read ( test_file_stub ) )
567
576
stub_request ( :get , "http://www.example.com/test.txt" ) . to_return ( status : 404 )
568
- instance . remote_images_urls = remote_images_url
569
577
end
570
578
571
579
context "does nothing when nil is assigned" do
572
- let ( :remote_images_url ) { nil }
580
+ before { instance . remote_images_urls = nil }
573
581
574
582
it { is_expected . to be_empty }
575
583
end
576
584
577
585
context "does nothing when an empty string is assigned" do
578
- let ( :remote_images_url ) { '' }
586
+ before { instance . remote_images_urls = '' }
579
587
580
588
it { is_expected . to be_empty }
581
589
end
582
590
583
591
context "retrieves from cache when a cache name is assigned" do
584
592
subject { images [ 0 ] . current_path }
585
593
586
- let ( :remote_images_url ) { [ "http://www.example.com/test.jpg" ] }
594
+ before { instance . remote_images_urls = [ "http://www.example.com/test.jpg" ] }
587
595
588
596
it { is_expected . to match ( /test.jpg$/ ) }
589
597
@@ -595,12 +603,10 @@ def monkey
595
603
context "writes over a previously stored file" do
596
604
subject { images [ 0 ] . current_path }
597
605
598
- let ( :remote_images_url ) { [ "http://www.example.com/test.jpg" ] }
599
-
600
606
before do
601
607
instance . images = [ stub_file ( "portrait.jpg" ) ]
602
608
instance . store_images!
603
- instance . remote_images_urls = remote_images_url
609
+ instance . remote_images_urls = [ "http://www.example.com/test.jpg" ]
604
610
end
605
611
606
612
it { is_expected . to match ( /test.jpg$/ ) }
@@ -609,11 +615,9 @@ def monkey
609
615
context "does not write over a previously assigned file" do
610
616
subject { images [ 0 ] . current_path }
611
617
612
- let ( :remote_images_url ) { [ "http://www.example.com/test.jpg" ] }
613
-
614
618
before do
615
619
instance . images = [ stub_file ( "portrait.jpg" ) ]
616
- instance . remote_images_urls = remote_images_url
620
+ instance . remote_images_urls = [ "http://www.example.com/test.jpg" ]
617
621
end
618
622
619
623
it { is_expected . to match ( /portrait.jpg$/ ) }
@@ -622,12 +626,10 @@ def monkey
622
626
context "when an empty string is assigned" do
623
627
subject { images [ 0 ] . current_path }
624
628
625
- let ( :remote_images_url ) { [ "" ] }
626
-
627
629
before do
628
630
instance . images = [ stub_file ( "portrait.jpg" ) ]
629
631
instance . store_images!
630
- instance . remote_images_urls = remote_images_url
632
+ instance . remote_images_urls = [ "" ]
631
633
end
632
634
633
635
it "does not write over a previously stored file" do
@@ -636,7 +638,7 @@ def monkey
636
638
end
637
639
638
640
context "if a file fails to be downloaded" do
639
- let ( :remote_images_url ) { [ "http://www.example.com/test.txt" , "http://www.example.com/test.jpg" ] }
641
+ before { instance . remote_images_urls = [ "http://www.example.com/test.txt" , "http://www.example.com/test.jpg" ] }
640
642
641
643
it "keeps files which was downloaded successfully" do
642
644
expect ( instance . images . map ( &:identifier ) ) . to eq [ 'test.jpg' ]
@@ -645,14 +647,25 @@ def monkey
645
647
646
648
context "clears the unsaved remote urls when nil is assigned" do
647
649
subject { instance . remote_images_urls }
648
- let ( :remote_images_url ) { [ 'invalid' ] }
650
+ before { instance . remote_images_urls = [ 'invalid' ] }
649
651
650
652
before do
651
653
instance . remote_images_urls = nil
652
654
end
653
655
654
656
it { is_expected . to be_empty }
655
657
end
658
+
659
+ context "when a file is already stored" do
660
+ before do
661
+ allow ( instance ) . to receive ( :read_uploader ) . and_return ( [ 'bork.txt' ] )
662
+ instance . remote_images_urls = [ "http://www.example.com/test.jpg" ]
663
+ end
664
+
665
+ it "marks the previously uploaded file as removed" do
666
+ expect ( instance . send ( :_mounter , :images ) . instance_variable_get ( :@removed_uploaders ) . map ( &:identifier ) ) . to eq [ 'bork.txt' ]
667
+ end
668
+ end
656
669
end
657
670
658
671
describe '#store_images!' do
@@ -716,6 +729,22 @@ def monkey
716
729
end
717
730
end
718
731
732
+ describe '#remove_images=' do
733
+ context "when a file is already stored" do
734
+ before do
735
+ attribute = [ 'bork.txt' ]
736
+ allow ( instance ) . to receive ( :read_uploader ) { attribute }
737
+ allow ( instance ) . to receive ( :write_uploader ) { |_ , value | attribute . replace ( value || [ ] ) }
738
+ end
739
+
740
+ it "marks the previously uploaded file as removed" do
741
+ instance . remove_images = true
742
+ instance . write_images_identifier
743
+ expect ( instance . send ( :_mounter , :images ) . instance_variable_get ( :@removed_uploaders ) . map ( &:identifier ) ) . to eq [ 'bork.txt' ]
744
+ end
745
+ end
746
+ end
747
+
719
748
describe '#remove_images?' do
720
749
subject { instance . remove_images? }
721
750
0 commit comments