Class: Spree::Promotion::Actions::CreateAdjustment

Inherits:
Spree::PromotionAction show all
Includes:
Core::CalculatedAdjustments
Defined in:
app/models/spree/promotion/actions/create_adjustment.rb

Overview

Responsible for the creation and management of an adjustment since an an adjustment uses its originator to also update its eligiblity and amount

Instance Method Summary collapse

Methods included from Core::CalculatedAdjustments

included

Instance Method Details

#compute_amount(calculable) ⇒ Object

Ensure a negative amount which does not exceed the sum of the order's item_total and ship_total


48
49
50
51
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 48

def compute_amount(calculable)
  amount = self.calculator.compute(calculable).to_f.abs
  [(calculable.item_total + calculable.ship_total), amount].min * -1
end

#create_adjustment(label, target, calculable, mandatory = false) ⇒ Object

Override of CalculatedAdjustments#create_adjustment so promotional adjustments are added all the time. They will get their eligibility set to false if the amount is 0.

Currently an adjustment is created even when its promotion is not eligible. This helps to figure out later which adjustments should be eligible as the order is being updated

BTW The order is updated (through order#update) every time an adjustment is saved


35
36
37
38
39
40
41
42
43
44
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 35

def create_adjustment(label, target, calculable, mandatory=false)
  amount = compute_amount(calculable)
  target.adjustments.create(
    amount: amount,
    source: calculable,
    originator: self,
    label: label,
    mandatory: mandatory
  )
end

#perform(options = {}) ⇒ Object

Creates the adjustment related to a promotion for the order passed through options hash


18
19
20
21
22
23
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 18

def perform(options = {})
  order = options[:order]
  return if order.promotion_credit_exists?(self)

  self.create_adjustment("#{Spree.t(:promotion)} (#{promotion.name})", order, order)
end