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 performance of Promotion Rules: Taxon, Product, User and Opti… (#13402)
Damian Legawiec9b970391aad2
diff --git a/core/app/models/spree/promotion/rules/option_value.rb b/core/app/models/spree/promotion/rules/option_value.rbindex d14d0fb708..d36bedcde8 100644--- a/core/app/models/spree/promotion/rules/option_value.rb+++ b/core/app/models/spree/promotion/rules/option_value.rb@@ -13,15 +13,24 @@ module Spreeenddef eligible?(promotable, _options = {})+ return false if eligible_option_value_variant_ids.empty?+case preferred_match_policywhen 'any'- promotable.line_items.any? { |item| actionable?(item) }+ Spree::OptionValueVariant.where(id: eligible_option_value_variant_ids, variant_id: promotable.variant_ids).exists?endenddef actionable?(line_item)- option_value_variant_ids = line_item.variant.option_value_variant_ids.map(&:to_s)- (preferred_eligible_values & option_value_variant_ids).any?+ return false if eligible_option_value_variant_ids.empty?++ Spree::OptionValueVariant.where(id: eligible_option_value_variant_ids, variant_id: line_item.variant_id).exists?+ end++ private++ def eligible_option_value_variant_ids+ @eligible_option_value_variant_ids ||= preferred_eligible_values.map(&:to_s)endendenddiff --git a/core/app/models/spree/promotion/rules/product.rb b/core/app/models/spree/promotion/rules/product.rbindex 752c2108ba..f461e63d85 100644--- a/core/app/models/spree/promotion/rules/product.rb+++ b/core/app/models/spree/promotion/rules/product.rb@@ -34,23 +34,27 @@ module Spreeproductsend+ def eligible_product_ids+ @eligible_product_ids ||= product_promotion_rules.pluck(:product_id)+ end+def applicable?(promotable)promotable.is_a?(Spree::Order)enddef eligible?(order, _options = {})- return true if eligible_products.empty?+ return true if eligible_product_ids.empty?if preferred_match_policy == 'all'- unless eligible_products.all? { |p| order.products.include?(p) }+ unless eligible_product_ids.all? { |p| order.product_ids.include?(p) }eligibility_errors.add(:base, eligibility_error_message(:missing_product))endelsif preferred_match_policy == 'any'- unless order.products.any? { |p| eligible_products.include?(p) }+ unless order.product_ids.any? { |p| eligible_product_ids.include?(p) }eligibility_errors.add(:base, eligibility_error_message(:no_applicable_products))endelse- unless order.products.none? { |p| eligible_products.include?(p) }+ unless order.product_ids.none? { |p| eligible_product_ids.include?(p) }eligibility_errors.add(:base, eligibility_error_message(:has_excluded_product))endend@@ -61,9 +65,9 @@ module Spreedef actionable?(line_item)case preferred_match_policywhen 'any', 'all'- product_ids.include? line_item.variant.product_id+ eligible_product_ids.include? line_item.variant.product_idwhen 'none'- product_ids.exclude? line_item.variant.product_id+ eligible_product_ids.exclude? line_item.variant.product_idelseraise "unexpected match policy: #{preferred_match_policy.inspect}"end@@ -89,9 +93,15 @@ module Spreereturn if product_ids_to_add.nil?product_promotion_rules.delete_all- product_promotion_rules.insert_all(- product_ids_to_add.map { |product_id| { product_id: product_id, promotion_rule_id: id } }- )++ if product_ids_to_add.any?+ Spree::ProductPromotionRule.insert_all(+ product_ids_to_add.map { |product_id| { product_id: product_id, promotion_rule_id: id } }+ )+ end++ # Clear memoized values+ @eligible_product_ids = nilendendenddiff --git a/core/app/models/spree/promotion/rules/taxon.rb b/core/app/models/spree/promotion/rules/taxon.rbindex 9627c44a3a..c07f6ede5d 100644--- a/core/app/models/spree/promotion/rules/taxon.rb+++ b/core/app/models/spree/promotion/rules/taxon.rb@@ -30,14 +30,21 @@ module Spreepromotable.is_a?(Spree::Order)end+ def eligible_taxon_ids+ @eligible_taxon_ids ||= promotion_rule_taxons.pluck(:taxon_id)+ end+def eligible?(order, _options = {})+ return true if eligible_taxon_ids.empty?+if preferred_match_policy == 'all'- unless (taxons.to_a - taxons_in_order_including_parents(order)).empty?+ order_taxon_ids_with_ancestors = taxon_ids_in_order_including_ancestors(order)+ unless eligible_taxon_ids.all? { |id| order_taxon_ids_with_ancestors.include?(id) }eligibility_errors.add(:base, eligibility_error_message(:missing_taxon))endelse- order_taxons = taxons_in_order_including_parents(order)- unless taxons.any? { |taxon| order_taxons.include? taxon }+ order_taxon_ids_with_ancestors = taxon_ids_in_order_including_ancestors(order)+ unless eligible_taxon_ids.any? { |id| order_taxon_ids_with_ancestors.include?(id) }eligibility_errors.add(:base, eligibility_error_message(:no_matching_taxons))endend@@ -46,12 +53,7 @@ module Spreeenddef actionable?(line_item)- store = line_item.order.store-- store.products.- joins(:classifications).- where(Spree::Classification.table_name => { taxon_id: taxon_ids, product_id: line_item.product_id }).- exists?+ Spree::Classification.where(taxon_id: eligible_taxon_ids_including_children, product_id: line_item.product_id).exists?enddef taxon_ids_string@@ -71,34 +73,40 @@ module Spreeprivate- # All taxons in an order- def order_taxons(order)- taxon_ids = Spree::Classification.where(product_id: order.product_ids).pluck(:taxon_id).uniq+ # IDs of taxons in rule including all their children+ def eligible_taxon_ids_including_children+ @eligible_taxon_ids_including_children ||= begin+ return [] if eligible_taxon_ids.empty?- order.store.taxons.where(id: taxon_ids)+ Spree::Taxon.where(id: eligible_taxon_ids).flat_map(&:cached_self_and_descendants_ids).uniq+ endend- # ids of taxons rules and taxons rules children- def taxons_including_children_ids- taxons.inject([]) { |ids, taxon| ids += taxon.self_and_descendants.ids }- end+ # IDs of taxons in order that match rule taxons (or their children), plus all ancestors+ def taxon_ids_in_order_including_ancestors(order)+ # Get taxon IDs from order products that are within rule taxons or their children+ order_taxon_ids = Spree::Classification.where(product_id: order.product_ids, taxon_id: eligible_taxon_ids_including_children).pluck(:taxon_id).uniq- # taxons order vs taxons rules and taxons rules children- def order_taxons_in_taxons_and_children(order)- order_taxons(order).where(id: taxons_including_children_ids)- end+ return [] if order_taxon_ids.empty?- def taxons_in_order_including_parents(order)- order_taxons_in_taxons_and_children(order).inject([]) { |taxons, taxon| taxons << taxon.self_and_ancestors }.flatten.uniq+ # Get those taxons plus all their ancestors+ Spree::Taxon.where(id: order_taxon_ids).flat_map { |taxon| taxon.self_and_ancestors.ids }.uniqenddef add_taxonsreturn if taxon_ids_to_add.nil?promotion_rule_taxons.delete_all- promotion_rule_taxons.insert_all(- taxon_ids_to_add.map { |taxon_id| { taxon_id: taxon_id, promotion_rule_id: id } }- )++ if taxon_ids_to_add.any?+ Spree::PromotionRuleTaxon.insert_all(+ taxon_ids_to_add.map { |taxon_id| { taxon_id: taxon_id, promotion_rule_id: id } }+ )+ end++ # Clear memoized values+ @eligible_taxon_ids = nil+ @eligible_taxon_ids_including_children = nilendendenddiff --git a/core/app/models/spree/promotion/rules/user.rb b/core/app/models/spree/promotion/rules/user.rbindex bc72099441..ba71bba311 100644--- a/core/app/models/spree/promotion/rules/user.rb+++ b/core/app/models/spree/promotion/rules/user.rb@@ -24,8 +24,12 @@ module Spreepromotable.is_a?(Spree::Order)end+ def eligible_user_ids+ @eligible_user_ids ||= promotion_rule_users.pluck(:user_id)+ end+def eligible?(order, _options = {})- user_ids.include?(order.user_id)+ eligible_user_ids.include?(order.user_id)enddef user_ids_string@@ -50,9 +54,15 @@ module Spreereturn if user_ids_to_add.nil?promotion_rule_users.delete_all- promotion_rule_users.insert_all(- user_ids_to_add.map { |user_id| { user_id: user_id, promotion_rule_id: id } }- )++ if user_ids_to_add.any?+ Spree::PromotionRuleUser.insert_all(+ user_ids_to_add.map { |user_id| { user_id: user_id, promotion_rule_id: id } }+ )+ end++ # Clear memoized values+ @eligible_user_ids = nilendendenddiff --git a/core/spec/models/spree/promotion/rules/product_spec.rb b/core/spec/models/spree/promotion/rules/product_spec.rbindex 7fab237a55..2696939609 100644--- a/core/spec/models/spree/promotion/rules/product_spec.rb+++ b/core/spec/models/spree/promotion/rules/product_spec.rb@@ -12,7 +12,7 @@ describe Spree::Promotion::Rules::Product, type: :model dolet(:product3) { products.third }it 'is eligible if there are no products' do- allow(rule).to receive_messages(eligible_products: [])+ allow(rule).to receive_messages(eligible_product_ids: [])expect(rule).to be_eligible(order)end@@ -20,15 +20,15 @@ describe Spree::Promotion::Rules::Product, type: :model dolet(:rule_options) { super().merge(preferred_match_policy: 'any') }it 'is eligible if any of the products is in eligible products' do- allow(order).to receive_messages(products: [product1, product2])- allow(rule).to receive_messages(eligible_products: [product2, product3])+ allow(order).to receive_messages(product_ids: [product1.id, product2.id])+ allow(rule).to receive_messages(eligible_product_ids: [product2.id, product3.id])expect(rule).to be_eligible(order)endcontext 'when none of the products are eligible products' dobefore do- allow(order).to receive_messages(products: [product1])- allow(rule).to receive_messages(eligible_products: [product2, product3])+ allow(order).to receive_messages(product_ids: [product1.id])+ allow(rule).to receive_messages(eligible_product_ids: [product2.id, product3.id])endit { expect(rule).not_to be_eligible(order) }@@ -44,15 +44,15 @@ describe Spree::Promotion::Rules::Product, type: :model dolet(:rule_options) { super().merge(preferred_match_policy: 'all') }it 'is eligible if all of the eligible products are ordered' do- allow(order).to receive_messages(products: [product3, product2, product1])- allow(rule).to receive_messages(eligible_products: [product2, product3])+ allow(order).to receive_messages(product_ids: [product3.id, product2.id, product1.id])+ allow(rule).to receive_messages(eligible_product_ids: [product2.id, product3.id])expect(rule).to be_eligible(order)endcontext 'when any of the eligible products is not ordered' dobefore do- allow(order).to receive_messages(products: [product1, product2])- allow(rule).to receive_messages(eligible_products: [product1, product2, product3])+ allow(order).to receive_messages(product_ids: [product1.id, product2.id])+ allow(rule).to receive_messages(eligible_product_ids: [product1.id, product2.id, product3.id])endit { expect(rule).not_to be_eligible(order) }@@ -68,15 +68,15 @@ describe Spree::Promotion::Rules::Product, type: :model dolet(:rule_options) { super().merge(preferred_match_policy: 'none') }it "is eligible if none of the order's products are in eligible products" do- allow(order).to receive_messages(products: [product1])- allow(rule).to receive_messages(eligible_products: [product2, product3])+ allow(order).to receive_messages(product_ids: [product1.id])+ allow(rule).to receive_messages(eligible_product_ids: [product2.id, product3.id])expect(rule).to be_eligible(order)endcontext "when any of the order's products are in eligible products" dobefore do- allow(order).to receive_messages(products: [product1, product2])- allow(rule).to receive_messages(eligible_products: [product2, product3])+ allow(order).to receive_messages(product_ids: [product1.id, product2.id])+ allow(rule).to receive_messages(eligible_product_ids: [product2.id, product3.id])endit { expect(rule).not_to be_eligible(order) }@@ -94,12 +94,14 @@ describe Spree::Promotion::Rules::Product, type: :model dorule.actionable?(line_item)end- let(:rule_line_item) { Spree::LineItem.new(product: rule_product) }- let(:other_line_item) { Spree::LineItem.new(product: other_product) }-- let(:rule_options) { super().merge(products: [rule_product]) }let(:rule_product) { create(:product) }let(:other_product) { create(:product) }+ let(:rule_line_item) { build(:line_item, variant: rule_product.master) }+ let(:other_line_item) { build(:line_item, variant: other_product.master) }++ before do+ allow(rule).to receive(:eligible_product_ids).and_return([rule_product.id])+ endcontext "with 'any' match policy" dolet(:rule_options) { super().merge(preferred_match_policy: 'any') }@@ -158,7 +160,7 @@ describe Spree::Promotion::Rules::Product, type: :model doit 'adds the products to the rule' dorule.product_ids_to_add = products.map(&:id)rule.save!- expect(rule.products).to match_array(products)+ expect(rule.reload.eligible_product_ids).to match_array(products.map(&:id))endit 'removes the products from the rule' do@@ -166,7 +168,7 @@ describe Spree::Promotion::Rules::Product, type: :model dorule.save!rule.product_ids_to_add = []rule.save!- expect(rule.products).to be_empty+ expect(rule.reload.eligible_product_ids).to be_emptyendit 'does not remove the products when nil is passed' do@@ -174,7 +176,7 @@ describe Spree::Promotion::Rules::Product, type: :model dorule.save!rule.product_ids_to_add = nilrule.save!- expect(rule.products).to match_array(products)+ expect(rule.reload.eligible_product_ids).to match_array(products.map(&:id))endendenddiff --git a/core/spec/models/spree/promotion/rules/taxon_spec.rb b/core/spec/models/spree/promotion/rules/taxon_spec.rbindex 5f5f846d85..a148fc3f74 100644--- a/core/spec/models/spree/promotion/rules/taxon_spec.rb+++ b/core/spec/models/spree/promotion/rules/taxon_spec.rb@@ -1,9 +1,9 @@require 'spec_helper'describe Spree::Promotion::Rules::Taxon, type: :model do- let(:rule) { subject }-let(:store) { @default_store }+ let(:promotion) { create(:promotion) }+ let(:rule) { create(:promotion_rule_taxon, promotion: promotion) }describe '#eligible?(order)' dolet(:taxonomy) { create(:taxonomy, store: store) }@@ -11,10 +11,6 @@ describe Spree::Promotion::Rules::Taxon, type: :model dolet(:taxon2) { create :taxon, name: 'second', taxonomy: taxonomy }let(:order) { create :order_with_line_items, store: store }- before do- rule.save- end-context 'with any match policy' dobefore dorule.preferred_match_policy = 'any'diff --git a/core/spec/models/spree/promotion/rules/user_spec.rb b/core/spec/models/spree/promotion/rules/user_spec.rbindex 281a344d86..ec9ca97b66 100644--- a/core/spec/models/spree/promotion/rules/user_spec.rb+++ b/core/spec/models/spree/promotion/rules/user_spec.rb@@ -16,7 +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(user_ids: users.map(&:id))+ allow(rule).to receive_messages(eligible_user_ids: users.map(&:id))expect(rule).to be_eligible(order)end@@ -26,7 +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(user_ids: users.map(&:id))+ allow(rule).to receive_messages(eligible_user_ids: users.map(&:id))expect(rule).not_to be_eligible(order)end