-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit with Validator, helper and green tests
- Loading branch information
0 parents
commit 37ccd8a
Showing
14 changed files
with
346 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.config | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp | ||
*.bundle | ||
*.so | ||
*.o | ||
*.a | ||
mkmf.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in validates_zipcode.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2014 David Gil | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# ValidatesZipcode | ||
|
||
Add zipcode validation support to Rails (ActiveModel), considering different zipcode country formats. | ||
|
||
Currently supported country zipcodes are: USA, Canada, Spain, New Zeland, Australia, Colombia and Argentina. | ||
Any other country's zipcode will validate without errors. | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'validates_zipcode' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install validates_zipcode | ||
|
||
## Usage | ||
|
||
validates_zipcode :zipcode | ||
|
||
validates :zipcode, zipcode: true | ||
|
||
``ValidatesZipcode`` expects the model to have an attribute called ``country_alpha2`` to contain the country code. | ||
You can provide your own country_code using ``:country_code`` option, or specify which attribute contains this information | ||
using ``:country_code_attribute`` option. | ||
|
||
validates :zipcode, zipcode: { country_code: :es } | ||
|
||
validates :zipcode, zipcode: { country_code_attribute: :my_country_code_column } | ||
|
||
If you need to localize the error message, just add this to your I18n locale file: | ||
|
||
errors: | ||
messages: | ||
invalid_zipcode: Your zipcode error message. | ||
|
||
## Contributing | ||
|
||
1. Fork it ( https://github.com/[my-github-username]/validates_zipcode/fork ) | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create a new Pull Request | ||
|
||
## Contributors | ||
|
||
To see the generous people who have contributed code, take a look at the {contributors list}[http://github.com/dgilperez/validates_zipcode/contributors]. | ||
|
||
## Maintainers | ||
|
||
* {David Gil}[http://github.com/dgilperez] | ||
|
||
## License | ||
|
||
Copyright (c) 2014 David Gil Pérez, released under the MIT license |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require "bundler/gem_tasks" | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
task default: :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "validates_zipcode/railtie" if defined?(Rails) | ||
require "validates_zipcode/validator" | ||
require "validates_zipcode/helper_methods" | ||
require "validates_zipcode/version" | ||
|
||
module ValidatesZipcode | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module ActiveModel | ||
module Validations | ||
module HelperMethods | ||
def validates_zipcode(*attr_names) | ||
validates_with ZipcodeValidator, _merge_attributes(attr_names) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module ValidatesZipcode | ||
class Railtie < Rails::Railtie | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Usage: | ||
# | ||
# class User < ActiveModel | ||
# validates :postalcode, zipcode: true | ||
# | ||
# # Required country_alpha2 attribute | ||
# def country_alpha2 | ||
# country.alpha2 | ||
# end | ||
# end | ||
# | ||
|
||
require 'active_model' | ||
require 'active_model/validator' | ||
|
||
module ValidatesZipcode | ||
class Validator < ActiveModel::EachValidator | ||
# using country alpha2 code as key | ||
ZIPCODES_REGEX = { | ||
ES: /\A\d{5}\z/, | ||
AR: /\A([A-HJ-TP-Z]{1}\d{4}[A-Z]{3}|[a-z]{1}\d{4}[a-hj-tp-z]{3})\z/, | ||
CL: /\A[0-9]{3}[-]?[0-9]{4}\z/, | ||
NZ: /\A\d{4}\z/, | ||
AU: /\A\d{4}\z/, | ||
US: /\A\d{5}(-\d{4})?\z/, | ||
CA: /\A[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}\z/ | ||
# CA: /\A[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTWVXYZ]\d[ABCEGHJKLMNPRSTWVXYZ]\d\z/ | ||
} | ||
|
||
def initialize(options) | ||
@country_code = options.fetch(:country_code) { } | ||
@country_code_attribute = options.fetch(:country_code_attribute) { :country_alpha2 } | ||
|
||
super | ||
end | ||
|
||
def validate_each(record, attribute, value) | ||
alpha2 = @country_code || record.send(@country_code_attribute) | ||
alpha2 = alpha2.to_s.upcase.to_sym | ||
regexp = ZIPCODES_REGEX[alpha2] | ||
return unless regexp | ||
|
||
unless regexp.match(value.to_s) | ||
record.errors.add(attribute, I18n.t('errors.messages.invalid_zipcode', value: value)) | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActiveModel::Validations::ZipcodeValidator = ValidatesZipcode::Validator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module ValidatesZipcode | ||
VERSION = "0.0.1" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'validates_zipcode' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
require 'spec_helper' | ||
|
||
describe ValidatesZipcode, '#validate_each' do | ||
context "Spain" do | ||
it 'does not add errors with a valid zipcode' do | ||
record = build_record('93108', "ES") | ||
zipcode_should_be_valid(record) | ||
end | ||
|
||
it 'adds errors with an invalid Zipcode' do | ||
['1234', '12345-12345', 'D0D0D0', 'invalid_zip'].each do |zipcode| | ||
record = build_record(zipcode, 'ES') | ||
zipcode_should_be_invalid(record, zipcode) | ||
end | ||
end | ||
end | ||
|
||
context "USA" do | ||
it 'does not add errors with a valid zipcode' do | ||
record = build_record('93108', "US") | ||
zipcode_should_be_valid(record) | ||
[12345, '12345', '12345-1234'].each do |zipcode| | ||
record = build_record(zipcode, 'US') | ||
zipcode_should_be_valid(record) | ||
end | ||
end | ||
|
||
it 'adds errors with an invalid Zipcode' do | ||
['1234', '12345-12345', 'D0D0D0', 'invalid_zip'].each do |zipcode| | ||
record = build_record(zipcode, 'US') | ||
zipcode_should_be_invalid(record, zipcode) | ||
end | ||
end | ||
end | ||
|
||
context "Argentina" do | ||
it 'does not add errors with a valid zipcode' do | ||
record = build_record('C1424CHN', "AR") | ||
zipcode_should_be_valid(record) | ||
end | ||
|
||
it 'adds errors with an invalid Zipcode' do | ||
['1234', '12345', 'D0D0D0', 'invalid_zip'].each do |zipcode| | ||
record = build_record(zipcode, 'AR') | ||
zipcode_should_be_invalid(record, zipcode) | ||
end | ||
end | ||
end | ||
|
||
context "Chile" do | ||
it 'does not add errors with a valid zipcode' do | ||
['123-1233', '000-0000', '1234567'].each do |zipcode| | ||
record = build_record(zipcode, "CL") | ||
zipcode_should_be_valid(record) | ||
end | ||
end | ||
|
||
it 'adds errors with an invalid Zipcode' do | ||
['1234', '12345', 'D0D0D0', 'invalid_zip', 'C1424CHN', '122-12345'].each do |zipcode| | ||
record = build_record(zipcode, 'CL') | ||
zipcode_should_be_invalid(record, zipcode) | ||
end | ||
end | ||
end | ||
|
||
context "Australia and New Zeland" do | ||
it 'does not add errors with a valid zipcode' do | ||
["AU", "NZ"].each do |code| | ||
record = build_record('9310', code) | ||
zipcode_should_be_valid(record) | ||
end | ||
end | ||
|
||
it 'adds errors with an invalid Zipcode' do | ||
["AU", "NZ"].each do |code| | ||
['C1424CHN', '12345', 'invalid_zip'].each do |zipcode| | ||
record = build_record(zipcode, code) | ||
zipcode_should_be_invalid(record, zipcode) | ||
end | ||
end | ||
end | ||
end | ||
|
||
context "Canada" do | ||
it 'does not add errors with a valid zipcode' do | ||
record = build_record('A1J2Z9', 'CA') | ||
zipcode_should_be_valid(record) | ||
end | ||
|
||
it 'adds errors with an invalid Zipcode' do | ||
['C1424CHN', '12345', 'D0D0D0', 'invalid_zip'].each do |zipcode| | ||
record = build_record(zipcode, 'CA') | ||
zipcode_should_be_invalid(record, zipcode) | ||
end | ||
end | ||
end | ||
|
||
context "unknown country" do | ||
it 'does not add errors with a any zipcode' do | ||
record = build_record('A1J2Z9', 'ZZ') | ||
zipcode_should_be_valid(record) | ||
end | ||
end | ||
|
||
def zipcode_should_be_valid(record) | ||
ValidatesZipcode::Validator.new(attributes: :zipcode).validate(record) | ||
|
||
expect(record.errors).to be_empty | ||
end | ||
|
||
def zipcode_should_be_invalid(record, zipcode = "invalid_zip") | ||
ValidatesZipcode::Validator.new(attributes: :zipcode).validate(record) | ||
|
||
expect(record.errors.size).to eq 1 | ||
end | ||
|
||
def build_record(zipcode, country_alpha2) | ||
ValidationDummyClass.new.tap do |object| | ||
object.country_alpha2 = country_alpha2 | ||
object.zipcode = zipcode | ||
end | ||
end | ||
|
||
class ValidationDummyClass | ||
include ::ActiveModel::Validations | ||
attr_accessor :zipcode, :country_alpha2 | ||
|
||
def self.name | ||
'TestClass' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'validates_zipcode/version' | ||
|
||
Gem::Specification.new do |s| | ||
s.name = "validates_zipcode" | ||
s.version = ValidatesZipcode::VERSION | ||
s.authors = ["David Gil"] | ||
s.email = ["[email protected]"] | ||
s.summary = %q{Localizable zipcode validation for Rails.} | ||
s.description = %q{Adds zipcode validation methods to ActiveModel considering different country zipcode formats.} | ||
s.homepage = "http://github.com/dgilperez/validates_zipcode" | ||
s.license = "MIT" | ||
|
||
s.files = `git ls-files -z`.split("\x0") | ||
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) } | ||
s.test_files = s.files.grep(%r{^(test|spec|features)/}) | ||
s.require_paths = ["lib"] | ||
|
||
s.add_dependency 'activemodel', '>= 3.2.0' | ||
|
||
s.add_development_dependency "bundler", "~> 1.6" | ||
s.add_development_dependency "rake" | ||
s.add_development_dependency "rspec" | ||
end |