From ddabbcecc5637c285771423e0f286c49464f9861 Mon Sep 17 00:00:00 2001 From: Scott Comer Date: Mon, 17 Mar 2014 22:20:41 -0500 Subject: [PATCH] fix countries to use labels for users, bands, and new signups --- .../assets/javascripts/accounts_profile.js | 78 +++++++++++++++---- web/app/assets/javascripts/band_setup.js | 33 ++++---- web/app/assets/javascripts/jam_rest.js | 7 ++ .../api_maxmind_requests_controller.rb | 5 ++ web/app/controllers/users_controller.rb | 2 +- web/app/views/users/new.html.erb | 6 +- web/config/routes.rb | 1 + web/lib/max_mind_manager.rb | 12 +++ 8 files changed, 108 insertions(+), 36 deletions(-) diff --git a/web/app/assets/javascripts/accounts_profile.js b/web/app/assets/javascripts/accounts_profile.js index d5880f1f1..002f27b9b 100644 --- a/web/app/assets/javascripts/accounts_profile.js +++ b/web/app/assets/javascripts/accounts_profile.js @@ -12,6 +12,7 @@ var loadingCitiesData = false; var loadingRegionsData = false; var loadingCountriesData = false; + var nilOptionStr = ''; var nilOptionText = 'n/a'; function beforeShow(data) { @@ -109,18 +110,20 @@ function populateCountries(countries, userCountry) { + // countries has the format ["US", ...] + var foundCountry = false; var countrySelect = getCountryElement(); countrySelect.children().remove(); - var nilOption = $(''); + var nilOption = $(nilOptionStr); nilOption.text(nilOptionText); countrySelect.append(nilOption); $.each(countries, function(index, country) { if(!country) return; - var option = $(''); + var option = $(nilOptionStr); option.text(country); option.attr("value", country); @@ -132,10 +135,53 @@ }); if(!foundCountry) { - // in this case, the user has a country that is not in the database - // this can happen in a development/test scenario, but let's assume it can - // happen in production too. - var option = $(''); + // in this case, the user has a country that is not in the database + // this can happen in a development/test scenario, but let's assume it can + // happen in production too. + var option = $(nilOptionStr); + option.text(userCountry); + option.attr("value", userCountry); + countrySelect.append(option); + } + + countrySelect.val(userCountry); + countrySelect.attr("disabled", null) + + context.JK.dropdown(countrySelect); + } + + + function populateCountriesx(countriesx, userCountry) { + + // countriesx has the format [{countrycode: "US", countryname: "United States"}, ...] + + var foundCountry = false; + var countrySelect = getCountryElement(); + countrySelect.children().remove(); + + var nilOption = $(nilOptionStr); + nilOption.text(nilOptionText); + countrySelect.append(nilOption); + + $.each(countriesx, function(index, countryx) { + if(!countryx.countrycode) return; + + var option = $(nilOptionStr); + option.text(countryx.countryname); + option.attr("value", countryx.countrycode); + + if(countryx.countrycode == userCountry) { + foundCountry = true; + } + + countrySelect.append(option); + }); + + if(!foundCountry) { + // in this case, the user has a country that is not in the database + // this can happen in a development/test scenario, but let's assume it can + // happen in production too. + var option = $(nilOptionStr); option.text(userCountry); option.attr("value", userCountry); countrySelect.append(option); @@ -152,14 +198,14 @@ var regionSelect = getRegionElement() regionSelect.children().remove() - var nilOption = $(''); + var nilOption = $(nilOptionStr); nilOption.text(nilOptionText); regionSelect.append(nilOption); $.each(regions, function(index, region) { if(!region) return; - var option = $('') + var option = $(nilOptionStr) option.text(region) option.attr("value", region) @@ -176,14 +222,14 @@ var citySelect = getCityElement(); citySelect.children().remove(); - var nilOption = $(''); + var nilOption = $(nilOptionStr); nilOption.text(nilOptionText); citySelect.append(nilOption); $.each(cities, function(index, city) { if(!city) return; - var option = $('') + var option = $(nilOptionStr) option.text(city) option.attr("value", city) @@ -248,8 +294,8 @@ // make the 3 slower requests, which only matter if the user wants to affect their ISP or location - api.getCountries() - .done(function(countries) { populateCountries(countries["countries"], userDetail.country); } ) + api.getCountriesx() + .done(function(countriesx) { populateCountriesx(countriesx["countriesx"], userDetail.country); } ) .fail(app.ajaxError) .always(function() { loadingCountriesData = false; }) @@ -394,7 +440,7 @@ loadingRegionsData = true; regionElement.children().remove() - regionElement.append($('').text('loading...')) + regionElement.append($(nilOptionStr).text('loading...')) api.getRegions({ country: selectedCountry }) .done(getRegionsDone) @@ -405,7 +451,7 @@ } else { regionElement.children().remove() - regionElement.append($('').text(nilOptionText)) + regionElement.append($(nilOptionStr).text(nilOptionText)) } } @@ -419,7 +465,7 @@ loadingCitiesData = true; cityElement.children().remove() - cityElement.append($('').text('loading...')) + cityElement.append($(nilOptionStr).text('loading...')) api.getCities({ country: selectedCountry, region: selectedRegion }) .done(getCitiesDone) @@ -430,7 +476,7 @@ } else { cityElement.children().remove() - cityElement.append($('').text(nilOptionText)) + cityElement.append($(nilOptionStr).text(nilOptionText)) } } diff --git a/web/app/assets/javascripts/band_setup.js b/web/app/assets/javascripts/band_setup.js index 875f31f41..f1a0249d5 100644 --- a/web/app/assets/javascripts/band_setup.js +++ b/web/app/assets/javascripts/band_setup.js @@ -18,6 +18,7 @@ var userIds = []; var userPhotoUrls = []; var selectedFriendIds = {}; + var nilOptionStr = ''; var nilOptionText = 'n/a'; var bandId = ''; @@ -300,27 +301,27 @@ } function loadCountries(initialCountry, onCountriesLoaded) { - var $country = $("#band-country"); + var countrySelect = $("#band-country"); - var nilOption = $(''); + var nilOption = $(nilOptionStr); nilOption.text(nilOptionText); - $country.append(nilOption); + countrySelect.append(nilOption); - rest.getCountries().done(function (response) { - $.each(response["countries"], function (index, country) { - if (!country) return; - var option = $(''); - option.text(country); - option.attr("value", country); + rest.getCountriesx().done(function (response) { + $.each(response["countriesx"], function (index, countryx) { + if (!countryx.countrycode) return; + var option = $(nilOptionStr); + option.text(countryx.countryname); + option.attr("value", countryx.countrycode); - if (initialCountry === country) { + if (initialCountry === countryx.countrycode) { option.attr("selected", "selected"); } - $country.append(option); + countrySelect.append(option); }); - context.JK.dropdown($country); + context.JK.dropdown(countrySelect); if (onCountriesLoaded) { onCountriesLoaded(); @@ -333,7 +334,7 @@ $region.empty(); var selectedCountry = $("#band-country").val(); - var nilOption = $(''); + var nilOption = $(nilOptionStr); nilOption.text(nilOptionText); $region.append(nilOption); @@ -341,7 +342,7 @@ rest.getRegions({'country': selectedCountry}).done(function (response) { $.each(response["regions"], function (index, region) { if (!region) return; - var option = $(''); + var option = $(nilOptionStr); option.text(region); option.attr("value", region); @@ -368,7 +369,7 @@ var selectedCountry = $("#band-country").val(); var selectedRegion = $("#band-region").val(); - var nilOption = $(''); + var nilOption = $(nilOptionStr); nilOption.text(nilOptionText); $city.append(nilOption); @@ -376,7 +377,7 @@ rest.getCities({'country': selectedCountry, 'region': selectedRegion}).done(function (response) { $.each(response["cities"], function (index, city) { if (!city) return; - var option = $(''); + var option = $(nilOptionStr); option.text(city); option.attr("value", city); diff --git a/web/app/assets/javascripts/jam_rest.js b/web/app/assets/javascripts/jam_rest.js index c0e028ead..7b0baf066 100644 --- a/web/app/assets/javascripts/jam_rest.js +++ b/web/app/assets/javascripts/jam_rest.js @@ -301,6 +301,12 @@ }); } + function getCountriesx() { + return $.ajax('/api/countriesx', { + dataType : 'json' + }); + } + function getIsps(options) { var country = options["country"] @@ -911,6 +917,7 @@ this.getCities = getCities; this.getRegions = getRegions; this.getCountries = getCountries; + this.getCountriesx = getCountriesx; this.getIsps = getIsps; this.getResolvedLocation = getResolvedLocation; this.getInstruments = getInstruments; diff --git a/web/app/controllers/api_maxmind_requests_controller.rb b/web/app/controllers/api_maxmind_requests_controller.rb index 98c0cf8b3..36c5493d3 100644 --- a/web/app/controllers/api_maxmind_requests_controller.rb +++ b/web/app/controllers/api_maxmind_requests_controller.rb @@ -7,6 +7,11 @@ class ApiMaxmindRequestsController < ApiController render :json => { :countries => countries }, :status => 200 end + def countriesx + countriesx = MaxMindManager.countriesx() + render :json => { :countriesx => countriesx }, :status => 200 + end + def regions regions = MaxMindManager.regions(params[:country]) if regions && regions.length > 0 diff --git a/web/app/controllers/users_controller.rb b/web/app/controllers/users_controller.rb index 1420b2012..19a574a8f 100644 --- a/web/app/controllers/users_controller.rb +++ b/web/app/controllers/users_controller.rb @@ -439,7 +439,7 @@ class UsersController < ApplicationController @location[:country] = "US" if @location[:country].nil? # right now we only accept US signups for beta - @countries = MaxMindManager.countries() + @countriesx = MaxMindManager.countriesx() # populate regions based on current country @regions = MaxMindManager.regions(@location[:country]) @cities = @location[:state].nil? ? [] : MaxMindManager.cities(@location[:country], @location[:state]) diff --git a/web/app/views/users/new.html.erb b/web/app/views/users/new.html.erb index 064d7a335..13d101d5f 100644 --- a/web/app/views/users/new.html.erb +++ b/web/app/views/users/new.html.erb @@ -38,9 +38,9 @@ <%= f.label :country, "Country:" %> diff --git a/web/config/routes.rb b/web/config/routes.rb index 64d0dc9f7..cdf6443c5 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -316,6 +316,7 @@ SampleApp::Application.routes.draw do # Location lookups match '/countries' => 'api_maxmind_requests#countries', :via => :get + match '/countriesx' => 'api_maxmind_requests#countriesx', :via => :get match '/regions' => 'api_maxmind_requests#regions', :via => :get match '/cities' => 'api_maxmind_requests#cities', :via => :get match '/isps' => 'api_maxmind_requests#isps', :via => :get diff --git a/web/lib/max_mind_manager.rb b/web/lib/max_mind_manager.rb index 4ed9c8c08..05dfc1033 100644 --- a/web/lib/max_mind_manager.rb +++ b/web/lib/max_mind_manager.rb @@ -67,6 +67,18 @@ class MaxMindManager < BaseManager Country.get_all.map { |c| c.countrycode } end + def self.countriesx() + #ActiveRecord::Base.connection_pool.with_connection do |connection| + # pg_conn = connection.instance_variable_get("@connection") + # pg_conn.exec("SELECT DISTINCT country FROM max_mind_geo ORDER BY country ASC").map do |tuple| + # tuple["country"] + # end + #end + + # returns ordered array of Country objects (countrycode, countryname) + Country.get_all.map { |c| {countrycode: c.countrycode, countryname: c.countryname} } + end + def self.regions(country) #ActiveRecord::Base.connection_pool.with_connection do |connection|