Class: Spree::Shipment
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spree::Shipment
- Defined in:
- app/models/spree/shipment.rb
Instance Attribute Summary collapse
-
#special_instructions ⇒ Object
Returns the value of attribute special_instructions.
Instance Method Summary collapse
-
#cost ⇒ Object
(also: #amount)
The adjustment amount associated with this shipment (if any.) Returns only the first adjustment to match the shipment but there should never really be more than one.
- #currency ⇒ Object
-
#determine_state(order) ⇒ Object
Determines the appropriate
state
according to the following logic:. - #display_cost ⇒ Object (also: #display_amount)
- #editable_by?(user) ⇒ Boolean
- #line_items ⇒ Object
- #manifest ⇒ Object
- #shipped=(value) ⇒ Object
- #to_param ⇒ Object
-
#update!(order) ⇒ Object
Updates various aspects of the Shipment while bypassing any callbacks.
Instance Attribute Details
#special_instructions ⇒ Object
Returns the value of attribute special_instructions.
16 17 18 |
# File 'app/models/spree/shipment.rb', line 16 def special_instructions @special_instructions end |
Instance Method Details
#cost ⇒ Object Also known as: amount
The adjustment amount associated with this shipment (if any.) Returns only the first adjustment to match the shipment but there should never really be more than one.
51 52 53 |
# File 'app/models/spree/shipment.rb', line 51 def cost adjustment ? adjustment.amount : 0 end |
#currency ⇒ Object
45 46 47 |
# File 'app/models/spree/shipment.rb', line 45 def currency order.nil? ? Spree::Config[:currency] : order.currency end |
#determine_state(order) ⇒ Object
Determines the appropriate state
according to the following logic:
pending unless order is complete and order.payment_state
is paid
shipped if already shipped (ie. does not change the state) ready all other cases
114 115 116 117 118 119 |
# File 'app/models/spree/shipment.rb', line 114 def determine_state(order) return 'pending' unless order.can_ship? return 'pending' if inventory_units.any? &:backordered? return 'shipped' if state == 'shipped' order.paid? ? 'ready' : 'pending' end |
#display_cost ⇒ Object Also known as: display_amount
57 58 59 |
# File 'app/models/spree/shipment.rb', line 57 def display_cost Spree::Money.new(cost, { :currency => currency }) end |
#editable_by?(user) ⇒ Boolean
81 82 83 |
# File 'app/models/spree/shipment.rb', line 81 def editable_by?(user) !shipped? end |
#line_items ⇒ Object
91 92 93 94 95 96 97 |
# File 'app/models/spree/shipment.rb', line 91 def line_items if order.complete? and Spree::Config[:track_inventory_levels] order.line_items.select { |li| inventory_units.map(&:variant_id).include?(li.variant_id) } else order.line_items end end |
#manifest ⇒ Object
85 86 87 88 89 |
# File 'app/models/spree/shipment.rb', line 85 def manifest inventory_units.group_by(&:variant).map do |i| OpenStruct.new(:variant => i.first, :quantity => i.last.length) end end |
#shipped=(value) ⇒ Object
40 41 42 43 |
# File 'app/models/spree/shipment.rb', line 40 def shipped=(value) return unless value == '1' && shipped_at.nil? self.shipped_at = Time.now end |
#to_param ⇒ Object
34 35 36 37 38 |
# File 'app/models/spree/shipment.rb', line 34 def to_param number if number generate_shipment_number unless number number.to_s.to_url.upcase end |
#update!(order) ⇒ Object
Updates various aspects of the Shipment while bypassing any callbacks. Note that this method takes an explicit reference to the Order object. This is necessary because the association actually has a stale (and unsaved) copy of the Order and so it will not yield the correct results.
102 103 104 105 106 107 |
# File 'app/models/spree/shipment.rb', line 102 def update!(order) old_state = state new_state = determine_state(order) update_column 'state', new_state after_ship if new_state == 'shipped' and old_state != 'shipped' end |