Skip to content

Commit

Permalink
Add rake task for country code update to 3166-1
Browse files Browse the repository at this point in the history
  • Loading branch information
balazstar authored and steveyken committed Dec 20, 2012
1 parent c1d1c7a commit 32abdb5
Showing 1 changed file with 193 additions and 0 deletions.
193 changes: 193 additions & 0 deletions lib/tasks/fat_free_crm.rake
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,197 @@ namespace :ffcrm do
puts "Admin user has been created."
end
end

desc "Update country codes to ISO 3166-1"
namespace :update_data do
task :update_country_codes_to_iso3166_1 => :environment do
convert_table = [
["Aland Islands", "FI", "AX"],
["Algeria", "AG", "DZ"],
["American Samoa", "AQ", "AS"],
["Andorra", "AN", "AD"],
["Anguilla", "AV", "AI"],
["Antarctica", "AY", "AQ"],
["Antigua and Barbuda", "AC", "AG"],
["Aruba", "AA", "AW"],
["Australia", "AS", "AU"],
["Austria", "AU", "AT"],
["Azerbaijan", "AJ", "AZ"],
["Bahamas", "BF", "BS"],
["Bahrain", "BA", "BH"],
["Bangladesh", "BG", "BD"],
["Belarus", "BO", "BY"],
["Belize", "BH", "BZ"],
["Benin", "BN", "BJ"],
["Bermuda", "BD", "BM"],
["Bosnia and Herzegovina", "BK", "BA"],
["Botswana", "BC", "BW"],
["Brunei Darussalam", "BX", "BN"],
["Bulgaria", "BU", "BG"],
["Burkina Faso", "UV", "BF"],
["Burundi", "BY", "BI"],
["Cambodia", "CB", "KH"],
["Cayman Islands", "CJ", "KY"],
["Central African Republic", "CT", "CF"],
["Chad", "CD", "TD"],
["Chile", "CI", "CL"],
["China", "CH", "CN"],
["Christmas Island", "KT", "CX"],
["Cocos (Keeling) Islands", "CK", "CC"],
["Comoros", "CN", "KM"],
["Congo, the Democratic Republic of the", "CF", "CD"],
["Cook Islands", "CW", "CK"],
["Costa Rica", "CS", "CR"],
["Czech Republic", "EZ", "CZ"],
["Denmark", "DA", "DK"],
["Dominica", "DO", "DM"],
["Dominican Republic", "DR", "DO"],
["El Salvador", "ES", "SV"],
["Equatorial Guinea", "EK", "GQ"],
["Estonia", "EN", "EE"],
["French Guiana", "FG", "GF"],
["French Polynesia", "FP", "PF"],
["French Southern Territories", "FS", "TF"],
["Gabon", "GB", "GA"],
["Gambia", "GA", "GM"],
["Georgia", "GG", "GE"],
["Germany", "GM", "DE"],
["Grenada", "GJ", "GD"],
["Guam", "GQ", "GU"],
["Guernsey", "GK", "GG"],
["Guinea", "GV", "GN"],
["Guinea-Bissau", "PU", "GW"],
["Haiti", "HA", "HT"],
["Holy See (Vatican City State)", "VT", "VA"],
["Honduras", "HO", "HN"],
["Iceland", "IC", "IS"],
["Iraq", "IZ", "IQ"],
["Ireland", "EI", "IE"],
["Israel", "IS", "IL"],
["Japan", "JA", "JP"],
["Kiribati", "KR", "KI"],
["Korea, Democratic People's Republic of", "KN", "KP"],
["Korea, Republic of", "KS", "KR"],
["Kuwait", "KU", "KW"],
["Latvia", "LG", "LV"],
["Lebanon", "LE", "LB"],
["Lesotho", "LT", "LS"],
["Liberia", "LI", "LR"],
["Liechtenstein", "LS", "LI"],
["Lithuania", "LH", "LT"],
["Macao", "MC", "MO"],
["Madagascar", "MA", "MG"],
["Malawi", "MI", "MW"],
["Marshall Islands", "RM", "MH"],
["Martinique", "MB", "MQ"],
["Mauritius", "MP", "MU"],
["Mayotte", "MF", "YT"],
["Monaco", "MN", "MC"],
["Mongolia", "MG", "MN"],
["Montenegro", "MJ", "ME"],
["Montserrat", "MH", "MS"],
["Morocco", "MO", "MA"],
["Myanmar", "BM", "MM"],
["Namibia", "WA", "NA"],
["Nicaragua", "NU", "NI"],
["Niger", "NG", "NE"],
["Nigeria", "NI", "NG"],
["Niue", "NE", "NU"],
["Northern Mariana Islands", "CQ", "MP"],
["Oman", "MU", "OM"],
["Palau", "PS", "PW"],
["Palestinian Territory, Occupied", "WE", "PS"],
["Panama", "PM", "PA"],
["Papua New Guinea", "PP", "PG"],
["Paraguay", "PA", "PY"],
["Philippines", "RP", "PH"],
["Pitcairn", "PC", "PN"],
["Portugal", "PO", "PT"],
["Puerto Rico", "RQ", "PR"],
["Russian Federation", "RS", "RU"],
["Saint Kitts and Nevis", "SC", "KN"],
["Saint Lucia", "ST", "LC"],
["Saint Pierre and Miquelon", "SB", "PM"],
["Sao Tome and Principe", "TP", "ST"],
["Senegal", "SG", "SN"],
["Serbia", "RB", "RS"],
["Seychelles", "SE", "SC"],
["Singapore", "SN", "SG"],
["Slovakia", "LO", "SK"],
["Solomon Islands", "BP", "SB"],
["South Africa", "SF", "ZA"],
["South Georgia and the South Sandwich Islands", "SX", "GS"],
["Spain", "SP", "ES"],
["Sri Lanka", "CE", "LK"],
["Sudan", "SU", "SD"],
["Suriname", "NS", "SR"],
["Svalbard and Jan Mayen", "SV", "SJ"],
["Swaziland", "WZ", "SZ"],
["Sweden", "SW", "SE"],
["Switzerland", "SZ", "CH"],
["Tajikistan", "TI", "TJ"],
["Timor-Leste", "TT", "TL"],
["Togo", "TO", "TG"],
["Tokelau", "TL", "TK"],
["Tonga", "TN", "TO"],
["Trinidad and Tobago", "TD", "TT"],
["Tunisia", "TS", "TN"],
["Turkey", "TU", "TR"],
["Turkmenistan", "TX", "TM"],
["Turks and Caicos Islands", "TK", "TC"],
["Ukraine", "UP", "UA"],
["United Kingdom", "UK", "GB"],
["Vanuatu", "NH", "VU"],
["Viet Nam", "VM", "VN"],
["Virgin Islands, British", "VI", "VG"],
["Virgin Islands, U.S.", "VQ", "VI"],
["Western Sahara", "WI", "EH"],
["Yemen", "YM", "YE"],
["Zambia", "ZA", "ZM"],
["Zimbabwe", "ZI", "ZW"],
["United States", "USA", "US"],
["United States", "United States", "US"],
["Argentina", "Argentina", "AR"],
["Australia", "Australia", "AU"],
["Brasil", "Brasil", "BR"],
["Canada", "Canada", "CA"],
["Finland", "Finland", "FI"],
["France", "France", "FR"],
["Germany", "Germany", "DE"],
["Italy", "Italy", "IT"],
["Japan", "Japan", "JP"],
["Mexico", "Mexico", "MX"],
["Norway", "Norway", "NO"],
["Poland", "Poland", "PL"],
["Portugal", "Portugal", "PT"],
["Spain", "Spain", "ES"],
["Sweden", "Sweden", "SE"],
["Russia", "Russia", "RU"],
["United Kingdom", "United Kingdom", "GB"]
]

addresses_to_update = []

convert_table.each { |ct|
tmp = Address.where(:country => ct[1])
tmp.map{ |t| t.country=ct[2] }

unless tmp.blank?
addresses_to_update << tmp
end
}

begin
addresses_to_update.each { |address_arr|
address_arr.each { |address|
address.save!
}
}
rescue Exception => e
ActiveRecord::Rollback
puts e
end

end
end
end

0 comments on commit 32abdb5

Please sign in to comment.