diff --git a/lib/open_food_network/scope_variants_for_search.rb b/lib/open_food_network/scope_variants_for_search.rb index 1ceb52499d..9ca265aa47 100644 --- a/lib/open_food_network/scope_variants_for_search.rb +++ b/lib/open_food_network/scope_variants_for_search.rb @@ -34,7 +34,11 @@ module OpenFoodNetwork def query_scope Spree::Variant.where(is_master: false). includes(option_values: :option_type). - ransack(search_params.merge(m: 'or')).result + ransack(search_params.merge(m: 'or')). + result. + order("spree_products.name, display_name, display_as, spree_products.variant_unit_name"). + includes(:product). + joins(:product) end def distributor diff --git a/spec/lib/open_food_network/scope_variants_to_search_spec.rb b/spec/lib/open_food_network/scope_variants_to_search_spec.rb index 3090f5daf9..fdf394b012 100644 --- a/spec/lib/open_food_network/scope_variants_to_search_spec.rb +++ b/spec/lib/open_food_network/scope_variants_to_search_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'open_food_network/scope_variants_for_search' +require 'spec_helper' describe OpenFoodNetwork::ScopeVariantsForSearch do let!(:p1) { create(:simple_product, name: 'Product 1') } @@ -60,5 +61,21 @@ describe OpenFoodNetwork::ScopeVariantsForSearch do expect(result).to_not include v1, v2, v3 end end + + context "searching products starting with the same 3 caracters" do + let(:params) { { q: "pro" } } + it "returns variants ordered by display_name" do + p1.name = "Product b" + p2.name = "Product a" + p3.name = "Product c" + p4.name = "Product 1" + p1.save! + p2.save! + p3.save! + p4.save! + expect(result.map(&:name)). + to eq(["Product 1", "Product a", "Product b", "Product c"]) + end + end end end