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



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

def self.model_name
  Spree::Exchange
end

.param_keyObject



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

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.map(&:total).sum
end

#perform!Object



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

def perform!
  shipments = Spree::Stock::Coordinator.new(@order, @reimbursement_objects.map(&:build_exchange_inventory_unit)).shipments
  if shipments.flat_map(&:inventory_units).size != @reimbursement_objects.size
    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!(@order)
    shipment.finalize!
  end
end

#to_keyObject



33
34
35
# File 'app/models/spree/exchange.rb', line 33

def to_key
  nil
end