Class: Spree::Promotion::Actions::CreateAdjustment
- Inherits:
-
Spree::PromotionAction
- Object
- ActiveRecord::Base
- Spree::PromotionAction
- Spree::Promotion::Actions::CreateAdjustment
- Defined in:
- app/models/spree/promotion/actions/create_adjustment.rb
Instance Method Summary collapse
-
#compute_amount(calculable) ⇒ Object
Ensure a negative amount which does not exceed the sum of the order’s item_total and ship_total.
-
#perform(options = {}) ⇒ Object
Creates the adjustment related to a promotion for the order passed through options hash.
Methods included from Core::AdjustmentSource
Methods included from Core::CalculatedAdjustments
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
38 39 40 41 |
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 38 def compute_amount(calculable) amount = self.calculator.compute(calculable).to_f.abs [(calculable.item_total + calculable.ship_total), amount].min * -1 end |
#perform(options = {}) ⇒ Object
Creates the adjustment related to a promotion for the order passed through options hash
Returns ‘true` if an adjustment is applied to an order, `false` if the promotion has already been applied.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 20 def perform( = {}) order = [:order] return if promotion_credit_exists?(order) amount = compute_amount(order) return if amount == 0 Spree::Adjustment.create!( amount: amount, order: order, adjustable: order, source: self, label: "#{Spree.t(:promotion)} (#{promotion.name})" ) true end |