rails-countries 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f547f8e3c479eeabca9f17f7ccc63224151c9fb8b993640b114ed55574d3eb1
4
- data.tar.gz: 3f2afbb5495f46225a1f8dfe96d6f9a0b7ba70b3f788a68c417f80364e64dac6
3
+ metadata.gz: 3117edd5ad8690c96fcb03b7f35e7ae40ca78aaf835ae4f9e82c8ce386081a0a
4
+ data.tar.gz: 489cc1d19dfccbe35a52ffc6d3f5044dc697900bdf787eadc15713bda44f788b
5
5
  SHA512:
6
- metadata.gz: 74a9b9b2f85e3d5c06832d217eea40354f65ad6a5a7d3f9aeb2646d7c99ee985c0d3c528ca201c1810565a3028d19bd7d3df8389fd926a1b7e3714cf2c36fbac
7
- data.tar.gz: 3ae151f4dc5e6b2d14ed3b96d54741b83d1846810ebc2ad5ad808b12d1e91821ed3d1f9b6340e4b8459341e5f1fa2bdfda1764ab4ee06ff961925264aea44119
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
- [![Gem Version](https://badge.fury.io/rb/validates-countries.svg)](https://badge.fury.io/rb/validates-countries)
6
- [![Gem](https://img.shields.io/gem/dt/validates-countries.svg)]()
7
- [![license](https://img.shields.io/github/license/pedrofurtado/validates-countries.svg)]()
5
+ [![Gem Version](https://badge.fury.io/rb/rails-countries.svg)](https://badge.fury.io/rb/rails-countries)
6
+ [![Gem](https://img.shields.io/gem/dt/rails-countries.svg)]()
7
+ [![license](https://img.shields.io/github/license/pedrofurtado/rails-countries.svg)]()
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:
@@ -2,3 +2,4 @@ en:
2
2
  errors:
3
3
  messages:
4
4
  countries_alpha2_invalid: '%{alpha2} is not valid'
5
+ countries_alpha3_invalid: '%{alpha3} is not valid'
@@ -2,3 +2,4 @@ es:
2
2
  errors:
3
3
  messages:
4
4
  countries_alpha2_invalid: '%{alpha2} es invalido'
5
+ countries_alpha3_invalid: '%{alpha3} es invalido'
@@ -2,3 +2,4 @@ pt-BR:
2
2
  errors:
3
3
  messages:
4
4
  countries_alpha2_invalid: '%{alpha2} não é válido'
5
+ countries_alpha3_invalid: '%{alpha3} não é válido'
@@ -1,3 +1,5 @@
1
+ require 'rails'
2
+
1
3
  module RailsCountries
2
4
  class Engine < ::Rails::Engine
3
5
  initializer 'rails-countries.engine' do
@@ -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).translation(locale || I18n.locale)
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
@@ -1,3 +1,3 @@
1
1
  module RailsCountries
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
@@ -1,6 +1,3 @@
1
- require 'countries'
2
- require 'rails'
3
- require 'active_model'
4
1
  require 'rails-countries/version'
5
2
  require 'rails-countries/helpers'
6
3
  require 'rails-countries/engine'
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.0.0
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-18 00:00:00.000000000 Z
11
+ date: 2019-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails