Sha256: eafcc96c52697b8ad2c6d4bb0fe24db28639135931417cf83e7b4cbd026ad689

Contents?: true

Size: 1.07 KB

Versions: 9

Compression:

Stored size: 1.07 KB

Contents

module Spree
  module Adjustable
    class AdjustmentsUpdater
      def self.update(adjustable)
        new(adjustable).update
      end

      def initialize(adjustable)
        @adjustable = adjustable
        adjustable.reload if shipment? && adjustable.persisted?
      end

      def update
        return unless @adjustable.persisted?

        totals = {
          non_taxable_adjustment_total: 0,
          taxable_adjustment_total: 0
        }
        adjusters.each do |klass|
          klass.adjust(@adjustable, totals)
        end

        persist_totals totals
      end

      private

      def persist_totals(totals)
        attributes = totals
        attributes[:adjustment_total] = totals[:non_taxable_adjustment_total] +
          totals[:taxable_adjustment_total] +
          totals[:additional_tax_total]
        attributes[:updated_at] = Time.current
        @adjustable.update_columns(totals)
      end

      def shipment?
        @adjustable.is_a?(Shipment)
      end

      def adjusters
        Rails.application.config.spree.adjusters
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
spree_core-3.4.6 app/models/spree/adjustable/adjustments_updater.rb
spree_core-3.4.5 app/models/spree/adjustable/adjustments_updater.rb
spree_core-3.4.4 app/models/spree/adjustable/adjustments_updater.rb
spree_core-3.4.3 app/models/spree/adjustable/adjustments_updater.rb
spree_core-3.4.2 app/models/spree/adjustable/adjustments_updater.rb
spree_core-3.4.1 app/models/spree/adjustable/adjustments_updater.rb
spree_core-3.4.0 app/models/spree/adjustable/adjustments_updater.rb
spree_core-3.4.0.rc2 app/models/spree/adjustable/adjustments_updater.rb
spree_core-3.4.0.rc1 app/models/spree/adjustable/adjustments_updater.rb