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.

Properly cache `Store#countries_available_for_checkout` and `Store#st… (#13084)

Damian Legawiec4d5e8a4c4f45
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 51257f0ef8..af7feec565 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -329,15 +329,20 @@ module Spree
formatted_custom_domain || formatted_url
end
+ # Returns the countries available for checkout for the store or creates a new one if it doesn't exist
+ # @return [Array<Spree::Country>]
def countries_available_for_checkout
@countries_available_for_checkout ||= Rails.cache.fetch(countries_available_for_checkout_cache_key) do
- checkout_zone.try(:country_list) || Spree::Country.all
+ (checkout_zone.try(:country_list) || Spree::Country.all).to_a
end
end
+ # Returns the states available for checkout for the store or creates a new one if it doesn't exist
+ # @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
- checkout_zone.try(:state_list_for, country) || country.states
+ (checkout_zone.try(:state_list_for, country) || country.states).to_a
end
end
diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rb
index 2216bb67f2..6006804a3e 100644
--- a/core/spec/models/spree/store_spec.rb
+++ b/core/spec/models/spree/store_spec.rb
@@ -430,7 +430,7 @@ describe Spree::Store, type: :model, without_global_store: true do
include_context 'with checkout zone not set'
it 'returns list of all countries' do
- checkout_available_countries_ids = subject.countries_available_for_checkout.ids
+ checkout_available_countries_ids = subject.countries_available_for_checkout.pluck(:id)
all_countries_ids = Spree::Country.all.ids
expect(checkout_available_countries_ids).to eq(all_countries_ids)