Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Improve `Product`, `Taxon` and `User` promotion rules
Damian Legawiec508e7090be38
diff --git a/core/app/models/spree/promotion/rules/product.rb b/core/app/models/spree/promotion/rules/product.rbindex 8271dea27d..731ab4917a 100644--- a/core/app/models/spree/promotion/rules/product.rb+++ b/core/app/models/spree/promotion/rules/product.rb@@ -5,14 +5,30 @@ module Spreeclass Promotionmodule Rulesclass Product < PromotionRule+ #+ # Associations+ #has_many :product_promotion_rules, class_name: 'Spree::ProductPromotionRule',foreign_key: :promotion_rule_id,dependent: :destroyhas_many :products, through: :product_promotion_rules, class_name: 'Spree::Product'+ #+ # Preferences+ #MATCH_POLICIES = %w(any all none)preference :match_policy, :string, default: MATCH_POLICIES.first+ #+ # Attributes+ #+ attr_accessor :product_ids_to_add++ #+ # Callbacks+ #+ after_save :add_products+# scope/association that is used to test eligibilitydef eligible_productsproducts@@ -54,13 +70,30 @@ module Spreeenddef product_ids_string+ ActiveSupport::Deprecation.warn(+ 'Please use `product_ids=` instead.'+ )product_ids.join(',')enddef product_ids_string=(s)- # check this+ ActiveSupport::Deprecation.warn(+ 'Please use `product_ids=` instead.'+ )self.product_ids = send++ private++ def add_products+ return if product_ids_to_add.nil?++ product_promotion_rules.delete_all+ product_promotion_rules.upsert_all(+ product_ids_to_add.map { |product_id| { product_id: product_id, promotion_rule_id: id } },+ unique_by: [:product_id, :promotion_rule_id]+ )+ endendendenddiff --git a/core/app/models/spree/promotion/rules/taxon.rb b/core/app/models/spree/promotion/rules/taxon.rbindex b2765ec066..6b79314597 100644--- a/core/app/models/spree/promotion/rules/taxon.rb+++ b/core/app/models/spree/promotion/rules/taxon.rb@@ -2,14 +2,30 @@ module Spreeclass Promotionmodule Rulesclass Taxon < PromotionRule+ #+ # Associations+ #has_many :promotion_rule_taxons, class_name: 'Spree::PromotionRuleTaxon',foreign_key: 'promotion_rule_id',dependent: :destroyhas_many :taxons, through: :promotion_rule_taxons, class_name: 'Spree::Taxon'+ #+ # Preferences+ #MATCH_POLICIES = %w(any all)preference :match_policy, default: MATCH_POLICIES.first+ #+ # Attributes+ #+ attr_accessor :taxon_ids_to_add++ #+ # Callbacks+ #+ after_save :add_taxons+def applicable?(promotable)promotable.is_a?(Spree::Order)end@@ -39,10 +55,16 @@ module Spreeenddef taxon_ids_string+ ActiveSupport::Deprecation.warn(+ 'Please use `taxon_ids=` instead.'+ )taxons.pluck(:id).join(',')enddef taxon_ids_string=(s)+ ActiveSupport::Deprecation.warn(+ 'Please use `taxon_ids=` instead.'+ )ids = s.to_s.split(',').map(&:strip)self.taxons = Spree::Taxon.for_stores(stores).find(ids)end@@ -69,6 +91,16 @@ module Spreedef taxons_in_order_including_parents(order)order_taxons_in_taxons_and_children(order).inject([]) { |taxons, taxon| taxons << taxon.self_and_ancestors }.flatten.uniqend++ def add_taxons+ return if taxon_ids_to_add.nil?++ promotion_rule_taxons.delete_all+ promotion_rule_taxons.upsert_all(+ taxon_ids_to_add.map { |taxon_id| { taxon_id: taxon_id, promotion_rule_id: id } },+ unique_by: [:taxon_id, :promotion_rule_id]+ )+ endendendenddiff --git a/core/app/models/spree/promotion/rules/user.rb b/core/app/models/spree/promotion/rules/user.rbindex d9f4558e10..6c7193640d 100644--- a/core/app/models/spree/promotion/rules/user.rb+++ b/core/app/models/spree/promotion/rules/user.rb@@ -2,29 +2,59 @@ module Spreeclass Promotionmodule Rulesclass User < PromotionRule- belongs_to :user, class_name: "::#{Spree.user_class}"-+ #+ # Associations+ #has_many :promotion_rule_users, class_name: 'Spree::PromotionRuleUser',foreign_key: :promotion_rule_id,dependent: :destroyhas_many :users, through: :promotion_rule_users, class_name: "::#{Spree.user_class}"+ #+ # Attributes+ #+ attr_accessor :user_ids_to_add++ #+ # Callbacks+ #+ after_save :add_users+def applicable?(promotable)promotable.is_a?(Spree::Order)enddef eligible?(order, _options = {})- users.include?(order.user)+ user_ids.include?(order.user_id)enddef user_ids_string+ ActiveSupport::Deprecation.warn(+ 'Spree::Promotion::Rules::User#user_ids_string is deprecated and will be removed in Spree 6.0. ' \+ 'Please use `user_ids` instead.'+ )user_ids.join(',')enddef user_ids_string=(s)- # check this+ ActiveSupport::Deprecation.warn(+ 'Spree::Promotion::Rules::User#user_ids_string= is deprecated and will be removed in Spree 6.0. ' \+ 'Please use `user_ids=` instead.'+ )self.user_ids = send++ private++ def add_users+ return if user_ids_to_add.nil?++ promotion_rule_users.delete_all+ promotion_rule_users.upsert_all(+ user_ids_to_add.map { |user_id| { user_id: user_id, promotion_rule_id: id } },+ unique_by: [:user_id, :promotion_rule_id]+ )+ endendendenddiff --git a/core/db/migrate/20250312202705_add_unique_index_on_promotion_rules_associations.rb b/core/db/migrate/20250312202705_add_unique_index_on_promotion_rules_associations.rbnew file mode 100644index 0000000000..c8dceb2314--- /dev/null+++ b/core/db/migrate/20250312202705_add_unique_index_on_promotion_rules_associations.rb@@ -0,0 +1,37 @@+class AddUniqueIndexOnPromotionRulesAssociations < ActiveRecord::Migration[7.2]+ def change+ # Remove duplicate product promotion rules+ execute <<-SQL+ DELETE FROM spree_product_promotion_rules+ WHERE id NOT IN (+ SELECT MIN(id)+ FROM spree_product_promotion_rules+ GROUP BY product_id, promotion_rule_id+ );+ SQL++ # Remove duplicate taxon promotion rules+ execute <<-SQL+ DELETE FROM spree_promotion_rule_taxons+ WHERE id NOT IN (+ SELECT MIN(id)+ FROM spree_promotion_rule_taxons+ GROUP BY taxon_id, promotion_rule_id+ );+ SQL++ # Remove duplicate user promotion rules+ execute <<-SQL+ DELETE FROM spree_promotion_rule_users+ WHERE id NOT IN (+ SELECT MIN(id)+ FROM spree_promotion_rule_users+ GROUP BY user_id, promotion_rule_id+ );+ SQL++ add_index :spree_product_promotion_rules, [:product_id, :promotion_rule_id], unique: true+ add_index :spree_promotion_rule_taxons, [:taxon_id, :promotion_rule_id], unique: true+ add_index :spree_promotion_rule_users, [:user_id, :promotion_rule_id], unique: true+ end+enddiff --git a/core/lib/spree/testing_support/factories/promotion_rule_factory.rb b/core/lib/spree/testing_support/factories/promotion_rule_factory.rbindex 7d0315bb02..cf1e515feb 100644--- a/core/lib/spree/testing_support/factories/promotion_rule_factory.rb+++ b/core/lib/spree/testing_support/factories/promotion_rule_factory.rb@@ -2,4 +2,16 @@ FactoryBot.define dofactory :promotion_rule, class: Spree::PromotionRule doassociation :promotionend++ factory :promotion_rule_user, class: Spree::Promotion::Rules::User do+ association :promotion+ end++ factory :promotion_rule_product, class: Spree::Promotion::Rules::Product do+ association :promotion+ end++ factory :promotion_rule_taxon, class: Spree::Promotion::Rules::Taxon do+ association :promotion+ endenddiff --git a/core/spec/models/spree/promotion/rules/product_spec.rb b/core/spec/models/spree/promotion/rules/product_spec.rbindex d5827e3eef..7fab237a55 100644--- a/core/spec/models/spree/promotion/rules/product_spec.rb+++ b/core/spec/models/spree/promotion/rules/product_spec.rb@@ -4,7 +4,7 @@ describe Spree::Promotion::Rules::Product, type: :model dolet(:rule) { Spree::Promotion::Rules::Product.new(rule_options) }let(:rule_options) { {} }- context '#eligible?(order)' do+ describe '#eligible?(order)' dolet(:order) { Spree::Order.new }let!(:products) { create_list(:product, 3) }let(:product1) { products.first }@@ -149,4 +149,32 @@ describe Spree::Promotion::Rules::Product, type: :model doendendend++ describe '#add_products' do+ let(:promotion) { create(:promotion) }+ let(:rule) { create(:promotion_rule_product, promotion: promotion) }+ let(:products) { create_list(:product, 3) }++ it 'adds the products to the rule' do+ rule.product_ids_to_add = products.map(&:id)+ rule.save!+ expect(rule.products).to match_array(products)+ end++ it 'removes the products from the rule' do+ rule.product_ids_to_add = products.map(&:id)+ rule.save!+ rule.product_ids_to_add = []+ rule.save!+ expect(rule.products).to be_empty+ end++ it 'does not remove the products when nil is passed' do+ rule.product_ids_to_add = products.map(&:id)+ rule.save!+ rule.product_ids_to_add = nil+ rule.save!+ expect(rule.products).to match_array(products)+ end+ endenddiff --git a/core/spec/models/spree/promotion/rules/taxon_spec.rb b/core/spec/models/spree/promotion/rules/taxon_spec.rbindex 0339ca83a1..e215fee799 100644--- a/core/spec/models/spree/promotion/rules/taxon_spec.rb+++ b/core/spec/models/spree/promotion/rules/taxon_spec.rb@@ -5,7 +5,7 @@ describe Spree::Promotion::Rules::Taxon, type: :model dolet(:store) { Spree::Store.default }- context '#eligible?(order)' do+ describe '#eligible?(order)' dolet(:taxonomy) { create(:taxonomy, store: store) }let(:taxon) { create :taxon, name: 'first', taxonomy: taxonomy }let(:taxon2) { create :taxon, name: 'second', taxonomy: taxonomy }@@ -104,4 +104,32 @@ describe Spree::Promotion::Rules::Taxon, type: :model doendendend++ describe '#add_taxons' do+ let(:promotion) { create(:promotion) }+ let(:rule) { create(:promotion_rule_taxon, promotion: promotion) }+ let(:taxons) { create_list(:taxon, 3) }++ it 'adds the taxons to the rule' do+ rule.taxon_ids_to_add = taxons.map(&:id)+ rule.save!+ expect(rule.taxons).to match_array(taxons)+ end++ it 'removes the taxons from the rule' do+ rule.taxon_ids_to_add = taxons.map(&:id)+ rule.save!+ rule.taxon_ids_to_add = []+ rule.save!+ expect(rule.taxons).to be_empty+ end++ it 'does not remove the taxons when nil is passed' do+ rule.taxon_ids_to_add = taxons.map(&:id)+ rule.save!+ rule.taxon_ids_to_add = nil+ rule.save!+ expect(rule.taxons).to match_array(taxons)+ end+ endenddiff --git a/core/spec/models/spree/promotion/rules/user_spec.rb b/core/spec/models/spree/promotion/rules/user_spec.rbindex 23a8648bed..281a344d86 100644--- a/core/spec/models/spree/promotion/rules/user_spec.rb+++ b/core/spec/models/spree/promotion/rules/user_spec.rb@@ -6,7 +6,7 @@ describe Spree::Promotion::Rules::User, type: :model dolet(:user_placing_order) { create :user }describe '#eligible?' do- let(:order) { Spree::Order.new }+ let(:order) { build(:order, user: user_placing_order) }it 'is not eligible if users are not provided' doexpect(rule).not_to be_eligible(order)@@ -16,8 +16,7 @@ describe Spree::Promotion::Rules::User, type: :model dolet(:users) { [user_placing_order, random_user] }it 'is eligible if users include user placing the order' do- allow(rule).to receive_messages(users: users)- allow(order).to receive_messages(user: user_placing_order)+ allow(rule).to receive_messages(user_ids: users.map(&:id))expect(rule).to be_eligible(order)end@@ -27,8 +26,7 @@ describe Spree::Promotion::Rules::User, type: :model dolet(:users) { create_list(:user, 2) }it 'is not eligible if user placing the order is not listed' do- allow(rule).to receive_messages(users: users)- allow(order).to receive_messages(user: user_placing_order)+ allow(rule).to receive_messages(user_ids: users.map(&:id))expect(rule).not_to be_eligible(order)end@@ -39,4 +37,31 @@ describe Spree::Promotion::Rules::User, type: :model doexpect { rule.user_ids = "#{random_user.id}, #{user_placing_order.id}" }.not_to raise_errorendend++ describe '#add_users' do+ let(:promotion) { create(:promotion) }+ let(:rule) { create(:promotion_rule_user, promotion: promotion) }++ it 'adds users to the promotion rule' do+ rule.user_ids_to_add = [random_user.id, user_placing_order.id]+ rule.save!+ expect(rule.users).to include(random_user, user_placing_order)+ end++ it 'removes users from the promotion rule' do+ rule.user_ids_to_add = [random_user.id, user_placing_order.id]+ rule.save!+ rule.user_ids_to_add = []+ rule.save!+ expect(rule.users).to be_empty+ end++ it 'does not remove the users when nil is passed' do+ rule.user_ids_to_add = [random_user.id, user_placing_order.id]+ rule.save!+ rule.user_ids_to_add = nil+ rule.save!+ expect(rule.users).to include(random_user, user_placing_order)+ end+ endend