Skip to content

Commit

Permalink
Merge pull request #4155 from tmtrademarked/hide_master_from_stock
Browse files Browse the repository at this point in the history
Hide the master variants from stock management
  • Loading branch information
kennyadsl authored Sep 1, 2021
2 parents 5cd7e9c + 9cf55e5 commit 3b0b76b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b0b76b

Please sign in to comment.