rails-countries 1.0.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 +7 -0
- data/Gemfile +2 -0
- data/LICENSE +21 -0
- data/README.md +76 -0
- data/Rakefile +1 -0
- data/config/locales/en.yml +4 -0
- data/config/locales/es.yml +4 -0
- data/config/locales/pt-BR.yml +4 -0
- data/lib/rails-countries/engine.rb +7 -0
- data/lib/rails-countries/helpers.rb +13 -0
- data/lib/rails-countries/validators.rb +17 -0
- data/lib/rails-countries/version.rb +3 -0
- data/lib/rails-countries.rb +7 -0
- data/rails-countries.gemspec +21 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7f547f8e3c479eeabca9f17f7ccc63224151c9fb8b993640b114ed55574d3eb1
|
4
|
+
data.tar.gz: 3f2afbb5495f46225a1f8dfe96d6f9a0b7ba70b3f788a68c417f80364e64dac6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 74a9b9b2f85e3d5c06832d217eea40354f65ad6a5a7d3f9aeb2646d7c99ee985c0d3c528ca201c1810565a3028d19bd7d3df8389fd926a1b7e3714cf2c36fbac
|
7
|
+
data.tar.gz: 3ae151f4dc5e6b2d14ed3b96d54741b83d1846810ebc2ad5ad808b12d1e91821ed3d1f9b6340e4b8459341e5f1fa2bdfda1764ab4ee06ff961925264aea44119
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) Pedro Felipe de Azevedo Furtado
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# rails-countries
|
2
|
+
|
3
|
+
Integration between Rails and [countries](https://github.com/hexorx/countries) gem.
|
4
|
+
|
5
|
+
[](https://badge.fury.io/rb/validates-countries)
|
6
|
+
[]()
|
7
|
+
[]()
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'rails-countries'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install rails-countries
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### ActiveRecord models
|
28
|
+
```ruby
|
29
|
+
class Person < ActiveRecord::Base
|
30
|
+
validates :my_attribute, countries_alpha2: true
|
31
|
+
# or
|
32
|
+
validates_countries_alpha2_of :my_attribute
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
### Plain Old Ruby Objects
|
37
|
+
```ruby
|
38
|
+
class Person
|
39
|
+
include ActiveModel::Model
|
40
|
+
|
41
|
+
validates :my_attribute, countries_alpha2: true
|
42
|
+
# or
|
43
|
+
validates_countries_alpha2_of :my_attribute
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
### Views
|
48
|
+
|
49
|
+
#### Helpers
|
50
|
+
```erb
|
51
|
+
<%= i18n_country_name_by_alpha2('US') %>
|
52
|
+
<%= i18n_country_name_by_alpha2('US', 'pt-BR') %>
|
53
|
+
```
|
54
|
+
|
55
|
+
#### Rails Form helpers
|
56
|
+
```erb
|
57
|
+
<%= select_tag(:my_attribute, countries_alpha2_options) %>
|
58
|
+
<%= select_tag(:my_attribute, countries_alpha2_options('pt-BR')) %>
|
59
|
+
```
|
60
|
+
|
61
|
+
#### SimpleForm
|
62
|
+
```erb
|
63
|
+
<%= f.input :my_attribute, collection: countries_alpha2_options %>
|
64
|
+
<%= f.input :my_attribute, collection: countries_alpha2_options('pt-BR') %>
|
65
|
+
```
|
66
|
+
|
67
|
+
## I18n
|
68
|
+
|
69
|
+
To customize your error messages you can create a locale file like this:
|
70
|
+
|
71
|
+
```yaml
|
72
|
+
en:
|
73
|
+
errors:
|
74
|
+
messages:
|
75
|
+
countries_alpha2_invalid: '%{alpha2} is not valid'
|
76
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module RailsCountries
|
2
|
+
module ActionView
|
3
|
+
module Helpers
|
4
|
+
def countries_alpha2_options(locale = nil)
|
5
|
+
ISO3166::Country.all.map { |country| [country.translation(locale || I18n.locale), country.alpha2] }
|
6
|
+
end
|
7
|
+
|
8
|
+
def i18n_country_name_by_alpha2(alpha2, locale = nil)
|
9
|
+
ISO3166::Country.new(alpha2).translation(locale || I18n.locale)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
module Validations
|
5
|
+
class CountriesAlpha2Validator < EachValidator
|
6
|
+
def validate_each(record, attribute, value)
|
7
|
+
record.errors.add(attribute, :countries_alpha2_invalid, alpha2: value) if value.present? && (value.is_a?(String) || value.is_a?(Symbol)) && !ISO3166::Country.all.map(&:alpha2).include?(value.to_s)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module HelperMethods
|
12
|
+
def validates_countries_alpha2_of(*attributes)
|
13
|
+
validates_with CountriesAlpha2Validator, _merge_attributes(attributes)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails-countries/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'rails-countries'
|
8
|
+
spec.version = RailsCountries::VERSION
|
9
|
+
spec.authors = ['Pedro Furtado']
|
10
|
+
spec.email = ['pedro.felipe.azevedo.furtado@gmail.com']
|
11
|
+
spec.summary = %q{Integration between Rails and countries gem.}
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = 'https://github.com/pedrofurtado/rails-countries'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.require_paths = ['lib']
|
17
|
+
spec.add_dependency 'rails'
|
18
|
+
spec.add_dependency 'countries'
|
19
|
+
spec.add_development_dependency 'bundler'
|
20
|
+
spec.add_development_dependency 'rake'
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-countries
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pedro Furtado
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-07-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: countries
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Integration between Rails and countries gem.
|
70
|
+
email:
|
71
|
+
- pedro.felipe.azevedo.furtado@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- Gemfile
|
77
|
+
- LICENSE
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- config/locales/en.yml
|
81
|
+
- config/locales/es.yml
|
82
|
+
- config/locales/pt-BR.yml
|
83
|
+
- lib/rails-countries.rb
|
84
|
+
- lib/rails-countries/engine.rb
|
85
|
+
- lib/rails-countries/helpers.rb
|
86
|
+
- lib/rails-countries/validators.rb
|
87
|
+
- lib/rails-countries/version.rb
|
88
|
+
- rails-countries.gemspec
|
89
|
+
homepage: https://github.com/pedrofurtado/rails-countries
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubygems_version: 3.0.3
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Integration between Rails and countries gem.
|
112
|
+
test_files: []
|