From 0b81b52e428b1da3413783f8c4b44a765179449e Mon Sep 17 00:00:00 2001 From: Ryan Woods Date: Thu, 20 Oct 2022 16:55:11 +0200 Subject: [PATCH] Refactor Promotion factory Noteably, keep existing transient arguments to not cause breaking specs for stores. Also, by using the new with_adjustable_action trait, they automatically become more flexible due to its transient arguments. --- .../factories/promotion_factory.rb | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/core/lib/spree/testing_support/factories/promotion_factory.rb b/core/lib/spree/testing_support/factories/promotion_factory.rb index 14553106ae5..8ca243af8ce 100644 --- a/core/lib/spree/testing_support/factories/promotion_factory.rb +++ b/core/lib/spree/testing_support/factories/promotion_factory.rb @@ -22,8 +22,12 @@ end trait :with_action do - after(:create) do |promotion, _evaluator| - promotion.actions << Spree::Promotion::Actions::CreateAdjustment.new + transient do + promotion_action_class { Spree::Promotion::Actions::CreateAdjustment } + end + + after(:create) do |promotion, evaluator| + promotion.actions << evaluator.promotion_action_class.new end end @@ -48,11 +52,8 @@ adjustment_rate { 10 } end - after(:create) do |promotion, evaluator| - calculator = Spree::Calculator::FlatRate.new - calculator.preferred_amount = evaluator.adjustment_rate - Spree::Promotion::Actions::CreateItemAdjustments.create!(calculator: calculator, promotion: promotion) - end + with_adjustable_action + preferred_amount { adjustment_rate } end factory :promotion_with_item_adjustment, traits: [:with_line_item_adjustment] @@ -68,14 +69,11 @@ weighted_order_adjustment_amount { 10 } end - after(:create) do |promotion, evaluator| - calculator = Spree::Calculator::FlatRate.new - calculator.preferred_amount = evaluator.weighted_order_adjustment_amount - action = Spree::Promotion::Actions::CreateAdjustment.create!(calculator: calculator) - promotion.actions << action - promotion.save! - end + with_adjustable_action + preferred_amount { weighted_order_adjustment_amount } + promotion_action_class { Spree::Promotion::Actions::CreateAdjustment } end + factory :promotion_with_order_adjustment, traits: [:with_order_adjustment] trait :with_item_total_rule do