Sha256: 8b48734d2ba17818e15f0118f5763f8dea8da80ac2d0e0863beece2f77652eb3

Contents?: true

Size: 1.58 KB

Versions: 102

Compression:

Stored size: 1.58 KB

Contents

module Spree
  module AdjustmentSource
    extend ActiveSupport::Concern

    included do
      has_many :adjustments, as: :source
      before_destroy :deals_with_adjustments_for_deleted_source
    end

    protected

    def create_adjustment(order, adjustable, included = false)
      amount = compute_amount(adjustable)

      return if amount.zero?

      adjustments.new(
        adjustable: adjustable,
        amount: amount,
        included: included,
        label: label,
        order: order
      ).save
    end

    def create_unique_adjustment(order, adjustable)
      return if already_adjusted?(adjustable)

      create_adjustment(order, adjustable)
    end

    def create_unique_adjustments(order, adjustables)
      adjustables.where.not(id: already_adjusted_ids(order)).map do |adjustable|
        create_adjustment(order, adjustable) if !block_given? || yield(adjustable)
      end.any?
    end

    private

    def already_adjusted_ids(order)
      adjustments.where(order: order).pluck(:adjustable_id)
    end

    def already_adjusted?(adjustable)
      adjustments.where(adjustable: adjustable).exists?
    end

    def deals_with_adjustments_for_deleted_source
      # For incomplete orders, remove the adjustment completely.
      adjustments.for_incomplete_order.destroy_all

      # For complete orders, the source will be invalid.
      # Therefore we nullify the source_id, leaving the adjustment in place.
      # This would mean that the order's total is not altered at all.
      adjustments.for_complete_order.update_all(source_id: nil, updated_at: Time.current)
    end
  end
end

Version data entries

102 entries across 102 versions & 1 rubygems

Version Path
spree_core-3.7.12 app/models/concerns/spree/adjustment_source.rb
spree_core-4.2.0.rc1 app/models/concerns/spree/adjustment_source.rb
spree_core-4.0.4 app/models/concerns/spree/adjustment_source.rb
spree_core-3.7.11 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.11 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.10 app/models/concerns/spree/adjustment_source.rb
spree_core-4.2.0.beta app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.9 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.8 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.7 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.6 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.5 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.4 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.3 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.2 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.1 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.0 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.0.rc3 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.0.rc2 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.0.rc1 app/models/concerns/spree/adjustment_source.rb