Merge pull request #7647 from andrewpbrett/default-country-codes

Use correct default country code
This commit is contained in:
Matt-Yorkley
2021-05-25 11:21:34 +02:00
committed by GitHub
16 changed files with 42 additions and 39 deletions

View File

@@ -10,7 +10,7 @@ Darkswarm.service "GmapsGeo", ->
# console.log "Error: #{status}"
geocode: (address, callback) ->
geocoder = new google.maps.Geocoder()
geocoder.geocode {'address': address, 'region': "<%= Spree::Country.find_by(id: Spree::Config[:default_country_id]).iso %>"}, callback
geocoder.geocode {'address': address, 'region': "<%= DefaultCountry.code %>"}, callback
distanceBetween: (src, dst) ->
google.maps.geometry.spherical.computeDistanceBetween @toLatLng(src), @toLatLng(dst)
@@ -20,4 +20,4 @@ Darkswarm.service "GmapsGeo", ->
if locatable.lat?
locatable
else
new google.maps.LatLng locatable.latitude, locatable.longitude
new google.maps.LatLng locatable.latitude, locatable.longitude

View File

@@ -28,9 +28,7 @@ module Admin
def build_resource
enterprise_group = super
enterprise_group.address = Spree::Address.new
enterprise_group.address.country = Spree::Country.find_by(
id: Spree::Config[:default_country_id]
)
enterprise_group.address.country = DefaultCountry.country
enterprise_group
end

View File

@@ -119,7 +119,7 @@ module Admin
def build_resource
enterprise = super
enterprise.address ||= Spree::Address.new
enterprise.address.country ||= Spree::Country.find_by(id: Spree::Config[:default_country_id])
enterprise.address.country ||= DefaultCountry.country
enterprise
end

View File

@@ -24,7 +24,7 @@ module Spree
def self.default
country = begin
Spree::Country.find(Spree::Config[:default_country_id])
DefaultCountry.country
rescue StandardError
Spree::Country.first
end

View File

@@ -50,7 +50,7 @@ module Spree
category = TaxCategory.includes(:tax_rates).find_by(is_default: true)
return 0 unless category
address ||= Address.new(country_id: Spree::Config[:default_country_id])
address ||= Address.new(country_id: DefaultCountry.id)
rate = category.tax_rates.detect { |tax_rate| tax_rate.zone.include? address }.try(:amount)
rate || 0

View File

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

View File

@@ -10,7 +10,7 @@
= "admin.enterprises"
= admin_inject_available_countries(module: 'admin.enterprises')
= admin_inject_json "admin.enterprises", "defaultCountryID", Spree::Config[:default_country_id]
= admin_inject_json "admin.enterprises", "defaultCountryID", DefaultCountry.id
-# Form

View File

@@ -38,7 +38,7 @@
= validated_input t(:postcode), "order.bill_address.zipcode"
.small-6.columns.right
= validated_select t(:country), "order.bill_address.country_id", {}, {"ng-init" => "order.bill_address.country_id = order.bill_address.country_id || #{Spree::Config[:default_country_id]}", "ng-options" => "c.id as c.name for c in countries"}
= validated_select t(:country), "order.bill_address.country_id", {}, {"ng-init" => "order.bill_address.country_id = order.bill_address.country_id || #{DefaultCountry.id}", "ng-options" => "c.id as c.name for c in countries"}
.row
.small-12.columns.text-right

View File

@@ -22,7 +22,7 @@
.small-6.columns
= validated_input t(:postcode), "order.ship_address.zipcode"
.small-6.columns.right
= validated_select t(:country), "order.ship_address.country_id", {}, {"ng-init" => "order.ship_address.country_id = order.ship_address.country_id || #{Spree::Config[:default_country_id]}", "ng-options" => "c.id as c.name for c in countries"}
= validated_select t(:country), "order.ship_address.country_id", {}, {"ng-init" => "order.ship_address.country_id = order.ship_address.country_id || #{DefaultCountry.id}", "ng-options" => "c.id as c.name for c in countries"}
.row
.small-6.columns
= validated_input t(:phone), "order.ship_address.phone"

View File

@@ -53,7 +53,7 @@
.small-12.medium-8.large-8.columns
.field
%label{ for: 'enterprise_country' }= t(".country_field")
%select.chunky{ id: 'enterprise_country', name: 'country', required: true, ng: { init: "setDefaultCountry(#{Spree::Config[:default_country_id]})", model: 'enterprise.country', options: 'c as c.name for c in countries' } }
%select.chunky{ id: 'enterprise_country', name: 'country', required: true, ng: { init: "setDefaultCountry(#{DefaultCountry.id})", model: 'enterprise.country', options: 'c as c.name for c in countries' } }
%span.error{ ng: { show: "details.country.$error.required && submitted" } }
= t(".country_field_error")

View File

@@ -11,8 +11,8 @@
= configurations_sidebar_menu_item Spree.t(:tax_settings), edit_admin_tax_settings_path
= configurations_sidebar_menu_item Spree.t(:zones), admin_zones_path
= configurations_sidebar_menu_item Spree.t(:countries), admin_countries_path
- if Spree::Config[:default_country_id]
= configurations_sidebar_menu_item Spree.t(:states), admin_country_states_path(Spree::Config[:default_country_id])
- if DefaultCountry.id
= configurations_sidebar_menu_item Spree.t(:states), admin_country_states_path(DefaultCountry.id)
= configurations_sidebar_menu_item Spree.t(:payment_methods), admin_payment_methods_path
= configurations_sidebar_menu_item Spree.t(:taxonomies), admin_taxonomies_path
= configurations_sidebar_menu_item Spree.t(:shipping_methods), admin_shipping_methods_path

View File

@@ -75,13 +75,6 @@ module Openfoodnetwork
initializer 'ofn.spree_locale_settings', before: 'spree.promo.environment' do |app|
Spree::Config['checkout_zone'] = ENV['CHECKOUT_ZONE']
Spree::Config['currency'] = ENV['CURRENCY']
if ActiveRecord::Base.connected? && Spree::Country.table_exists?
country = Spree::Country.find_by(iso: ENV['DEFAULT_COUNTRY_CODE'])
Spree::Config['default_country_id'] = country.id if country.present?
else
Spree::Config['default_country_id'] = 12 # Australia
end
end
# Register Spree calculators

View File

@@ -227,4 +227,3 @@ Spree::Country.create!([
{ name: "Saint Kitts and Nevis", iso3: "KNA", iso: "KN", iso_name: "SAINT KITTS AND NEVIS", numcode: "659" },
{ name: "Serbia", iso3: "SRB", iso: "RS", "iso_name" => "SERBIA", numcode: "999" }
])
Spree::Config[:default_country_id] = Spree::Country.find_by(iso: ENV.fetch("DEFAULT_COUNTRY_CODE", "US")).id

View File

@@ -10,8 +10,10 @@ describe "States" do
before(:each) do
login_as_admin
@hungary = Spree::Country.create!(name: "Hungary", iso_name: "Hungary")
Spree::Config[:default_country_id] = country.id
@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 = Spree::Config[:default_country_id]
new_country = create(:country)
Spree::Config[:default_country_id] = new_country.id
end
after do
Spree::Config[:default_country_id] = @default_country_id
end
it "sets up a new record with Spree::Config[:default_country_id]" do
expect(Spree::Address.default.country).to eq Spree::Country.find(Spree::Config[:default_country_id])
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
Spree::Config[:default_country_id] = "0"
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

View File

@@ -168,7 +168,7 @@ RSpec.configure do |config|
# Geocoding
config.before(:each) { allow_any_instance_of(Spree::Address).to receive(:geocode).and_return([1, 1]) }
default_country_id = Spree::Config[:default_country_id]
default_country_id = DefaultCountry.id
checkout_zone = Spree::Config[:checkout_zone]
currency = Spree::Config[:currency]
# Ensure we start with consistent config settings