@@ -641,6 +641,58 @@ class Extension < Spree::Base
641
641
end
642
642
end
643
643
644
+ context "ransacker :variants_option_values" do
645
+ it "filters products based on option values of their variants" do
646
+ product_1 = create ( :product )
647
+ option_value_1 = create ( :option_value )
648
+ create ( :variant , product : product_1 , option_values : [ option_value_1 ] )
649
+
650
+ result = Spree ::Product . ransack ( variants_option_values_in : [ option_value_1 . id ] ) . result
651
+ expect ( result ) . to contain_exactly ( product_1 )
652
+ end
653
+
654
+ it "returns multiple products for the same option value" do
655
+ product_1 = create ( :product )
656
+ product_2 = create ( :product )
657
+ option_value_1 = create ( :option_value )
658
+ create ( :variant , product : product_1 , option_values : [ option_value_1 ] )
659
+ create ( :variant , product : product_2 , option_values : [ option_value_1 ] )
660
+
661
+ result = Spree ::Product . ransack ( variants_option_values_in : [ option_value_1 . id ] ) . result
662
+ expect ( result ) . to contain_exactly ( product_1 , product_2 )
663
+ end
664
+
665
+ it "returns no products if there is no match" do
666
+ non_existing_option_value_id = 99999
667
+ result = Spree ::Product . ransack ( variants_option_values_in : [ non_existing_option_value_id ] ) . result
668
+ expect ( result ) . to be_empty
669
+ end
670
+
671
+ it "returns products that match any of the provided option value IDs" do
672
+ product_1 = create ( :product )
673
+ product_2 = create ( :product )
674
+ option_value_1 = create ( :option_value )
675
+ option_value_2 = create ( :option_value )
676
+ create ( :variant , product : product_1 , option_values : [ option_value_1 ] )
677
+ create ( :variant , product : product_2 , option_values : [ option_value_2 ] )
678
+
679
+ result = Spree ::Product . ransack ( variants_option_values_in : [ option_value_1 . id , option_value_2 . id ] ) . result
680
+ expect ( result ) . to contain_exactly ( product_1 , product_2 )
681
+ end
682
+
683
+ it "doesn't return products that have other option values not in the query" do
684
+ product_1 = create ( :product )
685
+ product_2 = create ( :product )
686
+ option_value_1 = create ( :option_value )
687
+ option_value_2 = create ( :option_value )
688
+ create ( :variant , product : product_1 , option_values : [ option_value_1 ] )
689
+ create ( :variant , product : product_2 , option_values : [ option_value_2 ] )
690
+
691
+ result = Spree ::Product . ransack ( variants_option_values_in : [ option_value_1 . id ] ) . result
692
+ expect ( result ) . not_to include ( product_2 )
693
+ end
694
+ end
695
+
644
696
describe "ransack scopes" do
645
697
context "available scope" do
646
698
subject { described_class . ransack ( available : true ) . result }
0 commit comments