{"id":13747271,"url":"https://github.com/countries/country_select","last_synced_at":"2025-05-09T08:32:06.033Z","repository":{"id":2292194,"uuid":"3250243","full_name":"countries/country_select","owner":"countries","description":"Gemification of rails's country_select","archived":false,"fork":false,"pushed_at":"2025-01-02T17:40:49.000Z","size":377,"stargazers_count":708,"open_issues_count":4,"forks_count":198,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-01-02T17:45:41.298Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://country-select-demo.onrender.com","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/countries.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-01-23T20:48:11.000Z","updated_at":"2025-01-02T05:02:32.000Z","dependencies_parsed_at":"2024-02-17T12:25:22.850Z","dependency_job_id":"ef0a4caf-c5ca-486f-a8a6-ced580cc7eb4","html_url":"https://github.com/countries/country_select","commit_stats":{"total_commits":271,"total_committers":51,"mean_commits":5.313725490196078,"dds":0.6088560885608856,"last_synced_commit":"e1ef369536da10e29b11618aed7c77ceb4e1c587"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/countries%2Fcountry_select","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/countries%2Fcountry_select/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/countries%2Fcountry_select/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/countries%2Fcountry_select/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/countries","download_url":"https://codeload.github.com/countries/country_select/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253217080,"owners_count":21873013,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T06:01:23.449Z","updated_at":"2025-05-09T08:32:05.775Z","avatar_url":"https://github.com/countries.png","language":"Ruby","funding_links":[],"categories":["Ruby","Forms"],"sub_categories":[],"readme":"# Rails – Country Select\n[![Gem Version](https://badge.fury.io/rb/country_select.svg)](https://badge.fury.io/rb/country_select) [![build](https://github.com/countries/country_select/actions/workflows/test.yml/badge.svg)](https://github.com/countries/country_select/actions/workflows/test.yml)\n[![Code Climate](https://codeclimate.com/github/countries/country_select.svg)](https://codeclimate.com/github/countries/country_select)\n[![CodeQL](https://github.com/countries/country_select/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/countries/country_select/actions/workflows/codeql-analysis.yml)\n\n\nProvides a simple helper to get an HTML select list of countries using the\n[ISO 3166-1 standard](https://en.wikipedia.org/wiki/ISO_3166-1).\n\nWhile the ISO 3166 standard is a relatively neutral source of country\nnames, it may still offend some users. Developers are strongly advised\nto evaluate the suitability of this list given their user base.\n\n## UPGRADING\n\n[**An important message about upgrading from 1.x**](UPGRADING.md)\n\n## Reporting issues\n\nOpen an issue on the [issue tracker](https://github.com/countries/country_select/issues/new). Ideally provide versions used, and code example that demonstrates the issue.\n\n## Installation\n\nInstall as a gem using\n\n```shell\ngem install country_select\n```\nOr put the following in your Gemfile\n\n```ruby\ngem 'country_select', '~\u003e 8.0'\n```\n\n## Usage\n\nWithin `form_for` you can use this select like other form elements:\n\n```ruby\n\u003c%= form_for User.new, url: root_url do |f| %\u003e\n  \u003c%= f.country_select :country_code %\u003e\n\u003c% end %\u003e\n```\n\nSimple use supplying model and attribute as parameters:\n\n```ruby\ncountry_select(\"user\", \"country\")\n```\n\nSupplying priority countries to be placed at the top of the list:\n\n```ruby\ncountry_select(\"user\", \"country\", priority_countries: [\"GB\", \"FR\", \"DE\"]) # Countries will be sorted by name according to the current locale\n# or\ncountry_select(\"user\", \"country\", priority_countries: [\"GB\", \"FR\", \"DE\"], sort_provided: false) # Countries will be displayed is the provided order\n```\n\nSupplying only certain countries:\n\n```ruby\ncountry_select(\"user\", \"country\", only: [\"GB\", \"FR\", \"DE\"]) # Countries will be sorted by name according to the current locale\n# or\ncountry_select(\"user\", \"country\", only: [\"GB\", \"FR\", \"DE\"], sort_provided: false) # Countries will be displayed is the provided order\n```\n\nDiscarding certain countries:\n\n```ruby\ncountry_select(\"user\", \"country\", except: [\"GB\", \"FR\", \"DE\"])\n```\n\nPre-selecting a particular country:\n\n```ruby\ncountry_select(\"user\", \"country\", selected: \"GB\")\n```\n\nChanging the divider when priority_countries is active.\n```ruby\ncountry_select(\"user\", \"country\", priority_countries: [\"AR\", \"US\"], priority_countries_divider: \"~~~~~~\")\n```\n\nUsing existing `select` options:\n```ruby\ncountry_select(\"user\", \"country\", include_blank: true)\ncountry_select(\"user\", \"country\", { include_blank: 'Select a country' }, { class: 'country-select-box' })\n```\n\nSupplying additional html options:\n\n```ruby\ncountry_select(\"user\", \"country\", { priority_countries: [\"GB\", \"FR\"], selected: \"GB\" }, { class: 'form-control', data: { attribute: \"value\" } })\n```\n\n### Using a custom formatter\n\nYou can define a custom formatter which will receive an\n[`ISO3166::Country`](https://github.com/countries/countries/blob/master/lib/countries/country.rb)\n```ruby\n# config/initializers/country_select.rb\n\n# Return a string to customize the text in the \u003coption\u003e tag, `value` attribute will remain unchanged\nCountrySelect::FORMATS[:with_alpha2] = lambda do |country|\n  \"#{country.iso_short_name} (#{country.alpha2})\"\nend\n\n# Return an array to customize \u003coption\u003e text, `value` and other HTML attributes\nCountrySelect::FORMATS[:with_data_attrs] = lambda do |country|\n  [\n    country.iso_short_name,\n    country.alpha2,\n    {\n      'data-country-code' =\u003e country.country_code,\n      'data-alpha3' =\u003e country.alpha3\n    }\n  ]\nend\n```\n\n```ruby\ncountry_select(\"user\", \"country\", format: :with_alpha2)\ncountry_select(\"user\", \"country\", format: :with_data_attrs)\n```\n\n### Using customized defaults\n\nYou can configure overridable defaults for `except`, `format`, `locale`,\n`only`, `priority_countries` and `priority_countries_divider` in an initializer.\n\n````ruby\n# config/initializers/country_select.rb\n\nCountrySelect::DEFAULTS[:except] = [ \"ZZ\" ]\n````\n\nThe example would exclude \"ZZ\" from every `country_select` tag, where no `except` option is given.\n\n### ISO 3166-1 alpha-2 codes\nThe `option` tags use ISO 3166-1 alpha-2 codes as values and the country\nnames as display strings. For example, the United States would appear as\n`\u003coption value=\"US\"\u003eUnited States of America\u003c/option\u003e`\n\nCountry names are automatically localized based on the value of\n`I18n.locale` thanks to the wonderful\n[countries gem](https://github.com/countries/countries/).\n\nCurrent translations include:\n\n  * en\n  * de\n  * es\n  * fr\n  * it\n  * ja\n  * nl\n\nIn the event a translation is not available, it will revert to the\nglobally assigned locale (by default, \"en\").\n\nThis is the only way to use `country_select` as of version `2.0`. It\nis the recommended way to store your country data since it will be\nresistant to country names changing.\n\nThe locale can be overridden locally:\n\n```ruby\ncountry_select(\"user\", \"country_code\", locale: 'es')\n```\n\n#### Getting the Country Name from the countries gem\n\n```ruby\nclass User \u003c ActiveRecord::Base\n\n  # Assuming country_select is used with User attribute `country_code`\n  # This will attempt to translate the country name and use the default\n  # (usually English) name if no translation is available\n  def country_name\n    country = ISO3166::Country[country_code]\n    country.translations[I18n.locale.to_s] || country.common_name || country.iso_short_name\n  end\n\nend\n```\n\n## Example Application\n\nAn example Rails application demonstrating the different options is\navailable at [countries/country_select_demo](https://github.com/countries/country_select_demo) and deployed to [country-select-demo.onrender.com](https://country-select-demo.onrender.com).\nThe relevant view files live [here](https://github.com/countries/country_select_demo/tree/master/app/views/demo).\n\n## Contributing\n\n### Tests\n\n```shell\nbundle\nbundle exec rake\n```\n\n### Updating gemfiles\nThe default rake task will run the tests against multiple versions of\nRails. That means the gemfiles need occasional updating, especially when\nchanging the dependencies in the gemspec.\n\n```shell\nfor i in gemfiles/*.gemfile\ndo\nBUNDLE_GEMFILE=$i bundle install --local\ndone\n```\n\nCopyright (c) 2008 Michael Koziarski, released under the MIT license\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcountries%2Fcountry_select","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcountries%2Fcountry_select","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcountries%2Fcountry_select/lists"}