Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new NestedClassSet for configuring calculators #77

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/solidus_friendly_promotions/promotion_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions lib/solidus_friendly_promotions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions lib/solidus_friendly_promotions/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
Expand Up @@ -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
Expand Down