From 016178f11f7dc8e668b41e9a6daf8cac787c38e0 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Tue, 29 Mar 2022 12:45:03 +0200 Subject: [PATCH] Add Priority to Promotion Actions Similar to the scenario of multiple promotions, we need to account for multiple actions on the same promotion. Also here we need to sort by explicit priority when allowing cumulative promotions. --- core/app/models/spree/promotion_action.rb | 10 ++++++---- ...2_add_priority_to_spree_promotion_actions.rb | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 core/db/migrate/20220329104242_add_priority_to_spree_promotion_actions.rb diff --git a/core/app/models/spree/promotion_action.rb b/core/app/models/spree/promotion_action.rb index b09b1672a04..b65730ec7ad 100644 --- a/core/app/models/spree/promotion_action.rb +++ b/core/app/models/spree/promotion_action.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spree/preferences/persistable' +require "spree/preferences/persistable" module Spree # Base class for all types of promotion action. @@ -11,7 +11,9 @@ class PromotionAction < Spree::Base include Spree::Preferences::Persistable include Spree::SoftDeletable - belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_actions, optional: true + acts_as_list column: :priority, scope: :promotion_id + + belongs_to :promotion, class_name: "Spree::Promotion", inverse_of: :promotion_actions, optional: true has_many :line_item_discounts, class_name: "Spree::LineItemDiscount", inverse_of: :promotion_action has_many :shipment_discounts, class_name: "Spree::ShipmentDiscount", inverse_of: :promotion_action @@ -26,7 +28,7 @@ class PromotionAction < Spree::Base # # @note This method should be overriden in subclassses. def perform(_options = {}) - raise 'perform should be implemented in a sub-class of PromotionAction' + raise "perform should be implemented in a sub-class of PromotionAction" end # Removes the action from an order @@ -36,7 +38,7 @@ def perform(_options = {}) # @param order [Spree::Order] the order to remove the action from # @return [void] def remove_from(_order) - raise 'remove_from should be implemented in a sub-class of PromotionAction' + raise "remove_from should be implemented in a sub-class of PromotionAction" end def to_partial_path diff --git a/core/db/migrate/20220329104242_add_priority_to_spree_promotion_actions.rb b/core/db/migrate/20220329104242_add_priority_to_spree_promotion_actions.rb new file mode 100644 index 00000000000..78c6a1af3fa --- /dev/null +++ b/core/db/migrate/20220329104242_add_priority_to_spree_promotion_actions.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class AddPriorityToSpreePromotionActions < ActiveRecord::Migration[5.2] + def up + add_column :spree_promotion_actions, :priority, :integer + sql = <<~SQL + UPDATE spree_promotion_actions + SET priority = id + SQL + execute(sql) + change_column :spree_promotion_actions, :priority, :integer, null: false + end + + def down + remove_column :spree_promotion_actions, :priority, :integer, null: false + end +end