From 285b41dc4ac072c7668f36851a64f9f2dfdbf9f4 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 10 Aug 2023 15:47:54 +0100 Subject: [PATCH] Reinstate sorting by arbitrary list of product categories --- app/services/order_cycle_distributed_products.rb | 7 +++++++ app/services/products_renderer.rb | 10 ++++++++-- .../admin/enterprises/form/_shop_preferences.html.haml | 8 ++++++++ config/initializers/pagy.rb | 7 +++++-- .../controllers/api/v0/order_cycles_controller_spec.rb | 4 ++-- spec/services/products_renderer_spec.rb | 4 ++-- spec/system/admin/enterprises_spec.rb | 2 +- 7 files changed, 33 insertions(+), 9 deletions(-) diff --git a/app/services/order_cycle_distributed_products.rb b/app/services/order_cycle_distributed_products.rb index afe47b6cf7b3..82b03722f878 100644 --- a/app/services/order_cycle_distributed_products.rb +++ b/app/services/order_cycle_distributed_products.rb @@ -13,6 +13,13 @@ def products_relation Spree::Product.where(id: stocked_products).group("spree_products.id") end + def products_taxons_relation + Spree::Product.where(id: stocked_products). + joins("LEFT JOIN (SELECT DISTINCT ON(product_id) id, product_id, primary_taxon_id FROM spree_variants) first_variant ON spree_products.id = first_variant.product_id"). + select("spree_products.*, first_variant.primary_taxon_id"). + group("spree_products.id, first_variant.primary_taxon_id") + end + def variants_relation order_cycle. variants_distributed_by(distributor). diff --git a/app/services/products_renderer.rb b/app/services/products_renderer.rb index 16e936fab36a..44ce51925e3c 100644 --- a/app/services/products_renderer.rb +++ b/app/services/products_renderer.rb @@ -35,7 +35,7 @@ def products @products ||= begin results = distributed_products. - products_relation. + products_taxons_relation. order(Arel.sql(products_order)) filter_and_paginate(results). @@ -54,7 +54,7 @@ def enterprise_fee_calculator def filter_and_paginate(query) results = query.ransack(args[:q]).result - _pagy, paginated_results = pagy( + _pagy, paginated_results = pagy_arel( results, page: args[:page] || 1, items: args[:per_page] || DEFAULT_PER_PAGE @@ -74,6 +74,12 @@ def products_order .preferred_shopfront_producer_order .split(",").map { |id| "spree_products.supplier_id=#{id} DESC" } .join(", ") + ", spree_products.name ASC, spree_products.id ASC" + elsif distributor.preferred_shopfront_product_sorting_method == "by_category" && + distributor.preferred_shopfront_taxon_order.present? + distributor + .preferred_shopfront_taxon_order + .split(",").map { |id| "first_variant.primary_taxon_id=#{id} DESC" } + .join(", ") + ", spree_products.name ASC, spree_products.id ASC" else "spree_products.name ASC, spree_products.id" end diff --git a/app/views/admin/enterprises/form/_shop_preferences.html.haml b/app/views/admin/enterprises/form/_shop_preferences.html.haml index e70ac9bc9d89..531dfda005be 100644 --- a/app/views/admin/enterprises/form/_shop_preferences.html.haml +++ b/app/views/admin/enterprises/form/_shop_preferences.html.haml @@ -22,6 +22,14 @@ .three.columns.alpha = radio_button :enterprise, :preferred_shopfront_product_sorting_method, :by_product, { 'ng-model' => 'Enterprise.preferred_shopfront_product_sorting_method' } = label :enterprise, :preferred_shopfront_product_sorting_method_by_product, t('.shopfront_sort_by_product') +.row + .three.columns.alpha + = radio_button :enterprise, :preferred_shopfront_product_sorting_method, :by_category, { 'ng-model' => 'Enterprise.preferred_shopfront_product_sorting_method' } + = label :enterprise, :preferred_shopfront_product_sorting_method_by_category, t('.shopfront_sort_by_category') + .eight.columns.omega + %textarea.fullwidth{ id: 'enterprise_preferred_shopfront_taxon_order', name: 'enterprise[preferred_shopfront_taxon_order]', rows: 6, + 'ofn-taxon-autocomplete' => '', 'multiple-selection' => 'true', placeholder: t('.shopfront_sort_by_category_placeholder'), + ng: { model: 'Enterprise.preferred_shopfront_taxon_order', readonly: "Enterprise.preferred_shopfront_product_sorting_method != 'by_category'" }} .row .three.columns.alpha = radio_button :enterprise, :preferred_shopfront_product_sorting_method, :by_producer, { 'ng-model' => 'Enterprise.preferred_shopfront_product_sorting_method' } diff --git a/config/initializers/pagy.rb b/config/initializers/pagy.rb index ff9ed076e669..cd18f12b32e8 100644 --- a/config/initializers/pagy.rb +++ b/config/initializers/pagy.rb @@ -1,15 +1,18 @@ # frozen_string_literal: true +require 'pagy/extras/arel' +require 'pagy/extras/items' +require 'pagy/extras/overflow' + + # Pagy Variables # See https://ddnexus.github.io/pagy/api/pagy#variables Pagy::DEFAULT[:items] = 100 # Items extra: Allow the client to request a custom number of items per page with an optional selector UI # See https://ddnexus.github.io/pagy/extras/items -require 'pagy/extras/items' Pagy::DEFAULT[:items_param] = :per_page Pagy::DEFAULT[:max_items] = 100 # For handling requests for non-existant pages eg: page 35 when there are only 4 pages of results -require 'pagy/extras/overflow' Pagy::DEFAULT[:overflow] = :empty_page diff --git a/spec/controllers/api/v0/order_cycles_controller_spec.rb b/spec/controllers/api/v0/order_cycles_controller_spec.rb index 15bcb6c0663d..5103fe357f10 100644 --- a/spec/controllers/api/v0/order_cycles_controller_spec.rb +++ b/spec/controllers/api/v0/order_cycles_controller_spec.rb @@ -268,13 +268,13 @@ module Api exchange.variants << product8.variants.first end - xit "displays products in new order" do + it "displays products in new order" do api_get :products, id: order_cycle.id, distributor: distributor.id expect(product_ids).to eq [product7.id, product8.id, product2.id, product3.id, product5.id, product6.id, product1.id] end - xit "displays products in correct order across multiple pages" do + it "displays products in correct order across multiple pages" do api_get :products, id: order_cycle.id, distributor: distributor.id, per_page: 3 expect(product_ids).to eq [product7.id, product8.id, product2.id] diff --git a/spec/services/products_renderer_spec.rb b/spec/services/products_renderer_spec.rb index 51a0cf1c2de4..c71373df3ed8 100644 --- a/spec/services/products_renderer_spec.rb +++ b/spec/services/products_renderer_spec.rb @@ -39,7 +39,7 @@ end describe "sorting" do - xit "sorts products by the distributor's preferred taxon list" do + it "sorts products by the distributor's preferred taxon list" do allow(distributor) .to receive(:preferred_shopfront_taxon_order) { "#{cakes.id},#{fruits.id}" } products = products_renderer.send(:products) @@ -106,7 +106,7 @@ expect(products).to eq([product_apples, product_cherries]) end - xit "filters products with property when sorting is enabled" do + it "filters products with property when sorting is enabled" do allow(distributor).to receive(:preferred_shopfront_taxon_order) { "#{fruits.id},#{cakes.id}" } diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index d0b710ad1e01..339898e673c6 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -526,7 +526,7 @@ .to eq('Enterprise "First Distributor" has been successfully updated!') end - xit "sets the preference correctly" do + it "sets the preference correctly" do expect(distributor1.preferred_shopfront_product_sorting_method).to eql("by_category") expect(distributor1.preferred_shopfront_taxon_order).to eql(taxon.id.to_s) end