Skip to content

Commit

Permalink
1170 - Country State Select Implementation (#1209)
Browse files Browse the repository at this point in the history
* Removed city-state gem and its dependencies

* Implementaion for loading country state from YML

* Fix to show selected state value in droplist

* Lint fix

* Lint fix

* Test case fix

* Updated code to show country and state list

* Controller test for country_state_controller

* Lint fix

* Lint fix

* Lint fix

* Country and State list for new account request form

* Move location fields to partial

* Rename location field partial
  • Loading branch information
sarvaiyanidhi authored Dec 18, 2024
1 parent a3073cf commit 9d0c57d
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 119 deletions.
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ gem "active_link_to"
# needed for pagination
gem "pagy"

# Adds location data for cities and states around the world
gem "city-state", "~> 1.1.0"

# Adds a simple way to fetch with Javascript
gem "requestjs-rails", "~> 0.0.12"

Expand Down
4 changes: 0 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ GEM
xpath (~> 3.2)
childprocess (5.1.0)
logger (~> 1.5)
city-state (1.1.0)
rubyzip (>= 2.3)
coderay (1.1.3)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
Expand Down Expand Up @@ -472,7 +470,6 @@ GEM
ruby-next-core (1.0.3)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
sass-embedded (1.83.0)
google-protobuf (~> 4.28)
rake (>= 13)
Expand Down Expand Up @@ -586,7 +583,6 @@ DEPENDENCIES
brakeman
bugsnag (~> 6.27)
capybara
city-state (~> 1.1.0)
cuprite
dartsass-rails
debug
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/country_states_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CountryStatesController < ApplicationController
skip_before_action :authenticate_user!
skip_verify_authorized only: %i[index]

def index
country = params[:country]
@target = params[:target]
@name = params[:name]
@selected_state = params[:province_state]

@states = CS[country.to_sym][:states].invert

respond_to do |format|
format.turbo_stream
end
end
end
9 changes: 0 additions & 9 deletions app/controllers/states_controller.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/javascript/controllers/country_state_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export default class extends Controller {

updateStates() {
const path = this.countryTarget.dataset.path;

const params = new URLSearchParams({
country: this.countryTarget.value,
target: this.stateTarget.id,
name: this.stateTarget.name,
province_state: this.stateTarget.value
});

get(`${path}?${params}`, { responseKind: "turbo-stream" });
Expand Down
3 changes: 3 additions & 0 deletions app/views/country_states/index.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_stream.replace @target do %>
<%= select_tag @name, options_for_select(@states, @selected_state), 'data-country-state-target': 'state', class: 'form-control' %>
<% end %>
27 changes: 3 additions & 24 deletions app/views/organization_account_requests/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</div>

<div class="row">
<div class="col-md-6 mx-auto card p-5">
<div class="col-md-6 mx-auto card p-5" data-controller="country-state">
<%= bootstrap_form_with model: OrganizationAccountRequest.new, url: organization_account_request_path do |form| %>
<% if @organization_account_request.errors.any? %>
<div class="alert alert-danger mt-1" role="alert">
Expand Down Expand Up @@ -65,29 +65,8 @@
</div>

<div class="col-lg-6">
<div class="form-group">
<%= form.select :country,
CS.countries.invert,
{ prompt: "Please select" },
data: { path: country_states_path(:country) },
required: true,
class: 'form-control'
%>
</div>

<div class="form-group">
<%= form.text_field :province_state,
label: t('activerecord.attributes.location.province_state'),
required: true,
class: 'form-control'
%>
</div>

<div class="form-group">
<%= form.text_field :city_town,
label: t('activerecord.attributes.location.city_town'),
class: 'form-control'
%>
<div class="input-group">
<%= render 'shared/location_fields', form: form %>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,7 @@
<!-- Nested Form for Locations Table -->
<div class="form-group bigger" data-controller="country-state">
<%= person_form.fields_for :location, user.person.location do |location_form| %>
<!-- Country Select -->
<%= location_form.select :country,
CS.countries.invert,
{ prompt: "", selected: default_country },
'data-path': country_states_path(:country),
'data-country-state-target': 'country',
'data-action': 'change->country-state#updateStates',
'data-initial-value': location_form.object.country,
required: false,
class: 'form-control' %>

<!-- Province/State Select -->
<%= location_form.select :province_state,
CS.states(location_form.object.country).invert,
{ prompt: "" },
'data-country-state-target': 'state',
required: false,
class: 'form-control' %>

<!-- City/Town Text Field -->
<%= location_form.text_field :city_town,
required: false,
class: 'form-control' %>
<%= render 'shared/location_fields', form: location_form %>
<% end %>
</div>
</div>
Expand Down
22 changes: 1 addition & 21 deletions app/views/organizations/staff/fosterers/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,7 @@
<!-- Nested Form for Locations Table -->
<div class="form-group bigger" data-controller="country-state">
<%= f.fields_for :location, fosterer.location do |location_form| %>
<!-- Country Select -->
<%= location_form.select :country,
CS.countries.invert,
{ prompt: "", selected: default_country },
"data-path": country_states_path(:country),
"data-country-state-target": "country",
"data-action": "change->country-state#updateStates",
"data-initial-value": location_form.object.country,
required: false,
class: "form-control" %>
<!-- Province/State Select -->
<%= location_form.select :province_state,
CS.states(location_form.object.country).invert,
{ prompt: "" },
"data-country-state-target": "state",
required: false,
class: "form-control" %>
<!-- City/Town Text Field -->
<%= location_form.text_field :city_town,
required: false,
class: "form-control" %>
<%= render 'shared/location_fields', form: location_form %>
<% end %>
</div>
</div>
Expand Down
22 changes: 1 addition & 21 deletions app/views/organizations/staff/organizations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,7 @@
<div class="form-group bigger" data-controller="country-state">
<% location = organization.locations.last || organization.locations.build %>
<%= form.fields_for :locations, location do |location_form| %>
<!-- Country Select -->
<%= location_form.select :country,
CS.countries.invert,
{ prompt: "Please select" },
'data-path': country_states_path(:country),
'data-country-state-target': 'country',
'data-action': 'change->country-state#updateStates',
'data-initial-value': location_form.object.country,
required: true,
class: 'form-control' %>

<!-- Province/State Select -->
<%= location_form.select :province_state,
CS.states(location_form.object.country).invert,
{ prompt: "Please select" },
'data-country-state-target': 'state',
required: true,
class: 'form-control' %>

<!-- City/Town Text Field -->
<%= location_form.text_field :city_town, class: 'form-control' %>
<%= render 'shared/location_fields', form: location_form %>
<% end %>
</div>
</div>
Expand Down
25 changes: 25 additions & 0 deletions app/views/shared/_location_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- Country Select -->
<%= form.select :country,
CS.keys.index_with { |abbr| CS.dig(abbr, :name) }.invert,
{ prompt: "Please select" },
{
'data-path': country_states_path,
'data-country-state-target': 'country',
'data-action': 'change->country-state#updateStates',
required: true,
class: 'form-control'
} %>
<!-- Province/State Select -->
<%= form.select :province_state,
form.object.country.present? ?
CS.dig(form.object.country.to_sym, :states)&.map { |abbr, name| [:name, abbr] } : [],
{ prompt: "Please select" },
{
'data-country-state-target': 'state',
required: true,
class: 'form-control'
} %>
<!-- City/Town Text Field -->
<%= form.text_field :city_town,
required: false,
class: "form-control" %>
3 changes: 0 additions & 3 deletions app/views/states/index.turbo_stream.erb

This file was deleted.

105 changes: 105 additions & 0 deletions config/countries_states.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
US:
name: "United States"
states:
AK: "Alaska"
AL: "Alabama"
AR: "Arkansas"
AZ: "Arizona"
CA: "California"
CO: "Colorado"
CT: "Connecticut"
DC: "District of Columbia"
DE: "Delaware"
FL: "Florida"
GA: "Georgia"
HI: "Hawaii"
IA: "Iowa"
ID: "Idaho"
IL: "Illinois"
IN: "Indiana"
KS: "Kansas"
KY: "Kentucky"
LA: "Louisiana"
MA: "Massachusetts"
MD: "Maryland"
ME: "Maine"
MI: "Michigan"
MN: "Minnesota"
MO: "Missouri"
MS: "Mississippi"
MT: "Montana"
NC: "North Carolina"
ND: "North Dakota"
NE: "Nebraska"
NH: "New Hampshire"
NJ: "New Jersey"
NM: "New Mexico"
NV: "Nevada"
NY: "New York"
OH: "Ohio"
OK: "Oklahoma"
OR: "Oregon"
PA: "Pennsylvania"
RI: "Rhode Island"
SC: "South Carolina"
SD: "South Dakota"
TN: "Tennessee"
TX: "Texas"
UT: "Utah"
VA: "Virginia"
VT: "Vermont"
WA: "Washington"
WI: "Wisconsin"
WV: "West Virginia"
WY: "Wyoming"
CA:
name: "Canada"
states:
AB: "Alberta"
BC: "British Columbia"
MB: "Manitoba"
NB: "New Brunswick"
NL: "Newfoundland and Labrador"
NS: "Nova Scotia"
NT: "Northwest Territories"
NU: "Nunavut"
OT: "Ontario"
PE: "Prince Edward Island"
QC: "Quebec"
SK: "Saskatchewan"
YT: "Yukon"
MX:
name: "Mexico"
states:
AGU: "Aguascalientes"
BCN: "Baja California"
BCS: "Baja California Sur"
CAM: "Campeche"
CHH: "Chihuahua"
CHP: "Chiapas"
CMX: "Mexico City"
COA: "Coahuila"
COL: "Colima"
DUR: "Durango"
GRO: "Guerrero"
GUA: "Guanajuato"
HID: "Hidalgo"
JAL: "Jalisco"
MEX: "México"
MIC: "Michoacán"
MOR: "Morelos"
NAY: "Nayarit"
NLE: "Nuevo León"
OAX: "Oaxaca"
PUE: "Puebla"
QUE: "Querétaro"
ROO: "Quintana Roo"
SIN: "Sinaloa"
SLP: "San Luis Potosí"
SON: "Sonora"
TAB: "Tabasco"
TAM: "Tamaulipas"
TLA: "Tlaxcala"
VER: "Veracruz"
YUC: "Yucatán"
ZAC: "Zacatecas"
6 changes: 6 additions & 0 deletions config/initializers/countries_states.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
begin
CS = YAML.safe_load_file(Rails.root.join("config/countries_states.yml"), symbolize_names: true).freeze
rescue => e
puts "Error loading countries_states.yml: #{e.message}"
CS = {}.freeze
end
4 changes: 1 addition & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
}

# Application Scope
resources :countries, only: [] do
resources :states, only: [:index]
end
resources :country_states, only: [:index]

root "root#index"
get "/up", to: "root#up" # Health check endpoint to let Kamal know the app is up
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
require "test_helper"

class StatesControllerTest < ActionDispatch::IntegrationTest
class CountryStatesControllerTest < ActionDispatch::IntegrationTest
test "should return turbo stream with the states in it" do
adopter = create(:adopter)
sign_in adopter

name = "adopter[address_attributes][state]"
target = "adopter_foster_profile_location_attributes_province_state"

get country_states_path(:country),
get country_states_path,
headers: {"Accept" => "text/vnd.turbo-stream.html"},
params: {country: "CA", target:, name:}
params: {country: "CA", target:, name:, province_state: "BC"}

assert_response :success
assert_select "turbo-stream[action='replace'][target='#{target}']" do
Expand Down
Loading

0 comments on commit 9d0c57d

Please sign in to comment.