Skip to content

Commit

Permalink
Deprecate Spree::Promotion::MATCH_POLICIES without replacement
Browse files Browse the repository at this point in the history
This constant won't be needed anymore in 4.0 when we will remove
the any matching policy for promotion rules.

I tried to use a more simple ActiveSupport::Deprecation::DeprecatedObjectProxy
like:

    MATCH_POLICIES = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(
      %w(all any),
      "Spree::Promotion::MATCH_POLICIES is deprecated, please do not use it anymore.",
      Spree::Deprecation
    )

But it prints the deprecation warning twice for some reason. The
approach used in this commit will load the file that contains the
constant only when it's called, which will also print the warning.
  • Loading branch information
kennyadsl committed Mar 27, 2023
1 parent 3f20583 commit cd9c7b8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/app/models/spree/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

module Spree
class Promotion < Spree::Base
MATCH_POLICIES = %w(all any)

autoload(:MATCH_POLICIES, "spree/promotion/match_policies")

UNACTIVATABLE_ORDER_STATES = ["complete", "awaiting_return", "returned"]

Expand Down
2 changes: 2 additions & 0 deletions core/lib/spree/promotion/match_policies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Spree::Promotion::MATCH_POLICIES = %w(all any)
Spree::Deprecation.warn('Spree::Promotion::MATCH_POLICIES is deprecated')
7 changes: 7 additions & 0 deletions core/spec/models/spree/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -993,4 +993,11 @@
expect(order.adjustment_total).to eq(-10)
end
end

describe "MATCH_POLICIES" do
it "prints a deprecation warning when used" do
expect(Spree::Deprecation).to receive(:warn).once.with(/Spree::Promotion::MATCH_POLICIES is deprecated/)
expect(Spree::Promotion::MATCH_POLICIES).to eq %w(all any)
end
end
end

0 comments on commit cd9c7b8

Please sign in to comment.