Sha256: 60fbc06891b8dbef241f6b1d27923f506f95c2845acf8665522fcf6ee8780cd9
Contents?: true
Size: 1.62 KB
Versions: 44
Compression:
Stored size: 1.62 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 == 0 adjustments.new(order: order, adjustable: adjustable, label: label, amount: amount, included: included).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
44 entries across 44 versions & 2 rubygems