Sha256: cd4ef91ed04660643f6eba6ff38f48dbb005f6440e2f51c8b82ea4f67c0630b1

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

FactoryBot.define do
  factory :adjustment, class: 'Spree::Adjustment' do
    order
    adjustable { order }
    amount { 100.0 }
    label { 'Shipping' }
    association(:source, factory: :tax_rate)

    after(:build) do |adjustment|
      adjustments = adjustment.adjustable.adjustments
      if adjustments.loaded? && !adjustments.include?(adjustment)
        adjustments.proxy_association.add_to_target(adjustment)
      end
    end

    factory :tax_adjustment, class: 'Spree::Adjustment' do
      order { adjustable.order }
      association(:adjustable, factory: :line_item)
      amount { 10.0 }
      label { 'VAT 5%' }

      after(:create) do |adjustment|
        # Set correct tax category, so that adjustment amount is not 0
        if adjustment.adjustable.is_a?(Spree::LineItem)
          if adjustment.adjustable.tax_category.present?
            adjustment.source.tax_categories = [adjustment.adjustable.tax_category]
          else
            adjustment.source.tax_categories = []
          end
          adjustment.source.save
          adjustment.update!(amount: adjustment.source.compute_amount(adjustment.adjustable))
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solidus_core-4.4.2 lib/spree/testing_support/factories/adjustment_factory.rb
solidus_core-4.4.1 lib/spree/testing_support/factories/adjustment_factory.rb
solidus_core-4.4.0 lib/spree/testing_support/factories/adjustment_factory.rb