Sha256: 140c941ea95da2eabbf7d408ba95236bcf9537563f12d4eeea554917fff00d8c

Contents?: true

Size: 940 Bytes

Versions: 2

Compression:

Stored size: 940 Bytes

Contents

module SpreeAvataxOfficial
  module Spree
    module AddressDecorator
      def self.prepended(base)
        base.around_save :recalculate_avatax_taxes
        base.const_set   'OBSERVABLE_FIELDS', %w[address1 address2 city zipcode state_id country_id].freeze
      end

      private

      def recalculate_avatax_taxes
        return yield unless SpreeAvataxOfficial::Config.enabled

        observed_fields_changed = self.class::OBSERVABLE_FIELDS & changed

        yield # around_save requires yield to perform save operation

        return unless observed_fields_changed.any? && persisted?

        address_sym = ::Spree::Config.tax_using_ship_address ? :ship_address : :bill_address
        order       = ::Spree::Order.incomplete.find_by(address_sym => self)

        return if order.blank?

        order.recalculate_avatax_taxes
      end
    end
  end
end

::Spree::Address.prepend ::SpreeAvataxOfficial::Spree::AddressDecorator

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spree_avatax_official-1.8.0 app/models/spree_avatax_official/spree/address_decorator.rb
spree_avatax_official-1.7.1 app/models/spree_avatax_official/spree/address_decorator.rb