-
-
Save jhawthorn/cad02bc01a902a83cf37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Spree | |
module Stock | |
module Splitter | |
class OneItemPerPackage < Base | |
cattr_accessor :shipping_category_names do | |
[] | |
end | |
def split(packages) | |
packages_to_be_processed = packages.select do |package| | |
package.contents.any? do |item| | |
self.class.shipping_category_names.include?(item.line_item.variant.shipping_category.name) | |
end | |
end | |
packages_to_be_processed.each do |package| | |
packages.delete(package) | |
package.contents.each do |item| | |
item.quantity.times do | |
package = build_package | |
package.add(item.line_item, 1, item.state, item.variant) | |
packages << package | |
end | |
end | |
end | |
packages | |
end | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
module Spree | |
module Stock | |
module Splitter | |
describe OneItemPerPackage do | |
let(:line_item1) do | |
line_item = build(:line_item, variant: build(:variant)) | |
line_item.variant.product.shipping_category = create(:shipping_category, name: 'Mattress') | |
line_item | |
end | |
before do | |
Spree::Stock::Splitter::OneItemPerPackage.shipping_category_names = ["Mattress"] | |
end | |
let(:packer) { build(:stock_packer) } | |
subject { Spree::Stock::Splitter::OneItemPerPackage.new(packer) } | |
it "allows only one item in each package from the 'Mattress' shipping category" do | |
package1 = Package.new(packer.stock_location, packer.order) | |
package1.add line_item1, 2 | |
packages = subject.split([package1]) | |
expect(packages.count).to eq(2) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment