Class: Spree::Promotion::Actions::CreateAdjustment
- Inherits:
-
Spree::PromotionAction
- Object
- ActiveRecord::Base
- Base
- Spree::PromotionAction
- Spree::Promotion::Actions::CreateAdjustment
- Includes:
- AdjustmentSource, CalculatedAdjustments
- 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.
-
#remove_from(order) ⇒ void
Removes any adjustments generated by this action from the order.
Methods included from AdjustmentSource
#remove_adjustments_from_incomplete_orders
Methods included from CalculatedAdjustments
#calculator_type, #calculator_type=
Methods inherited from Spree::PromotionAction
Methods inherited from Base
Methods included from Core::Permalinks
#generate_permalink, #save_permalink
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
40 41 42 43 44 45 |
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 40 def compute_amount(calculable) amount = calculator.compute(calculable) amount ||= BigDecimal(0) amount = amount.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.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 23 def perform( = {}) order = [:order] return if promotion_credit_exists?(order) amount = compute_amount(order) order.adjustments.create!( amount: amount, order: order, source: self, promotion_code: [:promotion_code], label: I18n.t('spree.adjustment_labels.order', promotion: Spree::Promotion.model_name.human, promotion_name: promotion.name) ) true end |
#remove_from(order) ⇒ void
This method returns an undefined value.
Removes any adjustments generated by this action from the order.
50 51 52 53 54 55 56 |
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 50 def remove_from(order) order.adjustments.each do |adjustment| if adjustment.source == self order.adjustments.destroy(adjustment) end end end |