From f9711318889b767377a8cc0df795732d80c23f53 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 2 Nov 2022 15:33:43 +1100 Subject: [PATCH 1/3] Cache default country for an hour The default country usually never changes after the initial setup of an instance. --- app/models/spree/country.rb | 6 ++++++ app/services/default_country.rb | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/spree/country.rb b/app/models/spree/country.rb index ee94968556..6647b8d698 100644 --- a/app/models/spree/country.rb +++ b/app/models/spree/country.rb @@ -6,6 +6,12 @@ module Spree validates :name, :iso_name, presence: true + def self.cached_find_by(attrs) + Rails.cache.fetch("countries/#{attrs.hash}", expires_in: 1.hour) do + find_by(attrs) + end + end + def <=>(other) name <=> other.name end diff --git a/app/services/default_country.rb b/app/services/default_country.rb index 11b85e90a4..cb2aae92ec 100644 --- a/app/services/default_country.rb +++ b/app/services/default_country.rb @@ -10,6 +10,6 @@ class DefaultCountry end def self.country - Spree::Country.find_by(iso: ENV["DEFAULT_COUNTRY_CODE"]) || Spree::Country.first + Spree::Country.cached_find_by(iso: ENV["DEFAULT_COUNTRY_CODE"]) || Spree::Country.first end end From 392aeb732102538cedee407b74a7d2a36cb8006f Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 2 Nov 2022 15:38:02 +1100 Subject: [PATCH 2/3] Remove unused method loading default country And another unused method. --- app/services/default_stock_location.rb | 11 -------- spec/services/default_stock_location_spec.rb | 28 -------------------- 2 files changed, 39 deletions(-) diff --git a/app/services/default_stock_location.rb b/app/services/default_stock_location.rb index 0b79f446ec..fb58f537d1 100644 --- a/app/services/default_stock_location.rb +++ b/app/services/default_stock_location.rb @@ -5,17 +5,6 @@ class DefaultStockLocation NAME = 'default' - def self.create! - country = Spree::Country.find_by(iso: ENV['DEFAULT_COUNTRY_CODE']) - state = country.states.first - Spree::StockLocation.create!(name: NAME, country_id: country.id, state_id: state.id, - backorderable_default: false) - end - - def self.destroy_all - Spree::StockLocation.where(name: NAME).destroy_all - end - def self.find_or_create Spree::StockLocation.find_or_create_by(name: NAME) end diff --git a/spec/services/default_stock_location_spec.rb b/spec/services/default_stock_location_spec.rb index bba56f1c15..4ba6324d1d 100644 --- a/spec/services/default_stock_location_spec.rb +++ b/spec/services/default_stock_location_spec.rb @@ -3,34 +3,6 @@ require 'spec_helper' describe DefaultStockLocation do - describe '.create!' do - it "names the location 'OFN default'" do - stock_location = described_class.create! - expect(stock_location.name).to eq('default') - end - - it 'sets the location in the default country' do - default_country = Spree::Country.find_by(iso: ENV['DEFAULT_COUNTRY_CODE']) - stock_location = described_class.create! - expect(stock_location.country).to eq(default_country) - end - - it 'sets the first state in the country' do - default_country = Spree::Country.find_by(iso: ENV['DEFAULT_COUNTRY_CODE']) - stock_location = described_class.create! - expect(stock_location.state).to eq(default_country.states.first) - end - end - - describe '.destroy_all' do - it "removes all stock locations named 'default'" do - create(:stock_location) - - expect { described_class.destroy_all } - .to change { Spree::StockLocation.count }.to(0) - end - end - describe '.find_or_create' do context 'when a location named default already exists' do let!(:location) do From a2c4242994fd94445c3a8cd6ee30b7d51b0271a5 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 2 Nov 2022 16:11:30 +1100 Subject: [PATCH 3/3] Simplify Address code The DefaultCountry code has a fallback already. --- app/models/spree/address.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/app/models/spree/address.rb b/app/models/spree/address.rb index e4dd2cf5ed..c40cb05a28 100644 --- a/app/models/spree/address.rb +++ b/app/models/spree/address.rb @@ -29,12 +29,7 @@ module Spree delegate :name, to: :state, prefix: true, allow_nil: true def self.default - country = begin - DefaultCountry.country - rescue StandardError - Spree::Country.first - end - new(country: country) + new(country: DefaultCountry.country) end def full_name