Class: Spree::Promotion::Actions::CreateItemAdjustments
- Inherits:
-
Spree::PromotionAction
- Object
- ActiveRecord::Base
- Base
- Spree::PromotionAction
- Spree::Promotion::Actions::CreateItemAdjustments
- Defined in:
- app/models/spree/promotion/actions/create_item_adjustments.rb
Instance Method Summary collapse
-
#compute_amount(adjustable) ⇒ Object
Ensure a negative amount which does not exceed the sum of the order’s item_total and ship_total.
- #create_adjustment(adjustable, order) ⇒ Object
- #perform(payload = {}) ⇒ Object
Methods included from Core::AdjustmentSource
Methods included from Core::CalculatedAdjustments
Methods inherited from Base
Methods included from Spree::Preferences::Preferable
#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Instance Method Details
#compute_amount(adjustable) ⇒ Object
Ensure a negative amount which does not exceed the sum of the order’s item_total and ship_total
46 47 48 49 50 |
# File 'app/models/spree/promotion/actions/create_item_adjustments.rb', line 46 def compute_amount(adjustable) promotion_amount = self.calculator.compute(adjustable).to_f.abs [adjustable.amount, promotion_amount].min * -1 end |
#create_adjustment(adjustable, order) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/spree/promotion/actions/create_item_adjustments.rb', line 31 def create_adjustment(adjustable, order) amount = self.compute_amount(adjustable) return if amount == 0 return if promotion.product_ids.present? and !promotion.product_ids.include?(adjustable.product.id) self.adjustments.create!( amount: amount, adjustable: adjustable, order: order, label: "#{Spree.t(:promotion)} (#{promotion.name})", ) true end |
#perform(payload = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/models/spree/promotion/actions/create_item_adjustments.rb', line 15 def perform(payload = {}) order = payload[:order] # Find only the orders' line items which have not already been adjusted by this promotion # HACK: Need to use [0] because `pluck` may return an empty array, which AR helpfully # coverts to meaning NOT IN (NULL) and the DB isn't happy about that. already_adjusted_line_items = [0] + self.adjustments. where(adjustable_id: order.line_items.pluck(:id), adjustable_type: 'Spree::LineItem'). pluck(:adjustable_id) result = false order.line_items.where("id NOT IN (?)", already_adjusted_line_items).find_each do |line_item| current_result = self.create_adjustment(line_item, order) result ||= current_result end return result end |