Class: Spree::Exchange

Inherits:
Object
  • Object
show all
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.



5
6
7
8
# File 'app/models/spree/exchange.rb', line 5

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

Class Method Details

.model_nameObject



46
47
48
# File 'app/models/spree/exchange.rb', line 46

def self.model_name
  Spree::Exchange
end

.param_keyObject



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

def self.param_key
  'spree_exchange'
end

Instance Method Details

#descriptionObject



10
11
12
13
14
# File 'app/models/spree/exchange.rb', line 10

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

#display_amountObject



16
17
18
# File 'app/models/spree/exchange.rb', line 16

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

#perform!Object



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

def perform!
  new_exchange_inventory_units = @reimbursement_objects.map(&:build_default_exchange_inventory_unit)
  shipments = Spree::Stock::Coordinator.new(@order, new_exchange_inventory_units).shipments
  shipments_units = shipments.flat_map(&:inventory_units)

  if shipments_units.sum(&:quantity) != new_exchange_inventory_units.sum(&:quantity)
    raise UnableToCreateShipments, 'Could not generate shipments for all items. Out of stock?'
  end

  @order.shipments += shipments
  @order.save!

  shipments.each do |shipment|
    shipment.update!(@order)
    shipment.finalize!
  end
end

#to_keyObject



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

def to_key
  nil
end