Class: Spree::Stock::Package
- Inherits:
-
Object
- Object
- Spree::Stock::Package
- Defined in:
- app/models/spree/stock/package.rb
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#shipping_rates ⇒ Object
Returns the value of attribute shipping_rates.
-
#stock_location ⇒ Object
readonly
Returns the value of attribute stock_location.
Instance Method Summary collapse
- #add(inventory_unit, state = :on_hand) ⇒ Object
- #add_multiple(inventory_units, state = :on_hand) ⇒ Object
- #backordered ⇒ Object
- #contents_by_weight ⇒ Object
- #currency ⇒ Object
- #dimension ⇒ Object
- #empty? ⇒ Boolean
- #find_item(inventory_unit, state = nil) ⇒ Object
-
#initialize(stock_location, contents = []) ⇒ Package
constructor
A new instance of Package.
- #inspect ⇒ Object
- #on_hand ⇒ Object
-
#order ⇒ Object
Fix regression that removed package.order.
- #quantity(state = nil) ⇒ Object
- #remove(inventory_unit) ⇒ Object
- #shipping_categories ⇒ Object
- #shipping_methods ⇒ Object
- #to_shipment ⇒ Object
- #volume ⇒ Object
- #weight ⇒ Object
Constructor Details
#initialize(stock_location, contents = []) ⇒ Package
Returns a new instance of Package.
7 8 9 10 11 |
# File 'app/models/spree/stock/package.rb', line 7 def initialize(stock_location, contents=[]) @stock_location = stock_location @contents = contents @shipping_rates = Array.new end |
Instance Attribute Details
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
4 5 6 |
# File 'app/models/spree/stock/package.rb', line 4 def contents @contents end |
#shipping_rates ⇒ Object
Returns the value of attribute shipping_rates.
5 6 7 |
# File 'app/models/spree/stock/package.rb', line 5 def shipping_rates @shipping_rates end |
#stock_location ⇒ Object (readonly)
Returns the value of attribute stock_location.
4 5 6 |
# File 'app/models/spree/stock/package.rb', line 4 def stock_location @stock_location end |
Instance Method Details
#add(inventory_unit, state = :on_hand) ⇒ Object
13 14 15 |
# File 'app/models/spree/stock/package.rb', line 13 def add(inventory_unit, state = :on_hand) contents << ContentItem.new(inventory_unit, state) unless find_item(inventory_unit) end |
#add_multiple(inventory_units, state = :on_hand) ⇒ Object
17 18 19 |
# File 'app/models/spree/stock/package.rb', line 17 def add_multiple(inventory_units, state = :on_hand) inventory_units.each { |inventory_unit| add(inventory_unit, state) } end |
#backordered ⇒ Object
40 41 42 |
# File 'app/models/spree/stock/package.rb', line 40 def backordered contents.select(&:backordered?) end |
#contents_by_weight ⇒ Object
91 92 93 |
# File 'app/models/spree/stock/package.rb', line 91 def contents_by_weight contents.sort { |x, y| y.weight <=> x.weight } end |
#currency ⇒ Object
60 61 62 |
# File 'app/models/spree/stock/package.rb', line 60 def currency order.currency end |
#dimension ⇒ Object
99 100 101 |
# File 'app/models/spree/stock/package.rb', line 99 def dimension contents.sum(&:dimension) end |
#empty? ⇒ Boolean
56 57 58 |
# File 'app/models/spree/stock/package.rb', line 56 def empty? quantity == 0 end |
#find_item(inventory_unit, state = nil) ⇒ Object
44 45 46 47 48 49 |
# File 'app/models/spree/stock/package.rb', line 44 def find_item(inventory_unit, state = nil) contents.detect do |item| item.inventory_unit == inventory_unit && (!state || item.state.to_s == state.to_s) end end |
#inspect ⇒ Object
72 73 74 75 76 |
# File 'app/models/spree/stock/package.rb', line 72 def inspect contents.map do |content_item| "#{content_item.variant.name} #{content_item.state}" end.join(' / ') end |
#on_hand ⇒ Object
36 37 38 |
# File 'app/models/spree/stock/package.rb', line 36 def on_hand contents.select(&:on_hand?) end |
#order ⇒ Object
Fix regression that removed package.order. Find it dynamically through an inventory_unit.
28 29 30 |
# File 'app/models/spree/stock/package.rb', line 28 def order contents.detect {|item| !!item.try(:inventory_unit).try(:order) }.try(:inventory_unit).try(:order) end |
#quantity(state = nil) ⇒ Object
51 52 53 54 |
# File 'app/models/spree/stock/package.rb', line 51 def quantity(state = nil) matched_contents = state.nil? ? contents : contents.select { |c| c.state.to_s == state.to_s } matched_contents.map(&:quantity).sum end |
#remove(inventory_unit) ⇒ Object
21 22 23 24 |
# File 'app/models/spree/stock/package.rb', line 21 def remove(inventory_unit) item = find_item(inventory_unit) @contents -= [item] if item end |
#shipping_categories ⇒ Object
64 65 66 |
# File 'app/models/spree/stock/package.rb', line 64 def shipping_categories contents.map { |item| item.variant.shipping_category }.compact.uniq end |
#shipping_methods ⇒ Object
68 69 70 |
# File 'app/models/spree/stock/package.rb', line 68 def shipping_methods shipping_categories.map(&:shipping_methods).reduce(:&).to_a end |
#to_shipment ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/spree/stock/package.rb', line 78 def to_shipment # At this point we should only have one content item per inventory unit # across the entire set of inventory units to be shipped, which has been # taken care of by the Prioritizer contents.each { |content_item| content_item.inventory_unit.state = content_item.state.to_s } Spree::Shipment.new( stock_location: stock_location, shipping_rates: shipping_rates, inventory_units: contents.map(&:inventory_unit) ) end |
#volume ⇒ Object
95 96 97 |
# File 'app/models/spree/stock/package.rb', line 95 def volume contents.sum(&:volume) end |
#weight ⇒ Object
32 33 34 |
# File 'app/models/spree/stock/package.rb', line 32 def weight contents.sum(&:weight) end |