From 31cc2549ed52d02e3a315a2fb9f3f345fd91928b Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 2 Dec 2021 11:46:15 +0100 Subject: [PATCH 1/6] Order variant search by display_name --- lib/open_food_network/scope_variants_for_search.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/open_food_network/scope_variants_for_search.rb b/lib/open_food_network/scope_variants_for_search.rb index 1ceb52499d..71089540f0 100644 --- a/lib/open_food_network/scope_variants_for_search.rb +++ b/lib/open_food_network/scope_variants_for_search.rb @@ -34,7 +34,9 @@ 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("display_name") end def distributor From 12f7935a263a64162b3989778af40466e1049f65 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 2 Dec 2021 11:46:30 +0100 Subject: [PATCH 2/6] Add needed Helper to handle `create` --- spec/lib/open_food_network/scope_variants_to_search_spec.rb | 1 + 1 file changed, 1 insertion(+) 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..0e698012ee 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') } From 1b6cfb60a114e35967203de2f309d90037c61275 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 2 Dec 2021 11:47:05 +0100 Subject: [PATCH 3/6] Searching variants should order results by variants display name --- .../scope_variants_to_search_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 0e698012ee..d96f97d8d9 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 @@ -61,5 +61,20 @@ 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 + v1.display_name = "Product 1 - b" + v2.display_name = "Product 1 - a" + v3.display_name = "Product 1 - c" + v4.display_name = "Product 1" + v1.save! + v2.save! + v3.save! + v4.save! + expect(result.map(&:display_name)).to eq ["Product 1", "Product 1 - a", "Product 1 - b", "Product 1 - c"] + end + end end end From cad8d8316de41d325bdbb77d54a0531f7cc700cf Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Fri, 3 Dec 2021 10:09:23 +0100 Subject: [PATCH 4/6] Improve variant order when searching --- .../scope_variants_for_search.rb | 3 ++- .../scope_variants_to_search_spec.rb | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/open_food_network/scope_variants_for_search.rb b/lib/open_food_network/scope_variants_for_search.rb index 71089540f0..abc0502750 100644 --- a/lib/open_food_network/scope_variants_for_search.rb +++ b/lib/open_food_network/scope_variants_for_search.rb @@ -36,7 +36,8 @@ module OpenFoodNetwork includes(option_values: :option_type). ransack(search_params.merge(m: 'or')). result. - order("display_name") + includes(:product). + order("products_spree_variants.name, display_name, display_as, products_spree_variants.variant_unit_name") 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 d96f97d8d9..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 @@ -65,15 +65,16 @@ describe OpenFoodNetwork::ScopeVariantsForSearch do context "searching products starting with the same 3 caracters" do let(:params) { { q: "pro" } } it "returns variants ordered by display_name" do - v1.display_name = "Product 1 - b" - v2.display_name = "Product 1 - a" - v3.display_name = "Product 1 - c" - v4.display_name = "Product 1" - v1.save! - v2.save! - v3.save! - v4.save! - expect(result.map(&:display_name)).to eq ["Product 1", "Product 1 - a", "Product 1 - b", "Product 1 - c"] + 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 From 0cc4e5728ed3f32d987d31bcd2af041819dcfecd Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 15 Dec 2021 15:23:52 +1100 Subject: [PATCH 5/6] Reference product attributes for sorting correctly The previous version failed in some cases as it relied on a table name generated by Rails. --- lib/open_food_network/scope_variants_for_search.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/open_food_network/scope_variants_for_search.rb b/lib/open_food_network/scope_variants_for_search.rb index abc0502750..b66ea88539 100644 --- a/lib/open_food_network/scope_variants_for_search.rb +++ b/lib/open_food_network/scope_variants_for_search.rb @@ -37,7 +37,9 @@ module OpenFoodNetwork ransack(search_params.merge(m: 'or')). result. includes(:product). - order("products_spree_variants.name, display_name, display_as, products_spree_variants.variant_unit_name") + merge(Spree::Product.order(:name)). + order(:display_name, :display_as). + merge(Spree::Product.order(:variant_unit_name)) end def distributor From a33ebbdc854cfa7101ed061a16f4464e6a9bbf95 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Wed, 15 Dec 2021 10:11:51 +0100 Subject: [PATCH 6/6] Include and joins after the order seems in order to select fields... ...that are in the order by --- lib/open_food_network/scope_variants_for_search.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/open_food_network/scope_variants_for_search.rb b/lib/open_food_network/scope_variants_for_search.rb index b66ea88539..9ca265aa47 100644 --- a/lib/open_food_network/scope_variants_for_search.rb +++ b/lib/open_food_network/scope_variants_for_search.rb @@ -36,10 +36,9 @@ module OpenFoodNetwork includes(option_values: :option_type). ransack(search_params.merge(m: 'or')). result. + order("spree_products.name, display_name, display_as, spree_products.variant_unit_name"). includes(:product). - merge(Spree::Product.order(:name)). - order(:display_name, :display_as). - merge(Spree::Product.order(:variant_unit_name)) + joins(:product) end def distributor