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.
-
#shipment ⇒ Object
Returns the value of attribute shipment.
-
#stock_location ⇒ Object
readonly
Returns the value of attribute stock_location.
Instance Method Summary collapse
-
#add(inventory_unit, state = :on_hand) ⇒ Object
Adds an inventory unit to this package.
-
#add_multiple(inventory_units, state = :on_hand) ⇒ Object
Adds multiple inventory units to this package.
-
#backordered ⇒ Array<Spree::Stock::ContentItem>
The content items in this package which are backordered.
-
#currency ⇒ String
The currency of the order this package belongs to.
-
#empty? ⇒ Boolean
True if there are no inventory units in this package.
-
#find_item(inventory_unit, state = nil) ⇒ Object
Find a content item in this package by inventory unit and optionally state.
-
#initialize(stock_location, contents = []) ⇒ Package
constructor
A new instance of Package.
-
#on_hand ⇒ Array<Spree::Stock::ContentItem>
The content items in this package which are on hand.
-
#order ⇒ Spree::Order
The order associated with this package.
-
#quantity(state = nil) ⇒ Fixnum
The number of inventory units in the package, counting only those in the given state if it was specified.
-
#remove(inventory_unit) ⇒ Object
Removes a given inventory unit from this package.
-
#shipping_categories ⇒ Array<Spree::ShippingCategory>
The shipping categories of the variants in this package.
-
#shipping_methods ⇒ ActiveRecord::Relation
The [Spree::ShippingMethod]s available for this pacakge based on the stock location and shipping categories.
-
#to_shipment ⇒ Spree::Shipment
A new shipment containing this package’s inventory units, with the appropriate shipping rates and associated with the correct stock location.
-
#weight ⇒ Float
The summed weight of the contents of this package.
Constructor Details
#initialize(stock_location, contents = []) ⇒ Package
Returns a new instance of Package.
11 12 13 14 |
# File 'app/models/spree/stock/package.rb', line 11 def initialize(stock_location, contents = []) @stock_location = stock_location @contents = contents end |
Instance Attribute Details
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
6 7 8 |
# File 'app/models/spree/stock/package.rb', line 6 def contents @contents end |
#shipment ⇒ Object
Returns the value of attribute shipment.
7 8 9 |
# File 'app/models/spree/stock/package.rb', line 7 def shipment @shipment end |
#stock_location ⇒ Object (readonly)
Returns the value of attribute stock_location.
6 7 8 |
# File 'app/models/spree/stock/package.rb', line 6 def stock_location @stock_location end |
Instance Method Details
#add(inventory_unit, state = :on_hand) ⇒ Object
Adds an inventory unit to this package.
22 23 24 |
# File 'app/models/spree/stock/package.rb', line 22 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
Adds multiple inventory units to this package.
32 33 34 |
# File 'app/models/spree/stock/package.rb', line 32 def add_multiple(inventory_units, state = :on_hand) inventory_units.each { |inventory_unit| add(inventory_unit, state) } end |
#backordered ⇒ Array<Spree::Stock::ContentItem>
Returns the content items in this package which are backordered.
65 66 67 |
# File 'app/models/spree/stock/package.rb', line 65 def backordered contents.select(&:backordered?) end |
#currency ⇒ String
Returns the currency of the order this package belongs to.
99 100 101 |
# File 'app/models/spree/stock/package.rb', line 99 def currency order.currency end |
#empty? ⇒ Boolean
Returns true if there are no inventory units in this package.
94 95 96 |
# File 'app/models/spree/stock/package.rb', line 94 def empty? quantity == 0 end |
#find_item(inventory_unit, state = nil) ⇒ Object
Find a content item in this package by inventory unit and optionally state.
76 77 78 79 80 81 |
# File 'app/models/spree/stock/package.rb', line 76 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 |
#on_hand ⇒ Array<Spree::Stock::ContentItem>
Returns the content items in this package which are on hand.
59 60 61 |
# File 'app/models/spree/stock/package.rb', line 59 def on_hand contents.select(&:on_hand?) end |
#order ⇒ Spree::Order
Returns the order associated with this package.
46 47 48 49 50 |
# File 'app/models/spree/stock/package.rb', line 46 def order # Fix regression that removed package.order. # Find it dynamically through an inventory_unit. contents.detect { |item| !!item.try(:line_item).try(:order) }.try(:line_item).try(:order) end |
#quantity(state = nil) ⇒ Fixnum
Returns the number of inventory units in the package, counting only those in the given state if it was specified.
87 88 89 90 |
# File 'app/models/spree/stock/package.rb', line 87 def quantity(state = nil) matched_contents = state.nil? ? contents : contents.select { |content| content.state.to_s == state.to_s } matched_contents.sum(&:quantity) end |
#remove(inventory_unit) ⇒ Object
Removes a given inventory unit from this package.
40 41 42 43 |
# File 'app/models/spree/stock/package.rb', line 40 def remove(inventory_unit) item = find_item(inventory_unit) @contents -= [item] if item end |
#shipping_categories ⇒ Array<Spree::ShippingCategory>
Returns the shipping categories of the variants in this package.
105 106 107 |
# File 'app/models/spree/stock/package.rb', line 105 def shipping_categories Spree::ShippingCategory.where(id: shipping_category_ids) end |
#shipping_methods ⇒ ActiveRecord::Relation
Returns the [Spree::ShippingMethod]s available for this pacakge based on the stock location and shipping categories.
111 112 113 114 115 |
# File 'app/models/spree/stock/package.rb', line 111 def shipping_methods Spree::ShippingMethod. with_all_shipping_category_ids(shipping_category_ids). available_in_stock_location(stock_location) end |
#to_shipment ⇒ Spree::Shipment
Returns a new shipment containing this package’s inventory units, with the appropriate shipping rates and associated with the correct stock location.
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/spree/stock/package.rb', line 120 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( order: order, stock_location: stock_location, inventory_units: contents.map(&:inventory_unit) ) end |
#weight ⇒ Float
Returns the summed weight of the contents of this package.
53 54 55 |
# File 'app/models/spree/stock/package.rb', line 53 def weight contents.sum(&:weight) end |