GithubHelp home page GithubHelp logo

robertlowe / country_select Goto Github PK

View Code? Open in Web Editor NEW

This project forked from countries/country_select

1.0 2.0 0.0 254 KB

Gemification of rails's country_select

Home Page: http://country-select-test.herokuapp.com/

License: MIT License

Ruby 100.00%

country_select's Introduction

Rails โ€“ Country Select

Build Status

Provides a simple helper to get an HTML select list of countries using the ISO 3166-1 standard.

While the ISO 3166 standard is a relatively neutral source of country names, it may still offend some users. Developers are strongly advised to evaluate the suitability of this list given their user base.

UPGRADING

An important message about upgrading from 1.x

Reporting issues

Open an issue on the issue tracker. Ideally provide versions used, and code example that demonstrates the issue.

Installation

Install as a gem using

gem install country_select

Or put the following in your Gemfile

gem 'country_select', '~> 4.0'

If you don't want to require sort_alphabetical (it depends on unicode_utils which is known to use lots of memory) you can opt out of using it as follows:

gem 'country_select', require: 'country_select_without_sort_alphabetical'

Usage

Within form_for you can use this select like other form elements:

<%= form_for User.new, url: root_url do |f| %>
  <%= f.country_select :country_code %>
<% end %>

Simple use supplying model and attribute as parameters:

country_select("user", "country")

Supplying priority countries to be placed at the top of the list:

country_select("user", "country", priority_countries: ["GB", "FR", "DE"])

Supplying only certain countries:

country_select("user", "country", only: ["GB", "FR", "DE"])

Discarding certain countries:

country_select("user", "country", except: ["GB", "FR", "DE"])

Pre-selecting a particular country:

country_select("user", "country", selected: "GB")

Changing the divider when priority_countries is active.

country_select("user", "country", priority_countries: ["AR", "US"], priority_countries_divider: "~~~~~~")

Using existing select options:

country_select("user", "country", include_blank: true)
country_select("user", "country", { include_blank: 'Select a country' }, { class: 'country-select-box' })

Supplying additional html options:

country_select("user", "country", { priority_countries: ["GB", "FR"], selected: "GB" }, { class: 'form-control', data: { attribute: "value" } })

Using a custom formatter

You can define a custom formatter which will receive an ISO3166::Country

# config/initializers/country_select.rb

# Return a string to customize the text in the <option> tag, `value` attribute will remain unchanged
CountrySelect::FORMATS[:with_alpha2] = lambda do |country|
  "#{country.name} (#{country.alpha2})"
end

# Return an array to customize <option> text, `value` and other HTML attributes
CountrySelect::FORMATS[:with_data_attrs] = lambda do |country|
  [
    country.name,
    country.alpha2,
    {
      'data-country-code' => country.country_code,
      'data-alpha3' => country.alpha3
    }
  ]
end
country_select("user", "country", format: :with_alpha2)
country_select("user", "country", format: :with_data_attrs)

ISO 3166-1 alpha-2 codes

The option tags use ISO 3166-1 alpha-2 codes as values and the country names as display strings. For example, the United States would appear as <option value="US">United States of America</option>

Country names are automatically localized based on the value of I18n.locale thanks to the wonderful countries gem.

Current translations include:

  • en
  • de
  • es
  • fr
  • it
  • ja
  • nl

In the event a translation is not available, it will revert to the globally assigned locale (by default, "en").

This is the only way to use country_select as of version 2.0. It is the recommended way to store your country data since it will be resistant to country names changing.

The locale can be overridden locally:

country_select("user", "country_code", locale: 'es')

Getting the Country Name from the countries gem

class User < ActiveRecord::Base

  # Assuming country_select is used with User attribute `country_code`
  # This will attempt to translate the country name and use the default
  # (usually English) name if no translation is available
  def country_name
    country = ISO3166::Country[country_code]
    country.translations[I18n.locale.to_s] || country.name
  end

end

Example Application

An example Rails application demonstrating the different options is available at scudco/country_select_test. The relevant view files live here.

Contributing

Tests

bundle
bundle exec rake

Updating gemfiles

The default rake task will run the tests against multiple versions of Rails. That means the gemfiles need occasional updating, especially when changing the dependencies in the gemspec.

for i in gemfiles/*.gemfile
do
BUNDLE_GEMFILE=$i bundle install --no-deployment
done

Copyright (c) 2008 Michael Koziarski, released under the MIT license

country_select's People

Contributors

auzroz avatar barrywoolgar avatar bradleypriest avatar cbetta avatar dgilperez avatar espen avatar fschwahn avatar georgepalmer avatar j0an avatar jjballano avatar joemsak avatar joshgoebel avatar kennethkalmer avatar lenart avatar michaelem avatar mikerogers0 avatar mschmidt avatar nicolasleger avatar nzkoz avatar oliverklee avatar oz avatar paolodona avatar petergoldstein avatar pwim avatar romaingweb avatar rposborne avatar scudco avatar slbug avatar sreuterle avatar stefanpenner avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.