Add custom country to Carmen

681 Views Asked by At

For my Ruby on Rails application, I have the Carmen gem installed in order to access country data and auto create country fields (for example in the mailing address field for the user).

However, for the purposes of this application, Puerto Rico needs to be considered a separate country. Sometime last year Carmen was updated to remove PR from the list of countries but I was wondering if there was a way to add it back.

Example of how I'm currently using Carmen:

<%= f.country_select :country_residence, prompt: ' ' %>
3

There are 3 best solutions below

1
On

To enable PR as a country, create a config/carmen/world.yml file with contents:

---
- alpha_2_code: PR
  _enabled: true

Then create an initializer file config/initializers/carmen.rb and add this to it:

Carmen.append_data_path File.expand_path('../../carmen', __FILE__)

To then remove PR from the US subregion list, create a file config/carmen/world/us.yml with contents:

---
- code: PR
  _enabled: false
0
On

Remove Puerto Rico from the list of countries as it is a subregion - Source change Log of the gem.

What you can do is enable the world.yml in this file.

I did the same and here is my fork

The i installed the gem from my fork

1.9.3-p545 :005 > us = Country.named('Puerto Rico')
 => <#Carmen::Country name="Puerto Rico"> 
1.9.3-p545 :006 > 
0
On

There is some documentation about customizing your list of countries: Overriding structural data.

It looks like you can define a custom overlay with a new definition for the US without Puerto Rico as an outlying area.

Also create a new definition for Puerto Rico with the required ISO codes.