Skip to content

Commit

Permalink
Prefixes phone number based on selected country
Browse files Browse the repository at this point in the history
as requested in https://www.notion.so/Apply-Form-pre-fill-country-code-13efee17e5754d4fa43690f2d80f7a25,
this diff enables prefixing of volunteer Phone field with international
country code based on selected country to speed up the submission
process.

- adds a new Gem [^1] for fetching country codes
- adds a new route for sending GET requests with alpha2 country params
  and getting back the country code
- adds a feature spec for checking the implementation. This spec is
  currently disabled because our JS is not loading in test env. Filed a
  separate issue here: #61

[^1]: [Countries Gem](https://github.com/hexorx/countries).
  • Loading branch information
dimanyc committed May 15, 2020
1 parent 49c8ff8 commit 430940c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ gem 'bootsnap', '>= 1.4.2', require: false

gem 'city-state', '>= 0.1.0'
gem 'country_state_select', '>= 3.0.5'
gem 'countries'
gem 'simple_form', '>= 5.0.2'
gem 'bootstrap', '~>4.3.1'
gem 'inline_svg', '>= 1.7.1'
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ GEM
rubyzip (>= 1.1)
coderay (1.1.2)
concurrent-ruby (1.1.6)
countries (3.0.1)
i18n_data (~> 0.10.0)
sixarm_ruby_unaccent (~> 1.1)
unicode_utils (~> 1.4)
country_state_select (3.0.5)
city-state
rails
Expand All @@ -105,6 +109,7 @@ GEM
activesupport (>= 4.2.0)
i18n (1.8.2)
concurrent-ruby (~> 1.0)
i18n_data (0.10.0)
inline_svg (1.7.1)
activesupport (>= 3.0)
nokogiri (>= 1.6)
Expand Down Expand Up @@ -216,6 +221,7 @@ GEM
simple_form (5.0.2)
actionpack (>= 5.0)
activemodel (>= 5.0)
sixarm_ruby_unaccent (1.2.0)
spring (2.1.0)
spring-watcher-listen (2.0.1)
listen (>= 2.7, < 4.0)
Expand All @@ -235,6 +241,7 @@ GEM
turbolinks-source (5.2.0)
tzinfo (1.2.6)
thread_safe (~> 0.1)
unicode_utils (1.4.0)
web-console (4.0.1)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand Down Expand Up @@ -265,6 +272,7 @@ DEPENDENCIES
byebug
capybara (>= 2.15)
city-state (>= 0.1.0)
countries
country_state_select (>= 3.0.5)
dotenv-rails
factory_bot_rails
Expand Down
14 changes: 14 additions & 0 deletions app/controllers/countries_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CountriesController < ApplicationController
def show
country = ISO3166::Country.find_country_by_alpha2(country_params[:country])
if country
render partial: "country_code", locals: {
country_code: country.country_code,
}
end
end

def country_params
params.require(:volunteer).permit(:country)
end
end
5 changes: 5 additions & 0 deletions app/views/countries/_country_code.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var phoneInput = document.getElementsByName("volunteer[phone]")[0]
var countryCode = "+#{ISO3166::Country.find_country_by_alpha2(e.target.value)}"
phoneInput.value = "+" + <%= country_code %>


9 changes: 8 additions & 1 deletion app/views/volunteers/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
<%= f.input :country,
label: false,
collection: countries,
:selected => 'US'
:selected => 'US',
input_html: {
data: {
country_code: "true",
remote: :true,
url: "/country-code",
}
}
%>

<%= f.input :state,
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
get '/about', to: 'pages#about'
get '/terms', to: 'pages#terms'
get '/privacy', to: 'pages#privacy'

get'/country-code', to: "countries#show"
end
15 changes: 15 additions & 0 deletions spec/features/volunteer_registering_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "rails_helper"

RSpec.describe "A volunteer fills out registration form", js: true do
scenario "their phone number if prefixed with country code" do
pending "fix node sass error in test"
visit "/"
expect(page).to have_content(I18n.t("home.index.section-titles.form"))
expect do
select "Romania", from: "volunteer[country]"
end.
to change { find_field("volunteer[phone]").value }.
from("").
to("+40")
end
end
6 changes: 3 additions & 3 deletions spec/views/layouts/application.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
expect(rendered).to match t("layouts.application.faq_nav_link")
# Footer is temporarily looking for contributors
# expect(rendered).to match t("layouts.application.footer.text")
expect(rendered).to match t("layouts.application.footer.about")
expect(rendered).to match t("layouts.application.footer.terms")
expect(rendered).to match t("layouts.application.footer.privacy")
expect(rendered).to match t("common.about")
expect(rendered).to match t("common.terms")
expect(rendered).to match t("common.privacy")
end
end

0 comments on commit 430940c

Please sign in to comment.