Skip to content

Commit

Permalink
Move config for promo calculators to Spree::PromotionConfiguration
Browse files Browse the repository at this point in the history
We want promotion calculator configuration to live on the
Spree::Config.promotions.calculators object, and with the new Nested
Class Set, this can be nicely accomplished.
  • Loading branch information
mamhoff committed Feb 12, 2024
1 parent f751a40 commit 4f68a7b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 47 deletions.
21 changes: 0 additions & 21 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -636,27 +636,6 @@ def user_last_url_storer_rules

def environment
@environment ||= Spree::Core::Environment.new(self).tap do |env|
env.calculators.promotion_actions_create_adjustments = %w[
Spree::Calculator::FlatPercentItemTotal
Spree::Calculator::FlatRate
Spree::Calculator::FlexiRate
Spree::Calculator::TieredPercent
Spree::Calculator::TieredFlatRate
]

env.calculators.promotion_actions_create_item_adjustments = %w[
Spree::Calculator::DistributedAmount
Spree::Calculator::FlatRate
Spree::Calculator::FlexiRate
Spree::Calculator::PercentOnLineItem
Spree::Calculator::TieredPercent
]

env.calculators.promotion_actions_create_quantity_adjustments = %w[
Spree::Calculator::PercentOnLineItem
Spree::Calculator::FlatRate
]

env.calculators.shipping_methods = %w[
Spree::Calculator::Shipping::FlatPercentItemTotal
Spree::Calculator::Shipping::FlatRate
Expand Down
38 changes: 35 additions & 3 deletions core/lib/spree/core/environment/calculators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,41 @@ class Calculators
add_class_set :shipping_methods
add_class_set :tax_rates

add_class_set :promotion_actions_create_adjustments
add_class_set :promotion_actions_create_item_adjustments
add_class_set :promotion_actions_create_quantity_adjustments
def promotion_actions_create_adjustments
promotion_config.calculators["Spree::Promotion::Actions::CreateAdjustment"]
end
deprecate :promotion_actions_create_adjustments, deprecator: Spree.deprecator

def promotion_actions_create_adjustments=(value)
promotion_config.calculators["Spree::Promotion::Actions::CreateAdjustment"] = value
end
deprecate :promotion_actions_create_adjustments=, deprecator: Spree.deprecator

def promotion_actions_create_item_adjustments
promotion_config.calculators["Spree::Promotion::Actions::CreateItemAdjustments"]
end
deprecate :promotion_actions_create_item_adjustments, deprecator: Spree.deprecator

def promotion_actions_create_item_adjustments=(value)
promotion_config.calculators["Spree::Promotion::Actions::CreateItemAdjustments"] = value
end
deprecate :promotion_actions_create_item_adjustments=, deprecator: Spree.deprecator

def promotion_actions_create_quantity_adjustments
promotion_config.calculators["Spree::Promotion::Actions::CreateQuantityAdjustments"]
end
deprecate :promotion_actions_create_quantity_adjustments, deprecator: Spree.deprecator

def promotion_actions_create_quantity_adjustments=(value)
promotion_config.calculators["Spree::Promotion::Actions::CreateQuantityAdjustments"] = value
end
deprecate :promotion_actions_create_quantity_adjustments=, deprecator: Spree.deprecator

private

def promotion_config
Spree::Config.promotions
end
end
end
end
Expand Down
61 changes: 38 additions & 23 deletions core/spec/lib/spree/app_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
end
end

shared_examples "working preferences set" do
it "allows adding new items" do
preferences_set << DummyClass
expect(preferences_set).to include DummyClass
preferences_set.delete DummyClass
end
end

it "should be available from the environment" do
prefs.layout = "my/layout"
expect(prefs.layout).to eq "my/layout"
Expand Down Expand Up @@ -44,6 +52,36 @@
expect(prefs.shipping_promotion_handler_class).to eq Spree::PromotionHandler::Shipping
end

context "deprecated preferences" do
let(:environment) { prefs.environment }

around do |example|
Spree.deprecator.silence do
example.run
end
end

context '.calculators' do
subject(:calculators) { environment.calculators }
it { is_expected.to be_a Spree::Core::Environment::Calculators }

context '.calculators.promotion_actions_create_adjustments' do
subject(:preferences_set) { calculators.promotion_actions_create_adjustments }
it_should_behave_like "working preferences set"
end

context '.calculators.promotion_actions_create_item_adjustments' do
subject(:preferences_set) { calculators.promotion_actions_create_item_adjustments }
it_should_behave_like "working preferences set"
end

context '.calculators.promotion_actions_create_quantity_adjustments' do
subject(:preferences_set) { calculators.promotion_actions_create_quantity_adjustments }
it_should_behave_like "working preferences set"
end
end
end

it "has a getter for the pricing options class provided by the variant price selector class" do
expect(prefs.pricing_options_class).to eq Spree::Variant::PriceSelector.pricing_options_class
end
Expand Down Expand Up @@ -76,14 +114,6 @@ class DummyClass; end;
subject(:environment) { prefs.environment }
it { is_expected.to be_a Spree::Core::Environment }

shared_examples "working preferences set" do
it "allows adding new items" do
preferences_set << DummyClass
expect(preferences_set).to include DummyClass
preferences_set.delete DummyClass
end
end

context '.payment_methods' do
subject(:preferences_set) { environment.payment_methods }
it_should_behave_like "working preferences set"
Expand All @@ -107,21 +137,6 @@ class DummyClass; end;
subject(:preferences_set) { calculators.tax_rates }
it_should_behave_like "working preferences set"
end

context '.calculators.promotion_actions_create_adjustments' do
subject(:preferences_set) { calculators.promotion_actions_create_adjustments }
it_should_behave_like "working preferences set"
end

context '.calculators.promotion_actions_create_item_adjustments' do
subject(:preferences_set) { calculators.promotion_actions_create_item_adjustments }
it_should_behave_like "working preferences set"
end

context '.calculators.promotion_actions_create_quantity_adjustments' do
subject(:preferences_set) { calculators.promotion_actions_create_quantity_adjustments }
it_should_behave_like "working preferences set"
end
end

context '.promotions' do
Expand Down

0 comments on commit 4f68a7b

Please sign in to comment.