Skip to content

Commit

Permalink
Refactor Promotion factory
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RyanofWoods committed Oct 21, 2022
1 parent f719ed8 commit 0b81b52
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions core/lib/spree/testing_support/factories/promotion_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]
Expand All @@ -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
Expand Down

0 comments on commit 0b81b52

Please sign in to comment.