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.

Remove model level caching (#13677)

Damian Legawiec6c9da0984300
diff --git a/spree/api/app/controllers/spree/api/v3/store/products_controller.rb b/spree/api/app/controllers/spree/api/v3/store/products_controller.rb
index da8f9b23c8..7d66be7022 100644
--- a/spree/api/app/controllers/spree/api/v3/store/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/products_controller.rb
@@ -38,8 +38,8 @@ module Spree
def scope_includes
[
thumbnail: [attachment_attachment: :blob],
- master: [:prices],
- variants: [:prices]
+ master: [:prices, stock_items: :stock_location],
+ variants: [:prices, stock_items: :stock_location]
]
end
diff --git a/spree/core/app/models/concerns/spree/adjustment_source.rb b/spree/core/app/models/concerns/spree/adjustment_source.rb
index 45fa329d45..1bde610b10 100644
--- a/spree/core/app/models/concerns/spree/adjustment_source.rb
+++ b/spree/core/app/models/concerns/spree/adjustment_source.rb
@@ -5,7 +5,6 @@ module Spree
included do
has_many :adjustments, as: :source
before_destroy :deals_with_adjustments_for_deleted_source
- after_commit :clear_adjustment_source_cache
end
protected
@@ -55,12 +54,5 @@ module Spree
# This would mean that the order's total is not altered at all.
adjustments.for_complete_order.update_all(source_id: nil, updated_at: Time.current)
end
-
- def clear_adjustment_source_cache
- # Use base_class.name to match the polymorphic_name stored in source_type
- # For STI models like PromotionAction subclasses, this ensures cache key consistency
- cache_class_name = self.class.polymorphic_name
- Rails.cache.delete("spree/adjustment_source/#{cache_class_name}/#{id}")
- end
end
end
diff --git a/spree/core/app/models/concerns/spree/stores/markets.rb b/spree/core/app/models/concerns/spree/stores/markets.rb
index 996135ca48..dd83ccf708 100644
--- a/spree/core/app/models/concerns/spree/stores/markets.rb
+++ b/spree/core/app/models/concerns/spree/stores/markets.rb
@@ -66,15 +66,15 @@ module Spree
# @return [ActiveRecord::Relation<Spree::Country>]
def countries_from_markets
Spree::Country
- .where(id: Spree::Country.joins(market_countries: :market).where(Spree::Market.table_name => { store_id: id, deleted_at: nil }).select(:id))
- .order(:name)
+ .where(id: Spree::Country.joins(market_countries: :market).where(Spree::Market.table_name => { store_id: id, deleted_at: nil }).select(:id))
+ .order(:name)
end
# Returns the countries available for checkout, derived from markets
# @return [Array<Spree::Country>]
def countries_available_for_checkout
- @countries_available_for_checkout ||= Rails.cache.fetch(countries_available_for_checkout_cache_key) do
- if markets.any?
+ @countries_available_for_checkout = begin
+ if has_markets?
markets.flat_map(&:countries).uniq.sort_by(&:name)
else
Spree::Country.all.to_a
@@ -85,7 +85,7 @@ module Spree
# Returns supported currencies derived from markets, falling back to store attributes
# @return [Array<Money::Currency>]
def supported_currencies_list
- @supported_currencies_list ||= if markets.any?
+ @supported_currencies_list ||= if has_markets?
markets.pluck(:currency).uniq.map do |code|
::Money::Currency.find(code)
end.compact.sort_by { |c| c.iso_code == default_currency ? 0 : 1 }
@@ -97,7 +97,7 @@ module Spree
# Returns supported locales derived from markets, falling back to store attributes
# @return [Array<String>]
def supported_locales_list
- @supported_locales_list ||= if markets.any?
+ @supported_locales_list ||= if has_markets?
(markets.flat_map(&:supported_locales_list) << default_locale).compact.uniq.sort
else
legacy_supported_locales_list
@@ -107,7 +107,7 @@ module Spree
private
def has_markets?
- persisted? && (markets.loaded? ? markets.any? : markets.exists?)
+ @has_markets ||= persisted? && (markets.loaded? ? markets.any? : markets.exists?)
end
def legacy_supported_currencies_list
diff --git a/spree/core/app/models/concerns/spree/vat_price_calculation.rb b/spree/core/app/models/concerns/spree/vat_price_calculation.rb
index c4ddb447ee..6ab1348a6a 100644
--- a/spree/core/app/models/concerns/spree/vat_price_calculation.rb
+++ b/spree/core/app/models/concerns/spree/vat_price_calculation.rb
@@ -1,7 +1,7 @@
module Spree
module VatPriceCalculation
def gross_amount(amount, price_options)
- return amount unless outside_default_vat_zone?(price_options)
+ return amount if amount.nil? || !outside_default_vat_zone?(price_options)
round_to_two_places(add_foreign_vat_for(amount, price_options))
end
@@ -38,7 +38,7 @@ module Spree
end
def default_zone
- @_default_zone ||= Spree::Zone.default_tax
+ @default_zone ||= Spree::Zone.default_tax
end
def round_to_two_places(amount)
diff --git a/spree/core/app/models/spree/adjustable/promotion_accumulator.rb b/spree/core/app/models/spree/adjustable/promotion_accumulator.rb
index c4517317a8..fd00697e6b 100644
--- a/spree/core/app/models/spree/adjustable/promotion_accumulator.rb
+++ b/spree/core/app/models/spree/adjustable/promotion_accumulator.rb
@@ -25,7 +25,7 @@ module Spree
def add_adjustment(adjustment, opts = {})
return unless adjustment.promotion?
- source = opts[:source] || adjustment.cached_source
+ source = opts[:source] || adjustment.source
promotion = opts[:promotion] || source.promotion
add(adjustments, adjustment, adjustment.id)
diff --git a/spree/core/app/models/spree/adjustment.rb b/spree/core/app/models/spree/adjustment.rb
index ad70503a86..923d0ca7ac 100644
--- a/spree/core/app/models/spree/adjustment.rb
+++ b/spree/core/app/models/spree/adjustment.rb
@@ -98,30 +98,11 @@ module Spree
!included?
end
- # Returns the source using Rails.cache to avoid repeated database lookups.
- # Sources are cached by their type and ID combination.
- # Cache is automatically invalidated when the source is saved (see AdjustmentSource concern).
- #
- # @return [Object, nil] The source object (TaxRate, PromotionAction, etc.)
- def cached_source
- return nil if source_type.blank? || source_id.blank?
-
- Rails.cache.fetch(source_cache_key) { source }
- rescue TypeError
- # Handle objects that can't be serialized (e.g., mock objects in tests)
- source
- end
-
- # Cache key for the source object
- def source_cache_key
- "spree/adjustment_source/#{source_type}/#{source_id}"
- end
-
# Passing a target here would always be recommended as it would avoid
# hitting the database again and would ensure you're compute values over
# the specific object amount passed here.
def update!(target = adjustable)
- src = cached_source
+ src = source
return amount if closed? || src.blank?
new_amount = src.compute_amount(target)
diff --git a/spree/core/app/models/spree/country.rb b/spree/core/app/models/spree/country.rb
index 1e759621ce..75dfc49886 100644
--- a/spree/core/app/models/spree/country.rb
+++ b/spree/core/app/models/spree/country.rb
@@ -43,7 +43,7 @@ module Spree
#
# @return [String, nil] currency code (e.g., 'USD', 'EUR') or nil if no market found
def market_currency
- Spree::Current.store&.market_for_country(self)&.currency
+ current_market&.currency
end
# Returns the default locale for this country from its market in the current store.
@@ -51,14 +51,18 @@ module Spree
#
# @return [String, nil] locale code (e.g., 'en', 'de') or nil if no market found
def market_locale
- Spree::Current.store&.market_for_country(self)&.default_locale
+ current_market&.default_locale
end
# Returns the supported locales for this country from its market in the current store.
#
# @return [Array<String>] locale codes (e.g., ['en', 'fr']) or empty array if no market found
def market_supported_locales
- Spree::Current.store&.market_for_country(self)&.supported_locales_list || []
+ current_market&.supported_locales_list || []
+ end
+
+ def current_market
+ @current_market ||= Spree::Current.store&.market_for_country(self)
end
def <=>(other)
diff --git a/spree/core/app/models/spree/current.rb b/spree/core/app/models/spree/current.rb
index 2939af21bc..e207733822 100644
--- a/spree/core/app/models/spree/current.rb
+++ b/spree/core/app/models/spree/current.rb
@@ -4,7 +4,9 @@ module Spree
# All attributes are automatically reset between requests by Rails.
# Fallback chains ensure sensible defaults when attributes are not explicitly set.
class Current < ::ActiveSupport::CurrentAttributes
- attribute :store, :market, :currency, :locale, :zone, :price_lists, :global_pricing_context
+ attribute :store, :market, :currency, :locale, :zone, :default_tax_zone, :price_lists, :global_pricing_context
+
+ resets { @default_tax_zone_loaded = false }
# Returns the current store, falling back to the default store.
# @return [Spree::Store]
@@ -35,7 +37,17 @@ module Spree
# Returns the current tax zone, falling back to the default tax zone.
# @return [Spree::Zone, nil]
def zone
- super || Spree::Zone.default_tax
+ super || default_tax_zone
+ end
+
+ # Returns the default tax zone (memoized per request).
+ # @return [Spree::Zone, nil]
+ def default_tax_zone
+ result = super
+ return result if result || @default_tax_zone_loaded
+
+ @default_tax_zone_loaded = true
+ self.default_tax_zone = Spree::Zone.find_by(default_tax: true)
end
# Returns the current price lists for the global pricing context.
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index 1f5a249f32..97a7da45af 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -668,9 +668,7 @@ module Spree
def taxons_for_store(store)
return if classification_count.zero?
- Rails.cache.fetch("#{cache_key_with_version}/taxons-per-store/#{store.id}") do
- taxons.loaded? ? taxons.find_all { |taxon| taxon.taxonomy.store_id == store.id } : taxons.for_store(store)
- end
+ taxons.loaded? ? taxons.find_all { |taxon| taxon.taxonomy.store_id == store.id } : taxons.for_store(store)
end
def any_variant_in_stock_or_backorderable?
diff --git a/spree/core/app/models/spree/promotion.rb b/spree/core/app/models/spree/promotion.rb
index 5bb10fa841..d05d37e990 100644
--- a/spree/core/app/models/spree/promotion.rb
+++ b/spree/core/app/models/spree/promotion.rb
@@ -190,26 +190,14 @@ 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 cached_rules.none?
+ return [] if rules.none?
- specific_rules = cached_rules.select { |rule| rule.applicable?(promotable) }
+ specific_rules = rules.select { |rule| rule.applicable?(promotable) }
return [] if specific_rules.none?
rule_eligibility = Hash[specific_rules.map do |rule|
diff --git a/spree/core/app/models/spree/promotion_handler/coupon.rb b/spree/core/app/models/spree/promotion_handler/coupon.rb
index ea8629eee7..d392847e34 100644
--- a/spree/core/app/models/spree/promotion_handler/coupon.rb
+++ b/spree/core/app/models/spree/promotion_handler/coupon.rb
@@ -177,8 +177,8 @@ module Spree
# Check for applied adjustments.
discount = order.all_adjustments.promotion.eligible.detect do |p|
- p.cached_source.promotion.code.try(:downcase) == coupon_code ||
- Spree::CouponCode.unused.where(promotion_id: p.cached_source.promotion_id, code: coupon_code).exists?
+ p.source.promotion.code.try(:downcase) == coupon_code ||
+ Spree::CouponCode.unused.where(promotion_id: p.source.promotion_id, code: coupon_code).exists?
end
# Check for applied line items.
@@ -209,7 +209,7 @@ module Spree
end
def handle_coupon_code(discount, coupon_code)
- Spree::CouponCode.unused.find_by(promotion_id: discount.cached_source.promotion_id, code: coupon_code)&.apply_order!(order)
+ Spree::CouponCode.unused.find_by(promotion_id: discount.source.promotion_id, code: coupon_code)&.apply_order!(order)
end
def load_gift_card_code
diff --git a/spree/core/app/models/spree/shipping_category.rb b/spree/core/app/models/spree/shipping_category.rb
index 217f936575..9b2b580148 100644
--- a/spree/core/app/models/spree/shipping_category.rb
+++ b/spree/core/app/models/spree/shipping_category.rb
@@ -16,10 +16,10 @@ module Spree
find_by(name: DIGITAL_NAME)
end
+ # Returns true if this shipping category includes a digital shipping method
+ # @return [Boolean]
def includes_digital_shipping_method?
- Rails.cache.fetch("#{cache_key_with_version}/includes-digital-shipping-method") do
- shipping_methods.digital.exists?
- end
+ @includes_digital_shipping_method ||= shipping_methods.digital.exists?
end
end
end
diff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rb
index b6480dd974..f698fb83c5 100644
--- a/spree/core/app/models/spree/store.rb
+++ b/spree/core/app/models/spree/store.rb
@@ -167,7 +167,6 @@ module Spree
after_create :ensure_default_automatic_taxons
after_create :ensure_default_post_categories_are_created
after_create :create_default_policies
- after_commit :clear_cache
#
# Scopes
@@ -184,17 +183,15 @@ module Spree
# @deprecated The or_initialize behavior will be removed in Spree 5.5.
def self.default
- Rails.cache.fetch('default_store') do
- # workaround for Mobility bug with first_or_initialize
- if where(default: true).any?
- where(default: true).first
- else
- Spree::Deprecation.warn(
- 'Spree::Store.default returning a new unpersisted store when no default store exists is deprecated ' \
- 'and will be removed in Spree 5.5. Please ensure a default store is created before calling Store.default.'
- )
- new(default: true)
- end
+ # workaround for Mobility bug with first_or_initialize
+ if where(default: true).any?
+ where(default: true).first
+ else
+ Spree::Deprecation.warn(
+ 'Spree::Store.default returning a new unpersisted store when no default store exists is deprecated ' \
+ 'and will be removed in Spree 5.5. Please ensure a default store is created before calling Store.default.'
+ )
+ new(default: true)
end
end
@@ -296,9 +293,7 @@ module Spree
# @param country [Spree::Country] the country to get the states for
# @return [Array<Spree::State>]
def states_available_for_checkout(country)
- Rails.cache.fetch(states_available_for_checkout_cache_key(country)) do
- country.states.to_a
- end
+ country.states.to_a
end
# @deprecated Use {Spree::Zone.all} or {#countries_with_shipping_coverage} instead.
@@ -388,18 +383,6 @@ module Spree
private
- def countries_available_for_checkout_cache_key
- "#{cache_key_with_version}/countries_available_for_checkout"
- end
-
- def states_available_for_checkout_cache_key(country)
- "#{cache_key_with_version}/states_available_for_checkout/#{country&.cache_key_with_version}"
- end
-
- def clear_cache
- Rails.cache.delete('default_store')
- end
-
def ensure_default_market
return if markets.exists?
diff --git a/spree/core/app/models/spree/tax_category.rb b/spree/core/app/models/spree/tax_category.rb
index d423dfd86e..2c9a3f02e2 100644
--- a/spree/core/app/models/spree/tax_category.rb
+++ b/spree/core/app/models/spree/tax_category.rb
@@ -10,15 +10,11 @@ module Spree
has_many :variants, dependent: :nullify
before_save :set_default_category
- after_update :delete_cache
- after_create :delete_cache
self.whitelisted_ransackable_attributes = %w[name is_default tax_code]
def self.default
- Rails.cache.fetch('default_tax_category') do
- find_by(is_default: true)
- end
+ find_by(is_default: true)
end
def set_default_category
@@ -28,11 +24,5 @@ module Spree
tax_category.update_columns(is_default: false, updated_at: Time.current)
end
end
-
- private
-
- def delete_cache
- Rails.cache.delete('default_tax_category')
- end
end
end
diff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rb
index fc9f871df0..1394996570 100644
--- a/spree/core/app/models/spree/variant.rb
+++ b/spree/core/app/models/spree/variant.rb
@@ -88,8 +88,6 @@ module Spree
after_create :increment_product_variant_count, unless: :is_master?
after_destroy :decrement_product_variant_count, unless: :is_master?
- after_touch :clear_in_stock_cache
-
scope :in_stock, -> { left_joins(:stock_items).where("#{Spree::Variant.table_name}.track_inventory = ? OR #{Spree::StockItem.table_name}.count_on_hand > ?", false, 0) }
scope :backorderable, -> { left_joins(:stock_items).where(spree_stock_items: { backorderable: true }) }
scope :in_stock_or_backorderable, -> { in_stock.or(backorderable) }
@@ -547,21 +545,13 @@ module Spree
# Returns true if the variant is in stock.
# @return [Boolean] true if the variant is in stock
def in_stock?
- @in_stock ||= if association(:stock_items).loaded? && association(:stock_locations).loaded?
- total_on_hand.positive?
- else
- Rails.cache.fetch(in_stock_cache_key, version: cache_version) do
- total_on_hand.positive?
- end
- end
+ @in_stock ||= total_on_hand.positive?
end
# Returns true if the variant is backorderable.
# @return [Boolean] true if the variant is backorderable
def backorderable?
- @backorderable ||= Rails.cache.fetch(['variant-backorderable', cache_key_with_version]) do
- quantifier.backorderable?
- end
+ @backorderable ||= quantifier.backorderable?
end
def on_sale?(currency)
@@ -619,10 +609,6 @@ module Spree
digitals.any?
end
- def clear_in_stock_cache
- Rails.cache.delete(in_stock_cache_key)
- end
-
private
def ensure_not_in_complete_orders
@@ -674,10 +660,6 @@ module Spree
end
end
- def in_stock_cache_key
- "variant-#{id}-in_stock"
- end
-
def disable_sku_validation?
Spree::Config[:disable_sku_validation]
end
diff --git a/spree/core/app/models/spree/zone.rb b/spree/core/app/models/spree/zone.rb
index aa15070d9b..c1734ef66e 100644
--- a/spree/core/app/models/spree/zone.rb
+++ b/spree/core/app/models/spree/zone.rb
@@ -27,9 +27,7 @@ module Spree
self.whitelisted_ransackable_attributes = ['description']
def self.default_tax
- Rails.cache.fetch('default_tax') do
- find_by(default_tax: true)
- end
+ Spree::Current.default_tax_zone
end
def self.potential_matching_zones(zone)
@@ -186,7 +184,6 @@ module Spree
def remove_previous_default
Spree::Zone.with_default_tax.where.not(id: id).update_all(default_tax: false)
- Rails.cache.delete('default_zone')
end
def set_zone_members(ids, type)
diff --git a/spree/core/lib/spree/core/pricing/resolver.rb b/spree/core/lib/spree/core/pricing/resolver.rb
index 2469f4fdb6..e3aa4cda37 100644
--- a/spree/core/lib/spree/core/pricing/resolver.rb
+++ b/spree/core/lib/spree/core/pricing/resolver.rb
@@ -12,9 +12,7 @@ module Spree
# Returns the best price for the variant
# @return [Spree::Price]
def resolve
- Rails.cache.fetch(cache_key, expires_in: 15.minutes) do
- find_best_price
- end
+ find_best_price
end
private
@@ -118,12 +116,6 @@ module Spree
price_list_id: nil
)
end
-
- # Returns the cache key for the resolver
- # @return [String]
- def cache_key
- "#{context.cache_key}/resolved_price"
- end
end
end
end
diff --git a/spree/core/spec/lib/spree/core/pricing/context_spec.rb b/spree/core/spec/lib/spree/core/pricing/context_spec.rb
index a744ae928a..e492d70156 100644
--- a/spree/core/spec/lib/spree/core/pricing/context_spec.rb
+++ b/spree/core/spec/lib/spree/core/pricing/context_spec.rb
@@ -12,6 +12,8 @@ module Spree
let(:quantity) { 5 }
let(:date) { Time.zone.parse('2024-01-15 12:00:00') }
+ after { Spree::Current.reset }
+
describe '#initialize' do
context 'with all parameters' do
subject do
@@ -44,8 +46,6 @@ module Spree
described_class.new(variant: variant, currency: currency)
end
- after { Spree::Current.reset }
-
it 'sets required attributes' do
expect(subject.variant).to eq(variant)
expect(subject.currency).to eq(currency)
@@ -82,7 +82,6 @@ module Spree
let(:current_store) { create(:store) }
before { Spree::Current.store = current_store }
- after { Spree::Current.reset }
subject do
described_class.new(variant: variant, currency: currency)
@@ -97,7 +96,6 @@ module Spree
let(:current_zone) { create(:zone) }
before { Spree::Current.zone = current_zone }
- after { Spree::Current.reset }
subject do
described_class.new(variant: variant, currency: currency)
@@ -112,7 +110,6 @@ module Spree
let(:current_market) { create(:market) }
before { Spree::Current.market = current_market }
- after { Spree::Current.reset }
subject do
described_class.new(variant: variant, currency: currency)
@@ -125,7 +122,6 @@ module Spree
context 'when market is explicitly nil' do
before { Spree::Current.market = nil }
- after { Spree::Current.reset }
subject do
described_class.new(variant: variant, currency: currency, market: nil)
@@ -198,7 +194,6 @@ module Spree
before do
allow(order).to receive(:tax_zone).and_return(nil)
- Rails.cache.delete('default_tax')
end
it 'falls back to default tax zone' do
@@ -265,8 +260,6 @@ module Spree
described_class.new(variant: variant, currency: currency)
end
- after { Spree::Current.reset }
-
it 'generates a cache key with default values from Spree::Current' do
Timecop.freeze do
expected_parts = [
@@ -295,8 +288,6 @@ module Spree
)
end
- after { Spree::Current.reset }
-
it 'includes present optional attributes in correct order' do
Timecop.freeze do
expected_parts = [
diff --git a/spree/core/spec/lib/spree/pricing/resolver_spec.rb b/spree/core/spec/lib/spree/pricing/resolver_spec.rb
index 0d845bb536..fbe2ffb2a7 100644
--- a/spree/core/spec/lib/spree/pricing/resolver_spec.rb
+++ b/spree/core/spec/lib/spree/pricing/resolver_spec.rb
@@ -7,11 +7,6 @@ describe Spree::Pricing::Resolver do
let(:context) { Spree::Pricing::Context.new(variant: variant, currency: currency, store: store) }
let(:resolver) { described_class.new(context) }
- # Clear cache before each test to avoid stale cached prices
- before do
- Rails.cache.clear
- end
-
describe '#resolve' do
context 'when no price lists exist' do
it 'returns the base price' do
@@ -79,7 +74,6 @@ describe Spree::Pricing::Resolver do
it 'returns base price when outside date range' do
base_price = variant.prices.base_prices.with_currency(currency).first
Timecop.travel(2.days.from_now) do
- Rails.cache.clear # Clear cache for the new time context
price = resolver.resolve
expect(price).to eq(base_price)
end
diff --git a/spree/core/spec/models/spree/adjustment_spec.rb b/spree/core/spec/models/spree/adjustment_spec.rb
index 918729ddc9..70b1d5002a 100644
--- a/spree/core/spec/models/spree/adjustment_spec.rb
+++ b/spree/core/spec/models/spree/adjustment_spec.rb
@@ -231,39 +231,4 @@ describe Spree::Adjustment, type: :model do
end
end
end
-
- describe '#cached_source' do
- let(:order) { create(:order) }
- let(:tax_rate) { create(:tax_rate) }
- let(:adjustment) { create(:adjustment, order: order, source: tax_rate) }
-
- it 'returns the source' do
- expect(adjustment.cached_source).to eq(tax_rate)
- end
-
- it 'uses Rails.cache.fetch to cache the source' do
- expect(Rails.cache).to receive(:fetch).with(adjustment.source_cache_key).and_call_original
- adjustment.cached_source
- end
-
- it 'returns nil when source_type is blank' do
- adjustment.update_columns(source_type: nil, source_id: nil)
- expect(adjustment.cached_source).to be_nil
- end
-
- it 'returns nil when source_id is blank' do
- adjustment.update_columns(source_id: nil)
- expect(adjustment.cached_source).to be_nil
- end
- end
-
- describe '#source_cache_key' do
- let(:order) { create(:order) }
- let(:tax_rate) { create(:tax_rate) }
- let(:adjustment) { create(:adjustment, order: order, source: tax_rate) }
-
- it 'returns a cache key based on source type and id' do
- expect(adjustment.source_cache_key).to eq("spree/adjustment_source/Spree::TaxRate/#{tax_rate.id}")
- end
- end
end
diff --git a/spree/core/spec/models/spree/country_spec.rb b/spree/core/spec/models/spree/country_spec.rb
index 1483ddfd25..02be3c24a6 100644
--- a/spree/core/spec/models/spree/country_spec.rb
+++ b/spree/core/spec/models/spree/country_spec.rb
@@ -91,7 +91,10 @@ describe Spree::Country, type: :model do
end
describe '#market_currency' do
- before { Spree::Current.store = store }
+ before do
+ america.remove_instance_variable(:@current_market) if america.instance_variable_defined?(:@current_market)
+ Spree::Current.store = store
+ end
after { Spree::Current.reset }
context 'when country belongs to a market' do
@@ -118,7 +121,10 @@ describe Spree::Country, type: :model do
end
describe '#market_locale' do
- before { Spree::Current.store = store }
+ before do
+ america.remove_instance_variable(:@current_market) if america.instance_variable_defined?(:@current_market)
+ Spree::Current.store = store
+ end
after { Spree::Current.reset }
context 'when country belongs to a market' do
@@ -145,7 +151,10 @@ describe Spree::Country, type: :model do
end
describe '#market_supported_locales' do
- before { Spree::Current.store = store }
+ before do
+ america.remove_instance_variable(:@current_market) if america.instance_variable_defined?(:@current_market)
+ Spree::Current.store = store
+ end
after { Spree::Current.reset }
context 'when country belongs to a market' do
diff --git a/spree/core/spec/models/spree/current_spec.rb b/spree/core/spec/models/spree/current_spec.rb
index d97c7c4d53..86fcbfdf1a 100644
--- a/spree/core/spec/models/spree/current_spec.rb
+++ b/spree/core/spec/models/spree/current_spec.rb
@@ -63,7 +63,6 @@ RSpec.describe Spree::Current do
context 'when zone is not set and no default tax zone exists' do
before do
Spree::Zone.update_all(default_tax: false)
- Rails.cache.delete('default_tax')
end
it 'returns nil' do
@@ -72,6 +71,48 @@ RSpec.describe Spree::Current do
end
end
+ describe '#default_tax_zone' do
+ context 'when default_tax_zone is explicitly set' do
+ let(:zone) { create(:zone) }
+
+ before { described_class.default_tax_zone = zone }
+
+ it 'returns the set zone' do
+ expect(described_class.default_tax_zone).to eq(zone)
+ end
+ end
+
+ context 'when default_tax_zone is not set' do
+ let!(:default_zone) { create(:zone, default_tax: true) }
+
+ it 'returns the default tax zone from the database' do
+ expect(described_class.default_tax_zone).to eq(default_zone)
+ end
+
+ it 'memoizes the result' do
+ described_class.default_tax_zone
+
+ expect(Spree::Zone).not_to receive(:find_by)
+ described_class.default_tax_zone
+ end
+ end
+
+ context 'when no default tax zone exists' do
+ before { Spree::Zone.update_all(default_tax: false) }
+
+ it 'returns nil' do
+ expect(described_class.default_tax_zone).to be_nil
+ end
+
+ it 'does not re-query on subsequent calls' do
+ described_class.default_tax_zone
+
+ expect(Spree::Zone).not_to receive(:find_by)
+ described_class.default_tax_zone
+ end
+ end
+ end
+
describe '#locale' do
context 'when locale is set' do
before { described_class.locale = 'fr' }
@@ -250,5 +291,14 @@ RSpec.describe Spree::Current do
# After reset, global_pricing_context should be fetched fresh
expect(described_class.instance_variable_get(:@global_pricing_context)).to be_nil
end
+
+ it 'clears memoized default_tax_zone' do
+ create(:zone, default_tax: true)
+ described_class.default_tax_zone
+
+ described_class.reset
+
+ expect(described_class.instance.instance_variable_get(:@default_tax_zone_loaded)).to be false
+ end
end
end
diff --git a/spree/core/spec/models/spree/promotion_spec.rb b/spree/core/spec/models/spree/promotion_spec.rb
index 3f9a3e47f1..6ba8ed89f4 100644
--- a/spree/core/spec/models/spree/promotion_spec.rb
+++ b/spree/core/spec/models/spree/promotion_spec.rb
@@ -858,10 +858,4 @@ 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
diff --git a/spree/core/spec/models/spree/store_spec.rb b/spree/core/spec/models/spree/store_spec.rb
index 2a99271d94..3f6147cdd8 100644
--- a/spree/core/spec/models/spree/store_spec.rb
+++ b/spree/core/spec/models/spree/store_spec.rb
@@ -366,8 +366,6 @@ describe Spree::Store, type: :model, without_global_store: true do
end
describe '.default' do
- before { Rails.cache.clear }
-
context 'when a default store is already present' do
let!(:store_2) { create(:store, default: true) }
@@ -382,7 +380,6 @@ describe Spree::Store, type: :model, without_global_store: true do
before do
described_class::Translation.delete_all
described_class.delete_all
- Rails.cache.clear
end
it 'builds a new default store' do
@@ -401,7 +398,6 @@ describe Spree::Store, type: :model, without_global_store: true do
before do
Spree::Store.where.not(id: store.id).update_all(default: false)
- Rails.cache.clear
end
it 'returns the default store supported locales' do
diff --git a/spree/core/spec/models/spree/variant_spec.rb b/spree/core/spec/models/spree/variant_spec.rb
index 3dd81c3377..23865b38eb 100644
--- a/spree/core/spec/models/spree/variant_spec.rb
+++ b/spree/core/spec/models/spree/variant_spec.rb
@@ -1082,11 +1082,6 @@ describe Spree::Variant, type: :model do
expect(variant.product.reload.updated_at).to be_within(3.seconds).of(Time.current)
end
- it 'clears the in_stock cache key' do
- expect(Rails.cache).to receive(:delete).with(variant.send(:in_stock_cache_key))
- variant.touch
- end
-
context 'when unlinking an option value' do
let(:option_value) { create(:option_value) }
diff --git a/spree/core/spec/spec_helper.rb b/spree/core/spec/spec_helper.rb
index 2cf4f90068..40c48d115a 100644
--- a/spree/core/spec/spec_helper.rb
+++ b/spree/core/spec/spec_helper.rb
@@ -93,6 +93,7 @@ RSpec.configure do |config|
config.before(:each) do
reset_spree_preferences
+ Spree::Current.reset
end
config.include FactoryBot::Syntax::Methods