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-4.10.1 app/models/concerns/spree/adjustment_source.rb
spree_core-4.10.0 app/models/concerns/spree/adjustment_source.rb
spree_core-4.9.0 app/models/concerns/spree/adjustment_source.rb
spree_core-4.8.3 app/models/concerns/spree/adjustment_source.rb
spree_core-4.8.2 app/models/concerns/spree/adjustment_source.rb
spree_core-4.7.3 app/models/concerns/spree/adjustment_source.rb
spree_core-4.6.6 app/models/concerns/spree/adjustment_source.rb
spree_core-4.5.5 app/models/concerns/spree/adjustment_source.rb
spree_core-4.0.9 app/models/concerns/spree/adjustment_source.rb
spree_core-4.1.15 app/models/concerns/spree/adjustment_source.rb
spree_core-4.2.7 app/models/concerns/spree/adjustment_source.rb
spree_core-4.3.3 app/models/concerns/spree/adjustment_source.rb
spree_core-4.4.1 app/models/concerns/spree/adjustment_source.rb
spree_core-4.5.4 app/models/concerns/spree/adjustment_source.rb
spree_core-4.6.5 app/models/concerns/spree/adjustment_source.rb
spree_core-4.7.2 app/models/concerns/spree/adjustment_source.rb
spree_core-4.7.1 app/models/concerns/spree/adjustment_source.rb
spree_core-4.6.4 app/models/concerns/spree/adjustment_source.rb
spree_core-4.7.0 app/models/concerns/spree/adjustment_source.rb
spree_core-4.6.3 app/models/concerns/spree/adjustment_source.rb