Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Added `Promotion#cached_rules` to cache promotion rules (#13096)

Damian Legawiece1c7baf74ab1
diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb
index ac43c6799e..8b1036af91 100644
--- a/core/app/models/spree/promotion.rb
+++ b/core/app/models/spree/promotion.rb
@@ -188,14 +188,26 @@ module Spree
!!eligible_rules(promotable, options)
end
+ # We cache the rules to avoid multiple database queries
+ # this is useful for orders with many line items
+ #
+ # @return [Array<Spree::PromotionRule>]
+ def cached_rules
+ Rails.cache.fetch("#{cache_key_with_version}/rules") do
+ rules.to_a
+ end
+ rescue TypeError # when using null_store in test environment
+ rules.to_a
+ end
+
# eligible_rules returns an array of promotion rules where eligible? is true for the promotable
# if there are no such rules, an empty array is returned
# if the rules make this promotable ineligible, then nil is returned (i.e. this promotable is not eligible)
def eligible_rules(promotable, options = {})
# Promotions without rules are eligible by default.
- return [] if rules.to_a.none? # preloaded rules as we're going to use them anyway, so avoiding additional database queries
+ return [] if cached_rules.none?
- specific_rules = rules.select { |rule| rule.applicable?(promotable) }
+ specific_rules = cached_rules.select { |rule| rule.applicable?(promotable) }
return [] if specific_rules.none?
rule_eligibility = Hash[specific_rules.map do |rule|
diff --git a/core/app/models/spree/promotion_action.rb b/core/app/models/spree/promotion_action.rb
index f80842c0a6..16c3fb6b1b 100644
--- a/core/app/models/spree/promotion_action.rb
+++ b/core/app/models/spree/promotion_action.rb
@@ -4,7 +4,7 @@ module Spree
class PromotionAction < Spree.base_class
acts_as_paranoid
- belongs_to :promotion, class_name: 'Spree::Promotion'
+ belongs_to :promotion, class_name: 'Spree::Promotion', touch: true
validates :promotion, :type, presence: true
diff --git a/core/app/models/spree/promotion_rule.rb b/core/app/models/spree/promotion_rule.rb
index d9c1878a38..56a798e79b 100644
--- a/core/app/models/spree/promotion_rule.rb
+++ b/core/app/models/spree/promotion_rule.rb
@@ -1,7 +1,7 @@
# Base class for all promotion rules
module Spree
class PromotionRule < Spree.base_class
- belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_rules
+ belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_rules, touch: true
delegate :stores, to: :promotion
diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb
index fd048cbac4..dce33c3294 100644
--- a/core/spec/models/spree/promotion_spec.rb
+++ b/core/spec/models/spree/promotion_spec.rb
@@ -857,4 +857,10 @@ describe Spree::Promotion, type: :model do
end
end
end
+
+ describe '#cached_rules' do
+ it 'returns the rules' do
+ expect(promotion.cached_rules).to eq(promotion.rules)
+ end
+ end
end