Coder Social home page Coder Social logo

Comments (8)

floere avatar floere commented on August 18, 2024

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

from phony.

barmstrong avatar barmstrong commented on August 18, 2024

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.

from phony.

floere avatar floere commented on August 18, 2024

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.

from phony.

barmstrong avatar barmstrong commented on August 18, 2024

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 !

from phony.

floere avatar floere commented on August 18, 2024

Hi Brian,

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

Cheers,
Florian

from phony.

barmstrong avatar barmstrong commented on August 18, 2024

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) %>

from phony.

floere avatar floere commented on August 18, 2024

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

from phony.

barmstrong avatar barmstrong commented on August 18, 2024

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

from phony.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.