From 168e33dedda813a5b4b7e09644d917fb89745639 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sun, 18 May 2025 11:17:25 +0200 Subject: [PATCH] Use iso_a2 from the countries table --- CHANGELOG.md | 2 +- app/helpers/country_flag_helper.rb | 210 +---------------------------- app/models/country.rb | 4 + 3 files changed, 6 insertions(+), 210 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35610bf7..bf1babf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,7 @@ Also, after updating to this version, Dawarich will start a huge background job - Reverse geocoding is now working as on-demand job instead of storing the result in the database. #619 - Stats cards now show the last update time. #733 - Visit card now shows buttons to confirm or decline a visit only if it's not confirmed or declined yet. -- Distance unit is now being stored in the user settings. You can choose between kilometers and miles, default is kilometers. The setting is accessible in the user settings -> Maps -> Distance Unit. You might want to recalculate your stats after changing the unit. +- Distance unit is now being stored in the user settings. You can choose between kilometers and miles, default is kilometers. The setting is accessible in the user settings -> Maps -> Distance Unit. You might want to recalculate your stats after changing the unit. #1126 - Fog of war is now being displayed as lines instead of dots. Thanks to @MeijiRestored! ## Fixed diff --git a/app/helpers/country_flag_helper.rb b/app/helpers/country_flag_helper.rb index d5e4f5b5..cfa711f0 100644 --- a/app/helpers/country_flag_helper.rb +++ b/app/helpers/country_flag_helper.rb @@ -13,218 +13,10 @@ module CountryFlagHelper private def country_to_code(country_name) - # Mapping of country names to ISO 3166-1 alpha-2 codes - # This is a basic mapping - you might want to expand it based on your data - mapping = { - "Afghanistan" => "AF", - "Albania" => "AL", - "Algeria" => "DZ", - "Andorra" => "AD", - "Angola" => "AO", - "Antigua and Barbuda" => "AG", - "Argentina" => "AR", - "Armenia" => "AM", - "Australia" => "AU", - "Austria" => "AT", - "Azerbaijan" => "AZ", - "Bahamas" => "BS", - "Bahrain" => "BH", - "Bangladesh" => "BD", - "Barbados" => "BB", - "Belarus" => "BY", - "Belgium" => "BE", - "Belize" => "BZ", - "Benin" => "BJ", - "Bhutan" => "BT", - "Bolivia" => "BO", - "Bosnia and Herzegovina" => "BA", - "Botswana" => "BW", - "Brazil" => "BR", - "Brunei" => "BN", - "Bulgaria" => "BG", - "Burkina Faso" => "BF", - "Burundi" => "BI", - "Cabo Verde" => "CV", - "Cambodia" => "KH", - "Cameroon" => "CM", - "Canada" => "CA", - "Central African Republic" => "CF", - "Chad" => "TD", - "Chile" => "CL", - "China" => "CN", - "Colombia" => "CO", - "Comoros" => "KM", - "Congo (Brazzaville)" => "CG", - "Congo (Kinshasa)" => "CD", - "Costa Rica" => "CR", - "Croatia" => "HR", - "Cuba" => "CU", - "Cyprus" => "CY", - "Czechia" => "CZ", - "Czech Republic" => "CZ", - "Denmark" => "DK", - "Djibouti" => "DJ", - "Dominica" => "DM", - "Dominican Republic" => "DO", - "Ecuador" => "EC", - "Egypt" => "EG", - "El Salvador" => "SV", - "Equatorial Guinea" => "GQ", - "Eritrea" => "ER", - "Estonia" => "EE", - "Eswatini" => "SZ", - "Swaziland" => "SZ", - "Ethiopia" => "ET", - "Fiji" => "FJ", - "Finland" => "FI", - "France" => "FR", - "Gabon" => "GA", - "Gambia" => "GM", - "Georgia" => "GE", - "Germany" => "DE", - "Ghana" => "GH", - "Greece" => "GR", - "Grenada" => "GD", - "Guatemala" => "GT", - "Guinea" => "GN", - "Guinea-Bissau" => "GW", - "Guyana" => "GY", - "Haiti" => "HT", - "Honduras" => "HN", - "Hungary" => "HU", - "Iceland" => "IS", - "India" => "IN", - "Indonesia" => "ID", - "Iran" => "IR", - "Iraq" => "IQ", - "Ireland" => "IE", - "Israel" => "IL", - "Italy" => "IT", - "Jamaica" => "JM", - "Japan" => "JP", - "Jordan" => "JO", - "Kazakhstan" => "KZ", - "Kenya" => "KE", - "Kiribati" => "KI", - "Kuwait" => "KW", - "Kyrgyzstan" => "KG", - "Laos" => "LA", - "Latvia" => "LV", - "Lebanon" => "LB", - "Lesotho" => "LS", - "Liberia" => "LR", - "Libya" => "LY", - "Liechtenstein" => "LI", - "Lithuania" => "LT", - "Luxembourg" => "LU", - "Madagascar" => "MG", - "Malawi" => "MW", - "Malaysia" => "MY", - "Maldives" => "MV", - "Mali" => "ML", - "Malta" => "MT", - "Marshall Islands" => "MH", - "Mauritania" => "MR", - "Mauritius" => "MU", - "Mexico" => "MX", - "Micronesia" => "FM", - "Moldova" => "MD", - "Monaco" => "MC", - "Mongolia" => "MN", - "Montenegro" => "ME", - "Morocco" => "MA", - "Mozambique" => "MZ", - "Myanmar" => "MM", - "Burma" => "MM", - "Namibia" => "NA", - "Nauru" => "NR", - "Nepal" => "NP", - "Netherlands" => "NL", - "New Zealand" => "NZ", - "Nicaragua" => "NI", - "Niger" => "NE", - "Nigeria" => "NG", - "North Korea" => "KP", - "North Macedonia" => "MK", - "Norway" => "NO", - "Oman" => "OM", - "Pakistan" => "PK", - "Palau" => "PW", - "Palestine" => "PS", - "Panama" => "PA", - "Papua New Guinea" => "PG", - "Paraguay" => "PY", - "Peru" => "PE", - "Philippines" => "PH", - "Poland" => "PL", - "Portugal" => "PT", - "Qatar" => "QA", - "Romania" => "RO", - "Russia" => "RU", - "Russian Federation" => "RU", - "Rwanda" => "RW", - "Saint Kitts and Nevis" => "KN", - "Saint Lucia" => "LC", - "Saint Vincent and the Grenadines" => "VC", - "Samoa" => "WS", - "San Marino" => "SM", - "Sao Tome and Principe" => "ST", - "Saudi Arabia" => "SA", - "Senegal" => "SN", - "Serbia" => "RS", - "Seychelles" => "SC", - "Sierra Leone" => "SL", - "Singapore" => "SG", - "Slovakia" => "SK", - "Slovenia" => "SI", - "Solomon Islands" => "SB", - "Somalia" => "SO", - "South Africa" => "ZA", - "South Korea" => "KR", - "South Sudan" => "SS", - "Spain" => "ES", - "Sri Lanka" => "LK", - "Sudan" => "SD", - "Suriname" => "SR", - "Sweden" => "SE", - "Switzerland" => "CH", - "Syria" => "SY", - "Taiwan" => "TW", - "Tajikistan" => "TJ", - "Tanzania" => "TZ", - "Thailand" => "TH", - "Timor-Leste" => "TL", - "Togo" => "TG", - "Tonga" => "TO", - "Trinidad and Tobago" => "TT", - "Tunisia" => "TN", - "Turkey" => "TR", - "Turkmenistan" => "TM", - "Tuvalu" => "TV", - "Uganda" => "UG", - "Ukraine" => "UA", - "United Arab Emirates" => "AE", - "United Kingdom" => "GB", - "UK" => "GB", - "Great Britain" => "GB", - "United States" => "US", - "USA" => "US", - "Uruguay" => "UY", - "Uzbekistan" => "UZ", - "Vanuatu" => "VU", - "Vatican City" => "VA", - "Venezuela" => "VE", - "Vietnam" => "VN", - "Yemen" => "YE", - "Zambia" => "ZM", - "Zimbabwe" => "ZW" - } + mapping = Country.names_to_iso_a2 - - # Try direct match first return mapping[country_name] if mapping[country_name] - # Try case-insensitive match or partial match mapping.each do |name, code| return code if country_name.downcase == name.downcase return code if country_name.downcase.include?(name.downcase) || name.downcase.include?(country_name.downcase) diff --git a/app/models/country.rb b/app/models/country.rb index fe649360..2cc5d4b7 100644 --- a/app/models/country.rb +++ b/app/models/country.rb @@ -8,4 +8,8 @@ class Country < ApplicationRecord .select(:id, :name, :iso_a2, :iso_a3) .first end + + def self.names_to_iso_a2 + pluck(:name, :iso_a2).to_h + end end