Simplify default country code lookup

The code was using the code from the environment variables to load a
reocrd from the database to then return the initial code again.

The only use of `DefaultCountry.code` is currently in the geocoder JS
compilation. Now it doesn't need the database anymore.
This commit is contained in:
Maikel Linke
2025-01-28 13:12:28 +11:00
parent 4b6c3fe1d0
commit ea6efa9164

View File

@@ -5,15 +5,14 @@ class DefaultCountry
country.id
end
# Two letter code defined in ISO-3166-1.
def self.code
country.iso
# Changing ENV requires restarting the process.
ENV.fetch("DEFAULT_COUNTRY_CODE", nil)
end
def self.country
# Changing ENV requires restarting the process.
iso = ENV.fetch("DEFAULT_COUNTRY_CODE", nil)
# When ENV changes on restart, this cache will be reset as well.
@country ||= Spree::Country.find_by(iso:) || Spree::Country.first
@country ||= Spree::Country.find_by(iso: code) || Spree::Country.first
end
end