diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index ac3e0bbf..ca9ed083 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -57,7 +57,7 @@ def quantity_with_part_line_items(quantity) end def quantity_without_part_line_items(quantity) - product.assemblies_parts.each_with_object({}) do |ap, hash| + variant.parts_variants.each_with_object({}) do |ap, hash| hash[ap.part] = ap.count * quantity end end diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index 20472f32..bc4bd225 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -9,35 +9,100 @@ module Spree context "bundle parts stock" do let(:parts) { (1..2).map { create(:variant) } } - before { product.master.parts << parts } + context 'assembly without variants' do + before { product.master.parts << parts } - context "one of them not in stock" do - before do - part = product.parts.first - part.stock_items.update_all backorderable: false + context "one of them not in stock" do + before do + part = product.parts.first + part.stock_items.update_all backorderable: false + + expect(part).not_to be_in_stock + end - expect(part).not_to be_in_stock + it "doesn't save line item quantity" do + expect { order.contents.add(variant, 10) }.to( + raise_error ActiveRecord::RecordInvalid + ) + end end - it "doesn't save line item quantity" do - expect { order.contents.add(variant, 10) }.to( - raise_error ActiveRecord::RecordInvalid - ) + context "in stock" do + before do + parts.each do |part| + part.stock_items.first.set_count_on_hand(10) + end + expect(parts[0]).to be_in_stock + expect(parts[1]).to be_in_stock + end + + it "saves line item quantity" do + line_item = order.contents.add(variant, 10) + expect(line_item).to be_valid + end end end - context "in stock" do + context 'multi-variant assembly' do + let(:assembly1) { variant } + let(:assembly2) { create(:variant) } + + let(:parts2) { (1..2).map { create(:variant) } } + before do - parts.each do |part| - part.stock_items.first.set_count_on_hand(10) + product.variants << assembly1 + product.variants << assembly2 + + assembly1.parts << parts + assembly2.parts << parts2 + end + + context "one part of desired assembly variant not in stock" do + before do + part = product.parts.first + part.stock_items.update_all backorderable: false + + expect(part).not_to be_in_stock + end + + it "doesn't save line item quantity" do + expect { order.contents.add(variant, 10) }.to( + raise_error ActiveRecord::RecordInvalid + ) end - expect(parts[0]).to be_in_stock - expect(parts[1]).to be_in_stock end - it "saves line item quantity" do - line_item = order.contents.add(variant, 10) - expect(line_item).to be_valid + context "in stock" do + before do + [parts, parts2].each do |parts_list| + parts_list.each do |part| + part.stock_items.first.set_count_on_hand(10) + end + + expect(parts_list[0]).to be_in_stock + expect(parts_list[1]).to be_in_stock + end + end + + it "saves line item quantity" do + line_item = order.contents.add(variant, 10) + expect(line_item).to be_valid + end + + context "one part of other assembly variant not in stock" do + before do + part = parts2.first + part.stock_items.update_all backorderable: false + part.stock_items.first.set_count_on_hand(0) + + expect(part).not_to be_in_stock + end + + it "saves line item quantity" do + line_item = order.contents.add(variant, 10) + expect(line_item).to be_valid + end + end end end end