Sha256: 59a2ee21172c34878fae3677c53be966bc764434f7b593a3ecc2b25f169920b2

Contents?: true

Size: 1.88 KB

Versions: 42

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require 'discard'

module Spree
  # Base class for all types of promotion action.
  #
  # PromotionActions perform the necessary tasks when a promotion is activated
  # by an event and determined to be eligible.
  class PromotionAction < Spree::Base
    acts_as_paranoid
    include Spree::ParanoiaDeprecations

    include Discard::Model
    self.discard_column = :deleted_at

    belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_actions

    scope :of_type, ->(t) { where(type: Array.wrap(t).map(&:to_s)) }
    scope :shipping, -> { of_type(Spree::Config.environment.promotions.shipping_actions.to_a) }

    # Updates the state of the order or performs some other action depending on
    # the subclass options will contain the payload from the event that
    # activated the promotion. This will include the key :user which allows
    # user based actions to be performed in addition to actions on the order
    #
    # @note This method should be overriden in subclassses.
    def perform(_options = {})
      raise 'perform should be implemented in a sub-class of PromotionAction'
    end

    # Removes the action from an order
    #
    # @note This method should be overriden in subclassses.
    #
    # @param order [Spree::Order] the order to remove the action from
    # @return [void]
    def remove_from(order)
      Spree::Deprecation.warn("#{self.class.name.inspect} does not define #remove_from. The default behavior may be incorrect and will be removed in a future version of Solidus.", caller)
      [order, *order.line_items, *order.shipments].each do |item|
        item.adjustments.each do |adjustment|
          if adjustment.source == self
            item.adjustments.destroy(adjustment)
          end
        end
      end
    end

    def to_partial_path
      "spree/admin/promotions/actions/#{model_name.element}"
    end
  end
end

Version data entries

42 entries across 42 versions & 2 rubygems

Version Path
solidus_core-2.9.6 app/models/spree/promotion_action.rb
solidus_core-2.8.6 app/models/spree/promotion_action.rb
solidus_core-2.9.5 app/models/spree/promotion_action.rb
solidus_core-2.9.4 app/models/spree/promotion_action.rb
solidus_core-2.6.6 app/models/spree/promotion_action.rb
solidus_core-2.7.4 app/models/spree/promotion_action.rb
solidus_core-2.8.5 app/models/spree/promotion_action.rb
solidus_core-2.9.3 app/models/spree/promotion_action.rb
solidus_core-2.9.2 app/models/spree/promotion_action.rb
solidus_core-2.7.3 app/models/spree/promotion_action.rb
solidus_core-2.6.5 app/models/spree/promotion_action.rb
solidus_core-2.9.1 app/models/spree/promotion_action.rb
solidus_core-2.9.0 app/models/spree/promotion_action.rb
solidus_core-2.9.0.rc.1 app/models/spree/promotion_action.rb
solidus_core-2.8.4 app/models/spree/promotion_action.rb
solidus_core-2.8.3 app/models/spree/promotion_action.rb
solidus_core-2.7.2 app/models/spree/promotion_action.rb
solidus_core-2.6.4 app/models/spree/promotion_action.rb
solidus_core-2.8.2 app/models/spree/promotion_action.rb
solidus_core-2.8.1 app/models/spree/promotion_action.rb