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.



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

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

Class Method Details

.param_keyObject



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

def self.param_key
  "spree_exchange"
end

Instance Method Details

#descriptionObject



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

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

#display_amountObject



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

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

#perform!Object



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

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!(@order)
    shipment.finalize!
  end
end

#to_keyObject



35
36
37
# File 'app/models/spree/exchange.rb', line 35

def to_key
  nil
end