mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Fix easy rubocop issues
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Spree
|
||||
module Stock
|
||||
class Package
|
||||
@@ -6,14 +8,14 @@ module Spree
|
||||
attr_reader :stock_location, :order, :contents
|
||||
attr_accessor :shipping_rates
|
||||
|
||||
def initialize(stock_location, order, contents=[])
|
||||
def initialize(stock_location, order, contents = [])
|
||||
@stock_location = stock_location
|
||||
@order = order
|
||||
@contents = contents
|
||||
@shipping_rates = Array.new
|
||||
@shipping_rates = []
|
||||
end
|
||||
|
||||
def add(variant, quantity, state=:on_hand)
|
||||
def add(variant, quantity, state = :on_hand)
|
||||
contents << ContentItem.new(variant, quantity, state)
|
||||
end
|
||||
|
||||
@@ -29,26 +31,26 @@ module Spree
|
||||
contents.select { |item| item.state == :backordered }
|
||||
end
|
||||
|
||||
def find_item(variant, state=:on_hand)
|
||||
def find_item(variant, state = :on_hand)
|
||||
contents.select do |item|
|
||||
item.variant == variant &&
|
||||
item.state == state
|
||||
item.state == state
|
||||
end.first
|
||||
end
|
||||
|
||||
def quantity(state=nil)
|
||||
def quantity(state = nil)
|
||||
case state
|
||||
when :on_hand
|
||||
on_hand.sum { |item| item.quantity }
|
||||
on_hand.sum(&:quantity)
|
||||
when :backordered
|
||||
backordered.sum { |item| item.quantity }
|
||||
backordered.sum(&:quantity)
|
||||
else
|
||||
contents.sum { |item| item.quantity }
|
||||
contents.sum(&:quantity)
|
||||
end
|
||||
end
|
||||
|
||||
def empty?
|
||||
quantity == 0
|
||||
quantity.zero?
|
||||
end
|
||||
|
||||
def flattened
|
||||
@@ -74,7 +76,7 @@ module Spree
|
||||
end
|
||||
|
||||
def currency
|
||||
#TODO calculate from first variant?
|
||||
# TODO calculate from first variant?
|
||||
end
|
||||
|
||||
def shipping_categories
|
||||
@@ -82,7 +84,7 @@ module Spree
|
||||
end
|
||||
|
||||
def shipping_methods
|
||||
shipping_categories.map { |sc| sc.shipping_methods }.flatten.uniq
|
||||
shipping_categories.map(&:shipping_methods).flatten.uniq
|
||||
end
|
||||
|
||||
def inspect
|
||||
@@ -100,7 +102,7 @@ module Spree
|
||||
shipment.shipping_rates = shipping_rates
|
||||
|
||||
contents.each do |item|
|
||||
item.quantity.times do |n|
|
||||
item.quantity.times do
|
||||
unit = shipment.inventory_units.build
|
||||
unit.pending = true
|
||||
unit.order = order
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
module Spree
|
||||
@@ -51,9 +53,9 @@ module Spree
|
||||
|
||||
it 'set contents from flattened' do
|
||||
flattened = [Package::ContentItem.new(variant, 1, :on_hand),
|
||||
Package::ContentItem.new(variant, 1, :on_hand),
|
||||
Package::ContentItem.new(variant, 1, :backordered),
|
||||
Package::ContentItem.new(variant, 1, :backordered)]
|
||||
Package::ContentItem.new(variant, 1, :on_hand),
|
||||
Package::ContentItem.new(variant, 1, :backordered),
|
||||
Package::ContentItem.new(variant, 1, :backordered)]
|
||||
|
||||
subject.flattened = flattened
|
||||
subject.on_hand.size.should eq 1
|
||||
@@ -66,8 +68,10 @@ module Spree
|
||||
it 'builds a list of shipping methods from all categories' do
|
||||
shipping_method1 = create(:shipping_method)
|
||||
shipping_method2 = create(:shipping_method)
|
||||
variant1 = mock_model(Variant, shipping_category: shipping_method1.shipping_categories.first)
|
||||
variant2 = mock_model(Variant, shipping_category: shipping_method2.shipping_categories.first)
|
||||
variant1 = mock_model(Variant,
|
||||
shipping_category: shipping_method1.shipping_categories.first)
|
||||
variant2 = mock_model(Variant,
|
||||
shipping_category: shipping_method2.shipping_categories.first)
|
||||
variant3 = mock_model(Variant, shipping_category: nil)
|
||||
contents = [Package::ContentItem.new(variant1, 1),
|
||||
Package::ContentItem.new(variant1, 1),
|
||||
@@ -78,30 +82,31 @@ module Spree
|
||||
package.shipping_methods.size.should eq 2
|
||||
end
|
||||
|
||||
|
||||
it "can convert to a shipment" do
|
||||
flattened = [Package::ContentItem.new(variant, 2, :on_hand),
|
||||
Package::ContentItem.new(variant, 1, :backordered)]
|
||||
Package::ContentItem.new(variant, 1, :backordered)]
|
||||
subject.flattened = flattened
|
||||
|
||||
shipping_method = build(:shipping_method)
|
||||
subject.shipping_rates = [ Spree::ShippingRate.new(shipping_method: shipping_method, cost: 10.00, selected: true) ]
|
||||
subject.shipping_rates = [
|
||||
Spree::ShippingRate.new(shipping_method: shipping_method, cost: 10.00, selected: true)
|
||||
]
|
||||
|
||||
shipment = subject.to_shipment
|
||||
shipment.order.should == subject.order
|
||||
shipment.stock_location.should == subject.stock_location
|
||||
shipment.inventory_units.size.should == 3
|
||||
expect(shipment.order).to eq subject.order
|
||||
expect(shipment.stock_location).to eq subject.stock_location
|
||||
expect(shipment.inventory_units.size).to eq 3
|
||||
|
||||
first_unit = shipment.inventory_units.first
|
||||
first_unit.variant.should == variant
|
||||
first_unit.state.should == 'on_hand'
|
||||
first_unit.order.should == subject.order
|
||||
expect(first_unit.variant).to eq variant
|
||||
expect(first_unit.state).to eq 'on_hand'
|
||||
expect(first_unit.order).to eq subject.order
|
||||
first_unit.should be_pending
|
||||
|
||||
last_unit = shipment.inventory_units.last
|
||||
last_unit.variant.should == variant
|
||||
last_unit.state.should == 'backordered'
|
||||
last_unit.order.should == subject.order
|
||||
expect(last_unit.variant).to eq variant
|
||||
expect(last_unit.state).to eq 'backordered'
|
||||
expect(last_unit.order).to eq subject.order
|
||||
|
||||
shipment.shipping_method.should eq shipping_method
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user