Skip to content

Commit

Permalink
Refactor countries states (#1278)
Browse files Browse the repository at this point in the history
* Refactor countries states to use full name module because the CS module name was breaking in the Org settings context (might interfere with an existing name)

* lint fixes

* update test
  • Loading branch information
kasugaijin authored Dec 18, 2024
1 parent 991f763 commit 9245063
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CountryStatesController < ApplicationController
class CountriesStatesController < ApplicationController
skip_before_action :authenticate_user!
skip_verify_authorized only: %i[index]

Expand All @@ -8,7 +8,7 @@ def index
@name = params[:name]
@selected_state = params[:province_state]

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

respond_to do |format|
format.turbo_stream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class OrganizationsController < Organizations::BaseController
before_action :set_organization, only: %i[edit update]

def edit
@location = @organization.locations.last || @organization.locations.build # polymorphic
end

def update
Expand Down
5 changes: 2 additions & 3 deletions app/views/organizations/staff/organizations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@
<div class="col-lg-6">

<!-- Nested Form for Locations Table -->
<div class="form-group bigger" data-controller="country-state">
<% location = organization.locations.last || organization.locations.build %>
<%= form.fields_for :locations, location do |location_form| %>
<div class="form-group" data-controller="country-state">
<%= form.fields_for :locations, @location do |location_form| %>
<%= render 'shared/location_fields', form: location_form %>
<% end %>
</div>
Expand Down
6 changes: 3 additions & 3 deletions app/views/shared/_location_fields.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!-- Country Select -->
<%= form.select :country,
CS.keys.index_with { |abbr| CS.dig(abbr, :name) }.invert,
COUNTRIES_STATES.keys.index_with { |abbr| COUNTRIES_STATES.dig(abbr, :name) }.invert,
{ prompt: "Please select" },
{
'data-path': country_states_path,
'data-path': countries_states_path,
'data-country-state-target': 'country',
'data-action': 'change->country-state#updateStates',
required: true,
Expand All @@ -12,7 +12,7 @@
<!-- Province/State Select -->
<%= form.select :province_state,
form.object.country.present? ?
CS.dig(form.object.country.to_sym, :states)&.map { |abbr, name| [:name, abbr] } : [],
COUNTRIES_STATES.dig(form.object.country.to_sym, :states)&.map { |abbr, name| [:name, abbr] } : [],
{ prompt: "Please select" },
{
'data-country-state-target': 'state',
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/countries_states.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
begin
CS = YAML.safe_load_file(Rails.root.join("config/countries_states.yml"), symbolize_names: true).freeze
COUNTRIES_STATES = 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
COUNTRIES_STATES = {}.freeze
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}

# Application Scope
resources :country_states, only: [:index]
resources :countries_states, only: [:index]

root "root#index"
get "/up", to: "root#up" # Health check endpoint to let Kamal know the app is up
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/country_states_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require "test_helper"

class CountryStatesControllerTest < ActionDispatch::IntegrationTest
class CountriesStatesControllerTest < 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,
get countries_states_path,
headers: {"Accept" => "text/vnd.turbo-stream.html"},
params: {country: "CA", target:, name:, province_state: "BC"}

Expand Down

0 comments on commit 9245063

Please sign in to comment.