Skip to content

Commit

Permalink
Merge pull request solidusio#5658 from mamhoff/legacy-promos-prep/cla…
Browse files Browse the repository at this point in the history
…ss-set-extensions

Nested Class Set extension, Promotion configuration object
  • Loading branch information
kennyadsl authored Feb 16, 2024
2 parents 9d9c031 + 0b70bfc commit 395d909
Show file tree
Hide file tree
Showing 23 changed files with 460 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Spree::Admin::PromotionActionsController < Spree::Admin::BaseController
before_action :validate_promotion_action_type, only: :create

def create
@calculators = Rails.application.config.spree.calculators.promotion_actions_create_adjustments
@promotion_action = @promotion_action_type.new(params[:promotion_action])
@calculators = @promotion_action.available_calculators
@promotion_action.promotion = @promotion
if @promotion_action.save
flash[:success] = t('spree.successfully_created', resource: t('spree.promotion_action'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def location_after_save
end

def load_data
@calculators = Rails.application.config.spree.calculators.promotion_actions_create_adjustments
@promotion_categories = Spree::PromotionCategory.order(:name)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<%= render partial: 'spree/admin/promotions/actions/promotion_calculators_with_custom_fields',
locals: { calculators: @calculators, promotion_action: promotion_action, param_prefix: param_prefix } %>
locals: { calculators: promotion_action.available_calculators, promotion_action: promotion_action, param_prefix: param_prefix } %>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= render(
"spree/admin/promotions/actions/promotion_calculators_with_custom_fields",
calculators: Rails.application.config.spree.calculators.promotion_actions_create_item_adjustments,
calculators: promotion_action.available_calculators,
promotion_action: promotion_action,
param_prefix: param_prefix
) %>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
</div>

<%= render "spree/admin/promotions/actions/promotion_calculators_with_custom_fields",
calculators: Rails.application.config.spree.calculators.promotion_actions_create_quantity_adjustments,
calculators: promotion_action.available_calculators,
promotion_action: promotion_action,
param_prefix: param_prefix %>
4 changes: 4 additions & 0 deletions core/app/models/spree/promotion_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,9 @@ def remove_from(_order)
def to_partial_path
"spree/admin/promotions/actions/#{model_name.element}"
end

def available_calculators
Spree::Config.promotions.calculators[self.class]
end
end
end
25 changes: 4 additions & 21 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,10 @@ def stock
@stock_configuration ||= Spree::Core::StockConfiguration.new
end

def promotions
@promotion_configuration ||= Spree::Core::PromotionConfiguration.new
end

def roles
@roles ||= Spree::RoleConfiguration.new.tap do |roles|
roles.assign_permissions :default, ['Spree::PermissionSets::DefaultCustomer']
Expand All @@ -634,27 +638,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
1 change: 1 addition & 0 deletions core/lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class GatewayError < RuntimeError; end
require 'spree/core/role_configuration'
require 'spree/core/state_machines'
require 'spree/core/stock_configuration'
require 'spree/core/promotion_configuration'
require 'spree/core/validators/email'
require 'spree/permission_sets'
require 'spree/user_class_handle'
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/core/class_constantizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module ClassConstantizer
class Set
include Enumerable

def initialize
@collection = ::Set.new
def initialize(default: [])
@collection = ::Set.new(default)
end

def <<(klass)
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
18 changes: 16 additions & 2 deletions core/lib/spree/core/environment_extension.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# frozen_string_literal: true

require 'spree/core/class_constantizer'
require 'spree/core/nested_class_set'

module Spree
module Core
module EnvironmentExtension
extend ActiveSupport::Concern

class_methods do
def add_class_set(name)
def add_class_set(name, default: [])
define_method(name) do
set = instance_variable_get("@#{name}")
set ||= send("#{name}=", [])
set ||= send("#{name}=", default)
set
end

Expand All @@ -21,6 +22,19 @@ def add_class_set(name)
instance_variable_set("@#{name}", set)
end
end

def add_nested_class_set(name, default: {})
define_method(name) do
set = instance_variable_get(:"@#{name}")
set ||= send(:"#{name}=", default)
set
end

define_method(:"#{name}=") do |hash|
set = Spree::Core::NestedClassSet.new(hash)
instance_variable_set(:"@#{name}", set)
end
end
end
end
end
Expand Down
28 changes: 28 additions & 0 deletions core/lib/spree/core/nested_class_set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "spree/core/class_constantizer"

module Spree
module Core
class NestedClassSet
attr_reader :klass_sets

def initialize(hash = {})
@klass_sets = hash.map do |key, value|
[
key.to_s,
ClassConstantizer::Set.new(default: value)
]
end.to_h
end

def [](klass)
klass_sets[klass.to_s] || []
end

def []=(klass, klasses)
klass_sets[klass.to_s] = ClassConstantizer::Set.new(default: klasses.map(&:to_s))
end
end
end
end
30 changes: 30 additions & 0 deletions core/lib/spree/core/promotion_configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Spree
module Core
class PromotionConfiguration
include Core::EnvironmentExtension

add_nested_class_set :calculators, default: {
"Spree::Promotion::Actions::CreateAdjustment" => %w[
Spree::Calculator::FlatPercentItemTotal
Spree::Calculator::FlatRate
Spree::Calculator::FlexiRate
Spree::Calculator::TieredPercent
Spree::Calculator::TieredFlatRate
],
"Spree::Promotion::Actions::CreateItemAdjustments" => %w[
Spree::Calculator::DistributedAmount
Spree::Calculator::FlatRate
Spree::Calculator::FlexiRate
Spree::Calculator::PercentOnLineItem
Spree::Calculator::TieredPercent
],
"Spree::Promotion::Actions::CreateQuantityAdjustments" => %w[
Spree::Calculator::PercentOnLineItem
Spree::Calculator::FlatRate
]
}
end
end
end
66 changes: 43 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 All @@ -53,6 +91,11 @@
it { is_expected.to be_a Spree::Core::StockConfiguration }
end

describe '#promotions' do
subject { prefs.promotions }
it { is_expected.to be_a Spree::Core::PromotionConfiguration }
end

describe '@default_country_iso_code' do
it 'is the USA by default' do
expect(prefs[:default_country_iso]).to eq("US")
Expand All @@ -71,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 @@ -102,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
8 changes: 8 additions & 0 deletions core/spec/lib/spree/core/class_constantizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ def self.reload
end
end

describe "initializing with a default" do
let(:set) { described_class.new(default: ['ClassConstantizerTest::ClassA']) }

it "contains the default" do
expect(set).to include(ClassConstantizerTest::ClassA)
end
end

describe "<<" do
it "can add by string" do
set << "ClassConstantizerTest::ClassA"
Expand Down
Loading

0 comments on commit 395d909

Please sign in to comment.