Class: Spree::Exchange

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
app/models/spree/exchange.rb

Defined Under Namespace

Classes: UnableToCreateShipments

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, reimbursement_objects) ⇒ Exchange

Returns a new instance of Exchange.



8
9
10
11
# File 'app/models/spree/exchange.rb', line 8

def initialize(order, reimbursement_objects)
  @order = order
  @reimbursement_objects = reimbursement_objects
end

Class Method Details

.param_keyObject



41
42
43
# File 'app/models/spree/exchange.rb', line 41

def self.param_key
  "spree_exchange"
end

Instance Method Details

#descriptionObject



13
14
15
16
17
# File 'app/models/spree/exchange.rb', line 13

def description
  @reimbursement_objects.map do |reimbursement_object|
    "#{reimbursement_object.variant.options_text} => #{reimbursement_object.exchange_variant.options_text}"
  end.join(" | ")
end

#display_amountObject



19
20
21
# File 'app/models/spree/exchange.rb', line 19

def display_amount
  Spree::Money.new @reimbursement_objects.sum(&:total)
end

#perform!Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/spree/exchange.rb', line 23

def perform!
  begin
    shipments = Spree::Config.stock.coordinator_class.new(@order, @reimbursement_objects.map(&:build_exchange_inventory_unit)).shipments
  rescue Spree::Order::InsufficientStock
    raise UnableToCreateShipments.new("Could not generate shipments for all items. Out of stock?")
  end
  @order.shipments += shipments
  @order.save!
  shipments.each do |shipment|
    shipment.update_state
    shipment.finalize!
  end
end

#to_keyObject



37
38
39
# File 'app/models/spree/exchange.rb', line 37

def to_key
  nil
end