From 9cf55e511a1f2b710e6a9bb8a10b92db74f67a38 Mon Sep 17 00:00:00 2001 From: Tom Wilson Date: Thu, 26 Aug 2021 17:07:07 -0400 Subject: [PATCH] Hide the master variants from stock management This PR hides the master variants from the stock management list if the product has variants. This prevents a strange experience when managing inventory for these products - they don't really have SKUs, and aren't sellable in the general case. --- .../spree/admin/stock_items_controller.rb | 7 +++- .../admin/stock_items_controller_spec.rb | 33 ++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/backend/app/controllers/spree/admin/stock_items_controller.rb b/backend/app/controllers/spree/admin/stock_items_controller.rb index 2477f851e53..cbdbe035a5e 100644 --- a/backend/app/controllers/spree/admin/stock_items_controller.rb +++ b/backend/app/controllers/spree/admin/stock_items_controller.rb @@ -51,7 +51,12 @@ def load_stock_management_data def variant_scope scope = Spree::Variant.accessible_by(current_ability) - scope = scope.where(product: @product) if @product + if @product + scope = scope.where( + product: @product, + is_master: !@product.has_variants? + ) + end scope = scope.order(:sku) scope end diff --git a/backend/spec/controllers/spree/admin/stock_items_controller_spec.rb b/backend/spec/controllers/spree/admin/stock_items_controller_spec.rb index f590f18a37b..22af041cc05 100644 --- a/backend/spec/controllers/spree/admin/stock_items_controller_spec.rb +++ b/backend/spec/controllers/spree/admin/stock_items_controller_spec.rb @@ -30,20 +30,45 @@ module Admin describe "#index" do let!(:variant_1) { create(:variant) } let!(:variant_2) { create(:variant) } + let!(:product_1) { create(:product) } + let!(:product_2) { create(:product) } + let!(:variant_3) { create(:variant, product: product_2) } + let!(:variant_4) { create(:variant, product: product_2) } context "with product_slug param" do it "scopes the variants by the product" do get :index, params: { product_slug: variant_1.product.slug } - expect(assigns(:variants)).to include variant_1 - expect(assigns(:variants)).not_to include variant_2 + expect(assigns(:variants)).to contain_exactly(variant_1) + end + + context "when a product with no variants is requested" do + it "returns the master variant of the product" do + get :index, params: { product_slug: product_1.slug } + expect(assigns(:variants)).to contain_exactly(product_1.master) + end + end + + context "when a product with variants is requested" do + it "returns only the variants of the product" do + get :index, params: { product_slug: product_2.slug } + expect(assigns(:variants)).to contain_exactly(variant_3, variant_4) + end end end context "without product_slug params" do it "allows all accessible variants to be returned" do get :index - expect(assigns(:variants)).to include variant_1 - expect(assigns(:variants)).to include variant_2 + expect(assigns(:variants)).to contain_exactly( + variant_1, + variant_1.product.master, + variant_2, + variant_2.product.master, + product_1.master, + product_2.master, + variant_3, + variant_4 + ) end end end