Sha256: 170e1e31dab4335668303aabf1ed2bf41a7fac4ca1f45bff11f9eca48948432a

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module SolidusFriendlyPromotions
  module OrderDecorator
    def self.prepended(base)
      base.has_many :friendly_order_promotions,
        class_name: "SolidusFriendlyPromotions::OrderPromotion",
        dependent: :destroy,
        inverse_of: :order
      base.has_many :friendly_promotions, through: :friendly_order_promotions, source: :promotion
    end

    def ensure_promotions_eligible
      Spree::Config.promotion_adjuster_class.new(self).call
      if promo_total_changed?
        restart_checkout_flow
        recalculate
        errors.add(:base, I18n.t("solidus_friendly_promotions.promotion_total_changed_before_complete"))
      end
      errors.empty?
    end

    def discountable_item_total
      line_items.sum(&:discountable_amount)
    end

    def reset_current_discounts
      line_items.each(&:reset_current_discounts)
      shipments.each(&:reset_current_discounts)
    end

    # This helper method excludes line items that are managed by an order action for the benefit
    # of calculators and actions that discount normal line items. Line items that are managed by an
    # order actions handle their discounts themselves.
    def discountable_line_items
      line_items.reject(&:managed_by_order_action)
    end

    def apply_shipping_promotions
      if Spree::Config.promotion_adjuster_class <= SolidusFriendlyPromotions::FriendlyPromotionAdjuster
        recalculate
      else
        super
      end
    end

    def free_from_order_action?(line_item, _options)
      !line_item.managed_by_order_action
    end

    Spree::Order.prepend self
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solidus_friendly_promotions-1.0.0 app/decorators/models/solidus_friendly_promotions/order_decorator.rb
solidus_friendly_promotions-1.0.0.rc.3 app/decorators/models/solidus_friendly_promotions/order_decorator.rb
solidus_friendly_promotions-1.0.0.rc.2 app/decorators/models/solidus_friendly_promotions/order_decorator.rb
solidus_friendly_promotions-1.0.0.rc.1 app/decorators/models/solidus_friendly_promotions/order_decorator.rb