Skip to content

Commit

Permalink
Reinstate sorting by arbitrary list of product categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-Yorkley committed Aug 11, 2023
1 parent 08c5e36 commit c564a2b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 9 deletions.
7 changes: 7 additions & 0 deletions app/services/order_cycle_distributed_products.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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").

Check warning on line 18 in app/services/order_cycle_distributed_products.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Line is too long. [173/100] Raw Output: app/services/order_cycle_distributed_products.rb:18:101: C: Layout/LineLength: Line is too long. [173/100]
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).
Expand Down
10 changes: 8 additions & 2 deletions app/services/products_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions app/views/admin/enterprises/form/_shop_preferences.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down
7 changes: 5 additions & 2 deletions config/initializers/pagy.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions spec/controllers/api/v0/order_cycles_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
4 changes: 2 additions & 2 deletions spec/services/products_renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}"
}
Expand Down
2 changes: 1 addition & 1 deletion spec/system/admin/enterprises_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c564a2b

Please sign in to comment.