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

Promotion configuration #5635

Merged
merged 5 commits into from
Feb 22, 2024
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
4 changes: 2 additions & 2 deletions api/app/controllers/spree/api/coupon_codes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def create
authorize! :update, @order, order_token

@order.coupon_code = params[:coupon_code]
@handler = Spree::Config.coupon_code_handler_class.new(@order).apply
@handler = Spree::Config.promotions.coupon_code_handler_class.new(@order).apply

if @handler.successful?
render 'spree/api/promotions/handler', status: 200
Expand All @@ -24,7 +24,7 @@ def destroy
authorize! :update, @order, order_token

@order.coupon_code = params[:id]
@handler = Spree::Config.coupon_code_handler_class.new(@order).remove
@handler = Spree::Config.promotions.coupon_code_handler_class.new(@order).remove

if @handler.successful?
render 'spree/api/promotions/handler', status: 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def load_promotion

def validate_promotion_action_type
requested_type = params[:action_type]
promotion_action_types = Rails.application.config.spree.promotions.actions
promotion_action_types = Spree::Config.promotions.actions
@promotion_action_type = promotion_action_types.detect do |klass|
klass.name == requested_type
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def model_class

def validate_promotion_rule_type
requested_type = params[:promotion_rule].delete(:type)
promotion_rule_types = Rails.application.config.spree.promotions.rules
promotion_rule_types = Spree::Config.promotions.rules
@promotion_rule_type = promotion_rule_types.detect do |klass|
klass.name == requested_type
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def collection
@collection = @search.result(distinct: true).
includes(promotion_includes).
page(params[:page]).
per(params[:per_page] || Spree::Config[:promotions_per_page])
per(params[:per_page] || Spree::Config.promotions.promotions_per_page)

@collection
end
Expand Down
2 changes: 1 addition & 1 deletion backend/app/helpers/spree/promotion_rules_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Spree
module PromotionRulesHelper
def options_for_promotion_rule_types(promotion)
existing = promotion.rules.map { |rule| rule.class.name }
rules = Rails.application.config.spree.promotions.rules.reject { |rule| existing.include? rule.name }
rules = Spree::Config.promotions.rules.reject { |rule| existing.include? rule.name }
options = rules.map { |rule| [rule.model_name.human, rule.name] }
options_for_select(options)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<fieldset id="action_fields" class="no-border-top">

<%= form_tag spree.admin_promotion_promotion_actions_path(@promotion), remote: true, id: 'new_promotion_action_form' do %>
<% options = options_for_select( Rails.application.config.spree.promotions.actions.map {|action| [ action.model_name.human, action.name] } ) %>
<% options = options_for_select(Spree::Config.promotions.actions.map {|action| [ action.model_name.human, action.name] } ) %>
<fieldset>
<legend align="center"><%= t('spree.promotion_actions') %></legend>
<% if can?(:update, @promotion) %>
Expand Down
6 changes: 3 additions & 3 deletions backend/spec/features/admin/promotion_adjustments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,12 @@ def self.description
"Complex Calculator"
end
end
@calculators = Rails.application.config.spree.calculators.promotion_actions_create_item_adjustments
Rails.application.config.spree.calculators.promotion_actions_create_item_adjustments = [ComplexCalculator]
@calculators = Spree::Config.promotions.calculators['Spree::Promotion::Actions::CreateItemAdjustments']
Spree::Config.promotions.calculators['Spree::Promotion::Actions::CreateItemAdjustments'] = [ComplexCalculator]
end

after do
Rails.application.config.spree.calculators.promotion_actions_create_item_adjustments = @calculators
Spree::Config.promotions.calculators['Spree::Promotion::Actions::CreateItemAdjustments'] = @calculators
end

it "does not show array and hash form fields" do
Expand Down
4 changes: 2 additions & 2 deletions core/app/jobs/spree/promotion_code_batch_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ def perform(promotion_code_batch)
).build_promotion_codes

if promotion_code_batch.email?
Spree::Config.promotion_code_batch_mailer_class
Spree::Config.promotions.promotion_code_batch_mailer_class
.promotion_code_batch_finished(promotion_code_batch)
.deliver_now
end
rescue StandardError => error
if promotion_code_batch.email?
Spree::Config.promotion_code_batch_mailer_class
Spree::Config.promotions.promotion_code_batch_mailer_class
.promotion_code_batch_errored(promotion_code_batch)
.deliver_now
end
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def create_shipments_for_line_item(line_item)
end

def apply_shipping_promotions
Spree::Config.shipping_promotion_handler_class.new(self).activate
Spree::Config.promotions.shipping_promotion_handler_class.new(self).activate
recalculate
end

Expand Down Expand Up @@ -799,7 +799,7 @@ def ensure_inventory_units
end

def ensure_promotions_eligible
Spree::Config.promotion_adjuster_class.new(self).call
Spree::Config.promotions.promotion_adjuster_class.new(self).call

if promo_total_changed?
restart_checkout_flow
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/order_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def log_state_change(name)
end

def update_promotions
Spree::Config.promotion_adjuster_class.new(order).call
Spree::Config.promotions.promotion_adjuster_class.new(order).call
end

def update_taxes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def call
promotion_adjustments = item.adjustments.select(&:promotion?)

promotion_adjustments.each { |adjustment| recalculate(adjustment) }
Spree::Config.promotion_chooser_class.new(promotion_adjustments).update
Spree::Config.promotions.promotion_chooser_class.new(promotion_adjustments).update

item.promo_total = promotion_adjustments.select(&:eligible?).sum(&:amount)
end
Expand All @@ -30,7 +30,7 @@ def call
# line items and/or shipments.
order_promotion_adjustments = order.adjustments.select(&:promotion?)
order_promotion_adjustments.each { |adjustment| recalculate(adjustment) }
Spree::Config.promotion_chooser_class.new(order_promotion_adjustments).update
Spree::Config.promotions.promotion_chooser_class.new(order_promotion_adjustments).update

order.promo_total = all_items.sum(&:promo_total) +
order_promotion_adjustments.
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PromotionAction < Spree::Base
belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_actions, optional: true

scope :of_type, ->(type) { where(type: Array.wrap(type).map(&:to_s)) }
scope :shipping, -> { of_type(Spree::Config.environment.promotions.shipping_actions.to_a) }
scope :shipping, -> { of_type(Spree::Config.promotions.shipping_actions.to_a) }

def preload_relations
[]
Expand Down
95 changes: 33 additions & 62 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,6 @@ class AppConfiguration < Preferences::Configuration
# @return [Integer] Products to show per-page in the frontend (default: +12+)
preference :products_per_page, :integer, default: 12

# @!attribute [rw] promotions_per_page
# @return [Integer] Promotions to show per-page in the admin (default: +15+)
preference :promotions_per_page, :integer, default: 15

# @!attribute [rw] require_master_price
# @return [Boolean] Require a price on the master variant of a product (default: +true+)
preference :require_master_price, :boolean, default: true
Expand Down Expand Up @@ -324,12 +320,6 @@ def default_pricing_options
# Spree::Variant::VatPriceGenerator.
class_name_attribute :variant_vat_prices_generator_class, default: 'Spree::Variant::VatPriceGenerator'

# promotion_chooser_class allows extensions to provide their own PromotionChooser
class_name_attribute :promotion_chooser_class, default: 'Spree::PromotionChooser'

# promotion_adjuster_class allows extensions to provide their own Promotion Adjuster
class_name_attribute :promotion_adjuster_class, default: 'Spree::Promotion::OrderAdjustmentsRecalculator'

class_name_attribute :allocator_class, default: 'Spree::Stock::Allocator::OnHandFirst'

class_name_attribute :shipping_rate_sorter_class, default: 'Spree::Stock::ShippingRateSorter'
Expand Down Expand Up @@ -367,31 +357,6 @@ def default_pricing_options
# Spree::OrderUpdater.
class_name_attribute :order_recalculator_class, default: 'Spree::OrderUpdater'

# Allows providing a different coupon code handler.
# @!attribute [rw] coupon_code_handler_class
# @see Spree::PromotionHandler::Coupon
# @return [Class] an object that conforms to the API of
# the standard coupon code handler class
# Spree::PromotionHandler::Coupon.
class_name_attribute :coupon_code_handler_class, default: 'Spree::PromotionHandler::Coupon'

# Allows providing a different shipping promotion handler.
# @!attribute [rw] shipping_promotion_handler_class
# @see Spree::PromotionHandler::Shipping
# @return [Class] an object that conforms to the API of
# the standard shipping promotion handler class
# Spree::PromotionHandler::Coupon.
class_name_attribute :shipping_promotion_handler_class, default: 'Spree::PromotionHandler::Shipping'

# Allows providing your own Mailer for promotion code batch mailer.
#
# @!attribute [rw] promotion_code_batch_mailer_class
# @return [ActionMailer::Base] an object that responds to "promotion_code_batch_finished",
# and "promotion_code_batch_errored"
# (e.g. an ActionMailer with a "promotion_code_batch_finished" method) with the same
# signature as Spree::PromotionCodeBatchMailer.promotion_code_batch_finished.
class_name_attribute :promotion_code_batch_mailer_class, default: 'Spree::PromotionCodeBatchMailer'

# Allows providing your own Mailer for reimbursement mailer.
#
# @!attribute [rw] reimbursement_mailer_class
Expand Down Expand Up @@ -623,6 +588,39 @@ def promotions
@promotion_configuration ||= Spree::Core::PromotionConfiguration.new
end

class << self
private

def promotions_deprecation_message(method)
"The `Spree::Config.#{method}` preference is deprecated and will be removed in Solidus 5.0. " \
"Use `Spree::Config.promotions.#{method}` instead"
end
end

delegate :promotion_adjuster_class, :promotion_adjuster_class=, to: :promotions
deprecate promotion_adjuster_class: promotions_deprecation_message("promotion_adjuster_class"), deprecator: Spree.deprecator
deprecate "promotion_adjuster_class=": promotions_deprecation_message("promotion_adjuster_class="), deprecator: Spree.deprecator

delegate :promotion_chooser_class, :promotion_chooser_class=, to: :promotions
deprecate promotion_chooser_class: promotions_deprecation_message("promotion_chooser_class"), deprecator: Spree.deprecator
deprecate "promotion_chooser_class=": promotions_deprecation_message("promotion_chooser_class="), deprecator: Spree.deprecator

delegate :shipping_promotion_handler_class, :shipping_promotion_handler_class=, to: :promotions
deprecate shipping_promotion_handler_class: promotions_deprecation_message("shipping_promotion_handler_class"), deprecator: Spree.deprecator
deprecate "shipping_promotion_handler_class=": promotions_deprecation_message("shipping_promotion_handler_class="), deprecator: Spree.deprecator

delegate :coupon_code_handler_class, :coupon_code_handler_class=, to: :promotions
deprecate coupon_code_handler_class: promotions_deprecation_message("coupon_code_handler_class"), deprecator: Spree.deprecator
deprecate "coupon_code_handler_class=": promotions_deprecation_message("coupon_code_handler_class"), deprecator: Spree.deprecator

delegate :promotion_code_batch_mailer_class, :promotion_code_batch_mailer_class=, to: :promotions
deprecate promotion_code_batch_mailer_class: promotions_deprecation_message("promotion_code_batch_mailer_class"), deprecator: Spree.deprecator
deprecate "promotion_code_batch_mailer_class=": promotions_deprecation_message("promotion_code_batch_mailer_class="), deprecator: Spree.deprecator

delegate :preferred_promotions_per_page, :preferred_promotions_per_page=, to: :promotions
deprecate preferred_promotions_per_page: promotions_deprecation_message("preferred_promotions_per_page"), deprecator: Spree.deprecator
deprecate "preferred_promotions_per_page=": promotions_deprecation_message("preferred_promotions_per_page="), deprecator: Spree.deprecator

def roles
@roles ||= Spree::RoleConfiguration.new.tap do |roles|
roles.assign_permissions :default, ['Spree::PermissionSets::DefaultCustomer']
Expand Down Expand Up @@ -658,33 +656,6 @@ def environment
Spree::PaymentMethod::Check
]

env.promotions.rules = %w[
Spree::Promotion::Rules::ItemTotal
Spree::Promotion::Rules::Product
Spree::Promotion::Rules::User
Spree::Promotion::Rules::FirstOrder
Spree::Promotion::Rules::UserLoggedIn
Spree::Promotion::Rules::OneUsePerUser
Spree::Promotion::Rules::Taxon
Spree::Promotion::Rules::MinimumQuantity
Spree::Promotion::Rules::NthOrder
Spree::Promotion::Rules::OptionValue
Spree::Promotion::Rules::FirstRepeatPurchaseSince
Spree::Promotion::Rules::UserRole
Spree::Promotion::Rules::Store
]

env.promotions.actions = %w[
Spree::Promotion::Actions::CreateAdjustment
Spree::Promotion::Actions::CreateItemAdjustments
Spree::Promotion::Actions::CreateQuantityAdjustments
Spree::Promotion::Actions::FreeShipping
]

env.promotions.shipping_actions = %w[
Spree::Promotion::Actions::FreeShipping
]

env.stock_splitters = %w[
Spree::Stock::Splitter::ShippingCategory
Spree::Stock::Splitter::Backordered
Expand Down
5 changes: 1 addition & 4 deletions core/lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

require 'active_support/deprecation'
require 'spree/deprecated_instance_variable_proxy'
require 'spree/deprecator'
require 'acts_as_list'
require 'awesome_nested_set'
require 'cancan'
Expand All @@ -28,10 +29,6 @@
StateMachines::Machine.ignore_method_conflicts = true

module Spree
def self.deprecator
@deprecator ||= ActiveSupport::Deprecation.new('5.0', 'Solidus')
end

autoload :Deprecation, 'spree/deprecation'

mattr_accessor :user_class, default: 'Spree::LegacyUser'
Expand Down
29 changes: 25 additions & 4 deletions core/lib/spree/core/environment/promotions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,32 @@ module Spree
module Core
class Environment
class Promotions
include EnvironmentExtension
class << self
private

add_class_set :rules
add_class_set :actions
add_class_set :shipping_actions
def promotions_deprecation_message(method)
"The `Rails.application.config.spree.promotions.#{method}` preference is deprecated and will be removed in Solidus 5.0. " \
"Use `Spree::Config.promotions.#{method}` instead."
end
end

delegate :rules, :rules=, to: :promotion_config
deprecate rules: promotions_deprecation_message("rules"), deprecator: Spree.deprecator
deprecate "rules=": promotions_deprecation_message("rules="), deprecator: Spree.deprecator

delegate :actions, :actions=, to: :promotion_config
deprecate actions: promotions_deprecation_message("actions"), deprecator: Spree.deprecator
deprecate "actions=": promotions_deprecation_message("actions="), deprecator: Spree.deprecator

delegate :shipping_actions, :shipping_actions=, to: :promotion_config
deprecate shipping_actions: promotions_deprecation_message("shipping_actions"), deprecator: Spree.deprecator
deprecate "shipping_actions=": promotions_deprecation_message("shipping_actions="), deprecator: Spree.deprecator

private

def promotion_config
Spree::Config.promotions
end
end
end
end
Expand Down
Loading
Loading