Skip to content

Commit

Permalink
Use new NestedClassSet for configuring calculators
Browse files Browse the repository at this point in the history
We have several actions, and several calculators. Not all calculators
work for all actions (e.g. the DistributedAmountCalculator really only
works for the AdjustLineItem action, and the `Tiered` ones don't work
for `AdjustLineItemQuantityGroups`. Rather than creating a new
configuration option for each calculator, let's use something like a
Hash here.
mamhoff committed Nov 6, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 15db33f commit 1d2c4b6
Showing 8 changed files with 52 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -7,10 +7,6 @@ def can_discount?(object)
object.is_a? Spree::LineItem
end

def available_calculators
SolidusFriendlyPromotions.config.line_item_discount_calculators
end

def level
:line_item
end
Original file line number Diff line number Diff line change
@@ -7,10 +7,6 @@ def can_discount?(object)
object.is_a?(Spree::Shipment) || object.is_a?(Spree::ShippingRate)
end

def available_calculators
SolidusFriendlyPromotions.config.shipment_discount_calculators
end

def level
:shipment
end
2 changes: 1 addition & 1 deletion app/models/solidus_friendly_promotions/promotion_action.rb
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ def relevant_rules
end

def available_calculators
raise NotImplementedError
SolidusFriendlyPromotions.config.promotion_calculators[self.class] || (raise NotImplementedError)
end
end
end
Original file line number Diff line number Diff line change
@@ -63,21 +63,27 @@
# How many promotions should be displayed on the index page in the admin.
config.promotions_per_page = 25

config.shipment_discount_calculators = [
"SolidusFriendlyPromotions::Calculators::FlatRate",
"SolidusFriendlyPromotions::Calculators::FlexiRate",
"SolidusFriendlyPromotions::Calculators::Percent",
"SolidusFriendlyPromotions::Calculators::TieredFlatRate",
"SolidusFriendlyPromotions::Calculators::TieredPercent"
]
config.line_item_discount_calculators = [
"SolidusFriendlyPromotions::Calculators::DistributedAmount",
"SolidusFriendlyPromotions::Calculators::FlatRate",
"SolidusFriendlyPromotions::Calculators::FlexiRate",
"SolidusFriendlyPromotions::Calculators::Percent",
"SolidusFriendlyPromotions::Calculators::TieredFlatRate",
"SolidusFriendlyPromotions::Calculators::TieredPercent"
]
config.promotion_calculators = SolidusFriendlyPromotions::NestedClassSet.new(
"SolidusFriendlyPromotions::Actions::AdjustShipment" => [
"SolidusFriendlyPromotions::Calculators::FlatRate",
"SolidusFriendlyPromotions::Calculators::FlexiRate",
"SolidusFriendlyPromotions::Calculators::Percent",
"SolidusFriendlyPromotions::Calculators::TieredFlatRate",
"SolidusFriendlyPromotions::Calculators::TieredPercent"
],
"SolidusFriendlyPromotions::Actions::AdjustLineItem" => [
"SolidusFriendlyPromotions::Calculators::DistributedAmount",
"SolidusFriendlyPromotions::Calculators::FlatRate",
"SolidusFriendlyPromotions::Calculators::FlexiRate",
"SolidusFriendlyPromotions::Calculators::Percent",
"SolidusFriendlyPromotions::Calculators::TieredFlatRate",
"SolidusFriendlyPromotions::Calculators::TieredPercent"
],
"SolidusFriendlyPromotions::Actions::AdjustLineItemQuantityGroups" => [
"SolidusFriendlyPromotions::Calculators::FlatRate",
"SolidusFriendlyPromotions::Calculators::Percent"
]
)

config.order_rules = [
"SolidusFriendlyPromotions::Rules::FirstOrder",
1 change: 1 addition & 0 deletions lib/solidus_friendly_promotions.rb
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
require "turbo-rails"
require "importmap-rails"
require "stimulus-rails"
require "solidus_friendly_promotions/nested_class_set"
require "solidus_friendly_promotions/configuration"
require "solidus_friendly_promotions/version"
require "solidus_friendly_promotions/engine"
2 changes: 2 additions & 0 deletions lib/solidus_friendly_promotions/configuration.rb
Original file line number Diff line number Diff line change
@@ -5,9 +5,11 @@
module SolidusFriendlyPromotions
class Configuration < Spree::Preferences::Configuration
attr_accessor :sync_order_promotions
attr_accessor :promotion_calculators

def initialize
@sync_order_promotions = true
@promotion_calculators = NestedClassSet.new
end

include Spree::Core::EnvironmentExtension
24 changes: 24 additions & 0 deletions lib/solidus_friendly_promotions/nested_class_set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "spree/core/class_constantizer"

module SolidusFriendlyPromotions
class NestedClassSet
attr_reader :klass_sets

def initialize(hash = {})
@klass_sets = hash.map do |key, value|
[
key,
Spree::Core::ClassConstantizer::Set.new.tap do |set|
value.each { |klass_name| set << klass_name }
end
]
end.to_h
end

def [](klass)
klass_sets[klass.name]
end
end
end
12 changes: 3 additions & 9 deletions spec/lib/solidus_friendly_promotions/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -19,16 +19,10 @@
end
end

describe ".shipment_discount_calculators" do
subject { config.shipment_discount_calculators }
describe ".promotion_calculators" do
subject { config.promotion_calculators }

it { is_expected.to be_a(Spree::Core::ClassConstantizer::Set) }
end

describe ".line_item_discount_calculators" do
subject { config.line_item_discount_calculators }

it { is_expected.to be_a(Spree::Core::ClassConstantizer::Set) }
it { is_expected.to be_a(SolidusFriendlyPromotions::NestedClassSet) }
end

describe ".order_rules" do

0 comments on commit 1d2c4b6

Please sign in to comment.