remove setters from DefaultCountry service

This commit is contained in:
Andy Brett
2021-05-19 09:42:52 -07:00
parent 45ad4bbcf1
commit e73584fef7
3 changed files with 16 additions and 25 deletions

View File

@@ -1,22 +1,13 @@
class DefaultCountry
def self.id
DefaultCountry.country.id
country.id
end
def self.code
DefaultCountry.country.iso
country.iso
end
def self.country
Spree::Country.find_by(iso: ENV["DEFAULT_COUNTRY_CODE"]) || Spree::Country.first
end
def self.id=(id)
ENV["DEFAULT_COUNTRY_CODE"] = Spree::Country.find(id).iso
end
def self.code=(code)
country = Spree::Country.find_by(iso: code) || Spree::Country.first
ENV["DEFAULT_COUNTRY_CODE"] = country.iso
end
end

View File

@@ -10,8 +10,10 @@ describe "States" do
before(:each) do
login_as_admin
@hungary = Spree::Country.create!(name: "Hungary", iso_name: "Hungary")
DefaultCountry.code = country.iso
@hungary = Spree::Country.create!(name: "Hungary", iso_name: "Hungary", iso: "HU")
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with("DEFAULT_COUNTRY_CODE").and_return("HU")
end
# TODO: For whatever reason, rendering of the states page takes a non-trivial amount of time

View File

@@ -144,23 +144,21 @@ describe Spree::Address do
end
context ".default" do
before do
@default_country_id = DefaultCountry.id
new_country = create(:country)
DefaultCountry.code = new_country.iso
end
after do
DefaultCountry.id = @default_country_id
end
it "sets up a new record the default country" do
expect(Spree::Address.default.country).to eq DefaultCountry.country
end
# Regression test for #1142
it "uses the first available country if :default_country_id is set to an invalid value" do
DefaultCountry.code = "notacountry"
expect(Spree::Address.default.country).to eq Spree::Country.first
context "The default country code is set to an invalid value" do
before do
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with("DEFAULT_COUNTRY_CODE").and_return("notacountry")
end
it "uses the first available country" do
expect(Spree::Address.default.country).to eq Spree::Country.first
end
end
end