Class: Spree::Promotion::Actions::CreateAdjustment
- Inherits:
-
Spree::PromotionAction
- Object
- 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.
- #preload_relations ⇒ Object
-
#remove_from(order) ⇒ void
Removes any adjustments generated by this action from the order.
Methods inherited from Spree::PromotionAction
#available_calculators, #to_partial_path
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
44 45 46 47 48 49 |
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 44 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.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 27 def perform( = {}) order = [:order] return if promotion_credit_exists?(order) amount = compute_amount(order) order.adjustments.create!( amount:, 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 |
#preload_relations ⇒ Object
18 19 20 |
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 18 def preload_relations [:calculator] end |
#remove_from(order) ⇒ void
This method returns an undefined value.
Removes any adjustments generated by this action from the order.
54 55 56 57 58 59 60 |
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 54 def remove_from(order) order.adjustments.each do |adjustment| if adjustment.source == self order.adjustments.destroy(adjustment) end end end |