Skip to content

Commit

Permalink
Add Priority to Promotion Actions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mamhoff committed Apr 11, 2022
1 parent f3182e3 commit 016178f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 6 additions & 4 deletions core/app/models/spree/promotion_action.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 016178f

Please sign in to comment.