Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a convenient way to get a hash or array of countries and their corresponding codes? #46

Closed
barmstrong opened this issue Jun 12, 2012 · 8 comments

Comments

@barmstrong
Copy link

I'm trying to build a select menu for the user to choose their country, and auto-populate the country code.

Couldn't see an easy way to get a hash or array of these values though to build the select. Any ideas? Thanks for the great gem btw.

@floere
Copy link
Owner

floere commented Jun 12, 2012

Hi Brian,

Thanks, and thanks for the issue. I am not sure what you exactly need. Is it the names of the countries, mapped to country codes, or "Switzerland (+41)", or just the country code itself in the select ("41")?

Cheers,
Florian

@barmstrong
Copy link
Author

Yep best would be country names mapped to codes like {"Switzerland" => 41, "United States" => 1} etc. But anything close to that I could probably make it work.

@floere
Copy link
Owner

floere commented Jun 13, 2012

At the moment, Phony does not save any country names, since the focus is only on the numbers themselves. I did consider it, but including the names would bring with it a whole host of issues (multi-language names etc.).

Are there any country gems that have this mapping ready? (Maybe the "countries" gem?) If not, maybe we can work something out, perhaps a 3166 to country code mapping, to be required separately in Phony.

@barmstrong
Copy link
Author

The countries gem has a country_select. It looks like you can get the country names mapped to country code with ISO3166::Country::Names. Their yaml file has the country_codes also, I'll see if I can find a way to pull the hash I need out of it tomorrow. Thanks @floere !

@floere
Copy link
Owner

floere commented Jun 13, 2012

Hi Brian,

Glad to hear it! I'll think about doing a built-in solution.

Cheers,
Florian

@barmstrong
Copy link
Author

Hey, I figured it out with the countries gem. You can do:

ISO3166::Country::Data.collect{|k,v| [v['name'], v['country_code']]}.sort

Since it's semi-expensive I cached it in a helper:

def cached_country_codes
  Rails.cache.fetch('cached_country_codes', expires_in: 1.hour) do
    ISO3166::Country::Data.collect{|k,v| [v['name'], v['country_code']]}.sort
  end
end

Then you can call:

<%= f.select :country_code, options_for_select(cached_country_codes) %>

@floere
Copy link
Owner

floere commented Jun 14, 2012

Great! I wonder about the expires_in – do countries change every hour? ;) But seriously: Good stuff! Cheers

@barmstrong
Copy link
Author

Thanks! You're right, prob a bit overkill :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants