Sha256: dbcde0e4621eee7c4da485edb5c0aa597dfdc970c1a6583f79e2b01387372568
Contents?: true
Size: 1.84 KB
Versions: 15
Compression:
Stored size: 1.84 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 = nil) amount = compute_amount(adjustable) return if amount == 0 adjustments.new(order: order, adjustable: adjustable, label: label(amount), 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 adjustment_scope = adjustments.includes(:order).references(:spree_orders) # For incomplete orders, remove the adjustment completely. adjustment_scope.where("spree_orders.completed_at IS NULL").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. adjustment_scope.where("spree_orders.completed_at IS NOT NULL").each do |adjustment| adjustment.update_columns( source_id: nil, updated_at: Time.now, ) end end end end
Version data entries
15 entries across 15 versions & 1 rubygems