rails-countries 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +25 -3
- data/config/locales/en.yml +1 -0
- data/config/locales/es.yml +1 -0
- data/config/locales/pt-BR.yml +1 -0
- data/lib/rails-countries/engine.rb +2 -0
- data/lib/rails-countries/helpers.rb +11 -1
- data/lib/rails-countries/validators.rb +11 -0
- data/lib/rails-countries/version.rb +1 -1
- data/lib/rails-countries.rb +0 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3117edd5ad8690c96fcb03b7f35e7ae40ca78aaf835ae4f9e82c8ce386081a0a
|
4
|
+
data.tar.gz: 489cc1d19dfccbe35a52ffc6d3f5044dc697900bdf787eadc15713bda44f788b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4203664b3d4e3e84bd8105c35403015e2d522dfc2d8bda6e338826987bcba381a86dab739b62b42bdad2844b0da7df34a621063ebbbdfcacabf62f959e5a638a
|
7
|
+
data.tar.gz: 2945505945c68052c151266d6d966a41a467120aa819dc5201b4ec77f08b371c13d7f2a1850d41b665a6b5cf6fe5b3d2d3d8c78fc4def155d101c99ba7038ac0
|
data/README.md
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
Integration between Rails and [countries](https://github.com/hexorx/countries) gem.
|
4
4
|
|
5
|
-
[](https://badge.fury.io/rb/rails-countries)
|
6
|
+
[]()
|
7
|
+
[]()
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -30,6 +30,10 @@ class Person < ActiveRecord::Base
|
|
30
30
|
validates :my_attribute, countries_alpha2: true
|
31
31
|
# or
|
32
32
|
validates_countries_alpha2_of :my_attribute
|
33
|
+
|
34
|
+
validates :another_attribute, countries_alpha3: true
|
35
|
+
# or
|
36
|
+
validates_countries_alpha3_of :another_attribute
|
33
37
|
end
|
34
38
|
```
|
35
39
|
|
@@ -41,6 +45,10 @@ class Person
|
|
41
45
|
validates :my_attribute, countries_alpha2: true
|
42
46
|
# or
|
43
47
|
validates_countries_alpha2_of :my_attribute
|
48
|
+
|
49
|
+
validates :another_attribute, countries_alpha3: true
|
50
|
+
# or
|
51
|
+
validates_countries_alpha3_of :another_attribute
|
44
52
|
end
|
45
53
|
```
|
46
54
|
|
@@ -50,18 +58,27 @@ end
|
|
50
58
|
```erb
|
51
59
|
<%= i18n_country_name_by_alpha2('US') %>
|
52
60
|
<%= i18n_country_name_by_alpha2('US', 'pt-BR') %>
|
61
|
+
|
62
|
+
<%= i18n_country_name_by_alpha3('USA') %>
|
63
|
+
<%= i18n_country_name_by_alpha3('USA', 'pt-BR') %>
|
53
64
|
```
|
54
65
|
|
55
66
|
#### Rails Form helpers
|
56
67
|
```erb
|
57
68
|
<%= select_tag(:my_attribute, countries_alpha2_options) %>
|
58
69
|
<%= select_tag(:my_attribute, countries_alpha2_options('pt-BR')) %>
|
70
|
+
|
71
|
+
<%= select_tag(:another_attribute, countries_alpha3_options) %>
|
72
|
+
<%= select_tag(:another_attribute, countries_alpha3_options('pt-BR')) %>
|
59
73
|
```
|
60
74
|
|
61
75
|
#### SimpleForm
|
62
76
|
```erb
|
63
77
|
<%= f.input :my_attribute, collection: countries_alpha2_options %>
|
64
78
|
<%= f.input :my_attribute, collection: countries_alpha2_options('pt-BR') %>
|
79
|
+
|
80
|
+
<%= f.input :my_attribute, collection: countries_alpha3_options %>
|
81
|
+
<%= f.input :my_attribute, collection: countries_alpha3_options('pt-BR') %>
|
65
82
|
```
|
66
83
|
|
67
84
|
## I18n
|
@@ -73,4 +90,9 @@ en:
|
|
73
90
|
errors:
|
74
91
|
messages:
|
75
92
|
countries_alpha2_invalid: '%{alpha2} is not valid'
|
93
|
+
countries_alpha3_invalid: '%{alpha3} is not valid'
|
76
94
|
```
|
95
|
+
|
96
|
+
## Contribute it!
|
97
|
+
|
98
|
+
Help us to improve the features and translations of this gem! Your contribution will be welcome :tada:
|
data/config/locales/en.yml
CHANGED
data/config/locales/es.yml
CHANGED
data/config/locales/pt-BR.yml
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'countries'
|
2
|
+
|
1
3
|
module RailsCountries
|
2
4
|
module ActionView
|
3
5
|
module Helpers
|
@@ -5,8 +7,16 @@ module RailsCountries
|
|
5
7
|
ISO3166::Country.all.map { |country| [country.translation(locale || I18n.locale), country.alpha2] }
|
6
8
|
end
|
7
9
|
|
10
|
+
def countries_alpha3_options(locale = nil)
|
11
|
+
ISO3166::Country.all.map { |country| [country.translation(locale || I18n.locale), country.alpha3] }
|
12
|
+
end
|
13
|
+
|
8
14
|
def i18n_country_name_by_alpha2(alpha2, locale = nil)
|
9
|
-
ISO3166::Country.new(alpha2)
|
15
|
+
ISO3166::Country.new(alpha2)&.translation(locale || I18n.locale)
|
16
|
+
end
|
17
|
+
|
18
|
+
def i18n_country_name_by_alpha3(alpha3, locale = nil)
|
19
|
+
ISO3166::Country.find_country_by_alpha3(alpha3)&.translation(locale || I18n.locale)
|
10
20
|
end
|
11
21
|
end
|
12
22
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'countries'
|
1
2
|
require 'active_model'
|
2
3
|
|
3
4
|
module ActiveModel
|
@@ -8,10 +9,20 @@ module ActiveModel
|
|
8
9
|
end
|
9
10
|
end
|
10
11
|
|
12
|
+
class CountriesAlpha3Validator < EachValidator
|
13
|
+
def validate_each(record, attribute, value)
|
14
|
+
record.errors.add(attribute, :countries_alpha3_invalid, alpha3: value) if value.present? && (value.is_a?(String) || value.is_a?(Symbol)) && !ISO3166::Country.all.map(&:alpha3).include?(value.to_s)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
11
18
|
module HelperMethods
|
12
19
|
def validates_countries_alpha2_of(*attributes)
|
13
20
|
validates_with CountriesAlpha2Validator, _merge_attributes(attributes)
|
14
21
|
end
|
22
|
+
|
23
|
+
def validates_countries_alpha3_of(*attributes)
|
24
|
+
validates_with CountriesAlpha3Validator, _merge_attributes(attributes)
|
25
|
+
end
|
15
26
|
end
|
16
27
|
end
|
17
28
|
end
|
data/lib/rails-countries.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-countries
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pedro Furtado
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-07-
|
11
|
+
date: 2019-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|