validates_vat_number 0.2
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.
- data/LICENSE +20 -0
- data/README.rdoc +63 -0
- data/Rakefile +11 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/validates_vat_number.rb +75 -0
- data/spec/models.rb +40 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/validates_vat_number/validations_spec.rb +345 -0
- data/uninstall.rb +1 -0
- metadata +69 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Aurélien Malisart
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
= Description
|
2
|
+
|
3
|
+
Use this plugin to validate VAT numbers for european countries.
|
4
|
+
|
5
|
+
= Usage
|
6
|
+
|
7
|
+
To validate a model field as a valid intracom VAT number :
|
8
|
+
|
9
|
+
validates_vat_number :vat_number
|
10
|
+
|
11
|
+
It may be optional like this :
|
12
|
+
|
13
|
+
validates_vat_number :vat_number, :allow_blank => true
|
14
|
+
|
15
|
+
If you want the VAT number's country to match another kind of country field, use
|
16
|
+
the option :
|
17
|
+
|
18
|
+
validates_vat_number :vat_number, :country_method => :my_method
|
19
|
+
|
20
|
+
where it is supposed that your model has a method named 'my_method' which
|
21
|
+
returns the country code you want to match. The available country codes
|
22
|
+
available for Europa are: DE, AT, BE, BG, CY, DK, ES, EE, FI, FR, EL, HU, IE,
|
23
|
+
IT, LV, LT, LU, MT, NL, PL, PT, GB, RO, SK, SI, SE and CZ.
|
24
|
+
|
25
|
+
Example :
|
26
|
+
|
27
|
+
class Invoice < ActiveRecord::Base
|
28
|
+
validates_vat_number :vat_number, :country_method => :country_code
|
29
|
+
|
30
|
+
# Logic to return the country code
|
31
|
+
def country_code
|
32
|
+
case country.downcase
|
33
|
+
when 'belgium' then 'BE'
|
34
|
+
when 'france' then 'FR'
|
35
|
+
when 'sweden' then 'SE'
|
36
|
+
# ...
|
37
|
+
else nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
= Installation
|
43
|
+
|
44
|
+
As a gem in your environment.rb :
|
45
|
+
|
46
|
+
config.gem 'validates_vat_number'
|
47
|
+
|
48
|
+
As an old-school Rails plugin :
|
49
|
+
|
50
|
+
script/plugin install git://github.com/aurels/validates_vat_number.git
|
51
|
+
|
52
|
+
= Tests
|
53
|
+
|
54
|
+
If you want to run the specs :
|
55
|
+
|
56
|
+
rake spec
|
57
|
+
|
58
|
+
= Credits
|
59
|
+
|
60
|
+
This plugin in released under MIT license by Aurélien Malisart (see LICENSE
|
61
|
+
file).
|
62
|
+
|
63
|
+
(c) http://aurelien.malisart.be
|
data/Rakefile
ADDED
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'validates_vat_number'
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module ValidatesVatNumber
|
2
|
+
VAT_PATTERNS = {
|
3
|
+
'DE' => /\ADE[0-9]{9}\Z/, # Germany
|
4
|
+
'AT' => /\AATU[0-9]{8}\Z/, # Austria
|
5
|
+
'BE' => /\ABE[0-9]{10}\Z/, # Belgium
|
6
|
+
'BG' => /\ABG[0-9]{9,10}\Z/, # Bulgaria
|
7
|
+
'CY' => /\ACY[0-9]{8}[A-Z]\Z/, # Cyprus
|
8
|
+
'DK' => /\ADK[0-9]{8}\Z/, # Denmark
|
9
|
+
'ES' => /\AES[0-9]{9}\Z/, # Spain
|
10
|
+
'EE' => /\AEE[0-9]{9}\Z/, # Estonia
|
11
|
+
'FI' => /\AFI[0-9]{8}\Z/, # Finland
|
12
|
+
'FR' => /\AFR[A-Z0-9]{2}[0-9]{9}\Z/, # France
|
13
|
+
'EL' => /\AEL[0-9]{9}\Z/, # Greece
|
14
|
+
'HU' => /\AHU[0-9]{8}\Z/, # Hungary
|
15
|
+
'IE' => /\AIE([0-9][A-Z][0-9]{5}[A-Z]|[0-9]{7}[A-Z])\Z/, # Ireland
|
16
|
+
'IT' => /\AIT[0-9]{11}\Z/, # Italy
|
17
|
+
'LV' => /\ALV[0-9]{11}\Z/, # Latvia
|
18
|
+
'LT' => /\ALT([0-9]{9}|[0-9]{12})\Z/, # Lithuania
|
19
|
+
'LU' => /\ALU[0-9]{8}\Z/, # Luxembourg
|
20
|
+
'MT' => /\AMT[0-9]{8}\Z/, # Malta
|
21
|
+
'NL' => /\ANL[0-9]{9}[A-Z][0-9]{2}\Z/, # Netherlands
|
22
|
+
'PL' => /\APL[0-9]{10}\Z/, # Poland
|
23
|
+
'PT' => /\APT[0-9]{9}\Z/, # Portugal
|
24
|
+
'GB' => /\AGB([0-9]{9}|[A-Z0-9]{2}[0-9]{3})\Z/, # United Kingdom
|
25
|
+
'RO' => /\ARO[0-9]{9}\Z/, # Romania
|
26
|
+
'SK' => /\ASK[0-9]{10}\Z/, # Slovakia
|
27
|
+
'SI' => /\ASI[0-9]{8}\Z/, # Slovenia
|
28
|
+
'SE' => /\ASE[0-9]{12}\Z/, # Sweden
|
29
|
+
'CZ' => /\ACZ[0-9]{8,10}\Z/ # Czech Republic
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
class ActiveRecord::Base
|
34
|
+
def self.validates_vat_number(*attr_names)
|
35
|
+
configuration = { :on => :save, :allow_blank => false }
|
36
|
+
configuration.update(attr_names.extract_options!)
|
37
|
+
|
38
|
+
validates_each(attr_names, configuration) do |record, attr_name, value|
|
39
|
+
if configuration[:country_method]
|
40
|
+
country = record.send(configuration[:country_method]).to_s
|
41
|
+
if !ValidatesVatNumber::VAT_PATTERNS.has_key?(country) || value.to_s !~ ValidatesVatNumber::VAT_PATTERNS[country]
|
42
|
+
record.errors.add(attr_name, :invalid)
|
43
|
+
end
|
44
|
+
else
|
45
|
+
unless ValidatesVatNumber::VAT_PATTERNS.values.detect { |p| value.to_s =~ p }
|
46
|
+
record.errors.add(attr_name, configuration[:message])
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# ActiveModel/Rails 3 style ? :
|
54
|
+
|
55
|
+
# module ActiveModel
|
56
|
+
# module Validations
|
57
|
+
# module ClassMethods
|
58
|
+
# def validates_vat_number(*attr_names)
|
59
|
+
# configuration = {
|
60
|
+
# :message => ActiveRecord::Errors.default_error_messages[:invalid],
|
61
|
+
# :on => :save,
|
62
|
+
# :with => nil
|
63
|
+
# }
|
64
|
+
#
|
65
|
+
# validates_each(attr_names, configuration) do |record, attr_name, value|
|
66
|
+
# unless configuration[:allow_blank] && value.to_s.blank?
|
67
|
+
# unless ValidatesVatNumber::VAT_PATTERNS.values.detect { |p| value.to_s =~ p }
|
68
|
+
# record.errors.add(attr_name, configuration[:message])
|
69
|
+
# end
|
70
|
+
# end
|
71
|
+
# end
|
72
|
+
# end
|
73
|
+
# end
|
74
|
+
# end
|
75
|
+
# end
|
data/spec/models.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Migrations ===================================================================
|
2
|
+
|
3
|
+
ActiveRecord::Schema.define do
|
4
|
+
create_table :invoices, :force => true do |t|
|
5
|
+
t.string :vat_number
|
6
|
+
end
|
7
|
+
|
8
|
+
create_table :optional_vat_invoices, :force => true do |t|
|
9
|
+
t.string :vat_number
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table :country_checked_invoices, :force => true do |t|
|
13
|
+
t.string :vat_number
|
14
|
+
t.string :country
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Models =======================================================================
|
19
|
+
|
20
|
+
class Invoice < ActiveRecord::Base
|
21
|
+
validates_vat_number :vat_number
|
22
|
+
end
|
23
|
+
|
24
|
+
class OptionalVatInvoice < ActiveRecord::Base
|
25
|
+
validates_vat_number :vat_number, :allow_blank => true
|
26
|
+
end
|
27
|
+
|
28
|
+
class CountryCheckedInvoice < ActiveRecord::Base
|
29
|
+
validates_vat_number :vat_number, :country_method => :country_code
|
30
|
+
|
31
|
+
# Logic to return the country code
|
32
|
+
def country_code
|
33
|
+
case country.downcase
|
34
|
+
when 'belgium' then 'BE'
|
35
|
+
when 'france' then 'FR'
|
36
|
+
when 'sweden' then 'SE'
|
37
|
+
else nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_record'
|
5
|
+
require 'action_controller'
|
6
|
+
require 'action_view'
|
7
|
+
|
8
|
+
require File.dirname(__FILE__) + '/../lib/validates_vat_number.rb'
|
9
|
+
|
10
|
+
# ActiveRecord setup ===========================================================
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection({
|
13
|
+
:adapter => 'sqlite3',
|
14
|
+
:database => 'test.db'
|
15
|
+
})
|
16
|
+
|
17
|
+
require 'models'
|
@@ -0,0 +1,345 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Invoice do
|
4
|
+
|
5
|
+
# DE
|
6
|
+
|
7
|
+
it "should validate well a DE vat number" do
|
8
|
+
Invoice.new(:vat_number => "DE345889003").should be_valid
|
9
|
+
|
10
|
+
Invoice.new(:vat_number => "DE34588900").should_not be_valid
|
11
|
+
Invoice.new(:vat_number => "DE3458890090").should_not be_valid
|
12
|
+
Invoice.new(:vat_number => "DE34588900C").should_not be_valid
|
13
|
+
end
|
14
|
+
|
15
|
+
# AT
|
16
|
+
|
17
|
+
it "should validate well a AT vat number" do
|
18
|
+
Invoice.new(:vat_number => "ATU03458890").should be_valid
|
19
|
+
|
20
|
+
Invoice.new(:vat_number => "ATU034588908").should_not be_valid
|
21
|
+
Invoice.new(:vat_number => "ATU0345908").should_not be_valid
|
22
|
+
Invoice.new(:vat_number => "ATU0345889Y").should_not be_valid
|
23
|
+
end
|
24
|
+
|
25
|
+
# BE
|
26
|
+
|
27
|
+
it "should validate well a BE vat number" do
|
28
|
+
Invoice.new(:vat_number => "BE0817331995").should be_valid
|
29
|
+
|
30
|
+
Invoice.new(:vat_number => "BE081733199").should_not be_valid
|
31
|
+
Invoice.new(:vat_number => "BE08173319944").should_not be_valid
|
32
|
+
Invoice.new(:vat_number => "BE081733199H").should_not be_valid
|
33
|
+
end
|
34
|
+
|
35
|
+
# BG
|
36
|
+
|
37
|
+
it "should validate well a BG vat number" do
|
38
|
+
Invoice.new(:vat_number => "BG468134789").should be_valid
|
39
|
+
Invoice.new(:vat_number => "BG4681347897").should be_valid
|
40
|
+
|
41
|
+
Invoice.new(:vat_number => "BG46813478979").should_not be_valid
|
42
|
+
Invoice.new(:vat_number => "BG4681347897C").should_not be_valid
|
43
|
+
Invoice.new(:vat_number => "BG46813478G").should_not be_valid
|
44
|
+
Invoice.new(:vat_number => "BG46813478").should_not be_valid
|
45
|
+
end
|
46
|
+
|
47
|
+
# CY
|
48
|
+
|
49
|
+
it "should validate well a CY vat number" do
|
50
|
+
Invoice.new(:vat_number => "CY36579347A").should be_valid
|
51
|
+
|
52
|
+
Invoice.new(:vat_number => "CY36579347").should_not be_valid
|
53
|
+
Invoice.new(:vat_number => "CY365793478").should_not be_valid
|
54
|
+
Invoice.new(:vat_number => "CY365793G").should_not be_valid
|
55
|
+
end
|
56
|
+
|
57
|
+
# DK
|
58
|
+
|
59
|
+
it "should validate well a DK vat number" do
|
60
|
+
Invoice.new(:vat_number => "DK67893463").should be_valid
|
61
|
+
|
62
|
+
Invoice.new(:vat_number => "DK678934637").should_not be_valid
|
63
|
+
Invoice.new(:vat_number => "DK6789346").should_not be_valid
|
64
|
+
Invoice.new(:vat_number => "DK6789346H").should_not be_valid
|
65
|
+
end
|
66
|
+
|
67
|
+
# ES
|
68
|
+
|
69
|
+
it "should validate well a ES vat number" do
|
70
|
+
Invoice.new(:vat_number => "ES567345987").should be_valid
|
71
|
+
Invoice.new(:vat_number => "ES5673459879").should_not be_valid
|
72
|
+
|
73
|
+
Invoice.new(:vat_number => "ES56734598").should_not be_valid
|
74
|
+
Invoice.new(:vat_number => "ES56734598J").should_not be_valid
|
75
|
+
end
|
76
|
+
|
77
|
+
# EE
|
78
|
+
|
79
|
+
it "should validate well a EE vat number" do
|
80
|
+
Invoice.new(:vat_number => "EE678678456").should be_valid
|
81
|
+
Invoice.new(:vat_number => "EE6786784560").should_not be_valid
|
82
|
+
Invoice.new(:vat_number => "EE67867845").should_not be_valid
|
83
|
+
Invoice.new(:vat_number => "EE67867845K").should_not be_valid
|
84
|
+
end
|
85
|
+
|
86
|
+
# FI
|
87
|
+
|
88
|
+
it "should validate well a FI vat number" do
|
89
|
+
Invoice.new(:vat_number => "FI67845638").should be_valid
|
90
|
+
|
91
|
+
Invoice.new(:vat_number => "FI678456389").should_not be_valid
|
92
|
+
Invoice.new(:vat_number => "FI6784563").should_not be_valid
|
93
|
+
Invoice.new(:vat_number => "FI6784563K").should_not be_valid
|
94
|
+
end
|
95
|
+
|
96
|
+
# FR
|
97
|
+
|
98
|
+
it "should validate well a FR vat number" do
|
99
|
+
Invoice.new(:vat_number => "FR99123543267").should be_valid
|
100
|
+
Invoice.new(:vat_number => "FRBB123543267").should be_valid
|
101
|
+
Invoice.new(:vat_number => "FR9B123543267").should be_valid
|
102
|
+
Invoice.new(:vat_number => "FRB9123543267").should be_valid
|
103
|
+
|
104
|
+
Invoice.new(:vat_number => "FR99123543267B").should_not be_valid
|
105
|
+
Invoice.new(:vat_number => "FRBB12354326").should_not be_valid
|
106
|
+
Invoice.new(:vat_number => "FR9B123543F67").should_not be_valid
|
107
|
+
Invoice.new(:vat_number => "FRB9123543").should_not be_valid
|
108
|
+
end
|
109
|
+
|
110
|
+
# EL
|
111
|
+
|
112
|
+
it "should validate well a EL vat number" do
|
113
|
+
Invoice.new(:vat_number => "EL678456345").should be_valid
|
114
|
+
|
115
|
+
Invoice.new(:vat_number => "EL67845634").should_not be_valid
|
116
|
+
Invoice.new(:vat_number => "EL6784563459").should_not be_valid
|
117
|
+
Invoice.new(:vat_number => "EL67845634P").should_not be_valid
|
118
|
+
end
|
119
|
+
|
120
|
+
# HU
|
121
|
+
|
122
|
+
it "should validate well a HU vat number" do
|
123
|
+
Invoice.new(:vat_number => "HU67894595").should be_valid
|
124
|
+
|
125
|
+
Invoice.new(:vat_number => "HU6789459").should_not be_valid
|
126
|
+
Invoice.new(:vat_number => "HU67894595L").should_not be_valid
|
127
|
+
Invoice.new(:vat_number => "HU6789459J").should_not be_valid
|
128
|
+
end
|
129
|
+
|
130
|
+
# IE
|
131
|
+
|
132
|
+
it "should validate well a IE vat number" do
|
133
|
+
Invoice.new(:vat_number => "IE1B12345J").should be_valid
|
134
|
+
Invoice.new(:vat_number => "IE1234567B").should be_valid
|
135
|
+
|
136
|
+
Invoice.new(:vat_number => "IE1B123459").should_not be_valid
|
137
|
+
Invoice.new(:vat_number => "IE19123450").should_not be_valid
|
138
|
+
Invoice.new(:vat_number => "IEA9123450").should_not be_valid
|
139
|
+
end
|
140
|
+
|
141
|
+
# IT
|
142
|
+
|
143
|
+
it "should validate well a IT vat number" do
|
144
|
+
Invoice.new(:vat_number => "IT45875359285").should be_valid
|
145
|
+
|
146
|
+
Invoice.new(:vat_number => "IT458753592859").should_not be_valid
|
147
|
+
Invoice.new(:vat_number => "IT4587535928G").should_not be_valid
|
148
|
+
Invoice.new(:vat_number => "IT4587535928").should_not be_valid
|
149
|
+
end
|
150
|
+
|
151
|
+
# LV
|
152
|
+
|
153
|
+
it "should validate well a LV vat number" do
|
154
|
+
Invoice.new(:vat_number => "LV85678368906").should be_valid
|
155
|
+
|
156
|
+
Invoice.new(:vat_number => "LV8567836890").should_not be_valid
|
157
|
+
Invoice.new(:vat_number => "LV856783689066").should_not be_valid
|
158
|
+
Invoice.new(:vat_number => "LV8567836890S").should_not be_valid
|
159
|
+
end
|
160
|
+
|
161
|
+
# LT
|
162
|
+
|
163
|
+
it "should validate well a LT vat number" do
|
164
|
+
Invoice.new(:vat_number => "LT678678987").should be_valid
|
165
|
+
Invoice.new(:vat_number => "LT678678987956").should be_valid
|
166
|
+
|
167
|
+
Invoice.new(:vat_number => "LT67867898").should_not be_valid
|
168
|
+
Invoice.new(:vat_number => "LT6786789870").should_not be_valid
|
169
|
+
Invoice.new(:vat_number => "LT678678987K").should_not be_valid
|
170
|
+
Invoice.new(:vat_number => "LT67867898709").should_not be_valid
|
171
|
+
Invoice.new(:vat_number => "LT6786789870C").should_not be_valid
|
172
|
+
Invoice.new(:vat_number => "LT67867898795H").should_not be_valid
|
173
|
+
end
|
174
|
+
|
175
|
+
# LU
|
176
|
+
|
177
|
+
it "should validate well a LU vat number" do
|
178
|
+
Invoice.new(:vat_number => "LU45679456").should be_valid
|
179
|
+
|
180
|
+
Invoice.new(:vat_number => "LU4567945").should_not be_valid
|
181
|
+
Invoice.new(:vat_number => "LU456794560").should_not be_valid
|
182
|
+
Invoice.new(:vat_number => "LU4567945J").should_not be_valid
|
183
|
+
end
|
184
|
+
|
185
|
+
# MT
|
186
|
+
|
187
|
+
it "should validate well a MT vat number" do
|
188
|
+
Invoice.new(:vat_number => "MT64569367").should be_valid
|
189
|
+
|
190
|
+
Invoice.new(:vat_number => "MT6456936").should_not be_valid
|
191
|
+
Invoice.new(:vat_number => "MT645693679").should_not be_valid
|
192
|
+
Invoice.new(:vat_number => "MT6456936L").should_not be_valid
|
193
|
+
end
|
194
|
+
|
195
|
+
# NL
|
196
|
+
|
197
|
+
it "should validate well a NL vat number" do
|
198
|
+
Invoice.new(:vat_number => "NL673739491A77").should be_valid
|
199
|
+
|
200
|
+
Invoice.new(:vat_number => "NL673739491977").should_not be_valid
|
201
|
+
Invoice.new(:vat_number => "NL67373949197").should_not be_valid
|
202
|
+
Invoice.new(:vat_number => "NL67373949").should_not be_valid
|
203
|
+
end
|
204
|
+
|
205
|
+
# PL
|
206
|
+
|
207
|
+
it "should validate well a PL vat number" do
|
208
|
+
Invoice.new(:vat_number => "PL6784567284").should be_valid
|
209
|
+
|
210
|
+
Invoice.new(:vat_number => "PL678456728").should_not be_valid
|
211
|
+
Invoice.new(:vat_number => "PL67845672849").should_not be_valid
|
212
|
+
Invoice.new(:vat_number => "PL678456728K").should_not be_valid
|
213
|
+
end
|
214
|
+
|
215
|
+
# PT
|
216
|
+
|
217
|
+
it "should validate well a PT vat number" do
|
218
|
+
Invoice.new(:vat_number => "PT346296476").should be_valid
|
219
|
+
|
220
|
+
Invoice.new(:vat_number => "PT34629647").should_not be_valid
|
221
|
+
Invoice.new(:vat_number => "PT3462964769").should_not be_valid
|
222
|
+
end
|
223
|
+
|
224
|
+
# GB
|
225
|
+
|
226
|
+
it "should validate well a GB vat number" do
|
227
|
+
Invoice.new(:vat_number => "GB123456789").should be_valid
|
228
|
+
Invoice.new(:vat_number => "GBAB123").should be_valid
|
229
|
+
Invoice.new(:vat_number => "GBA0123").should be_valid
|
230
|
+
Invoice.new(:vat_number => "GB0B123").should be_valid
|
231
|
+
Invoice.new(:vat_number => "GB00123").should be_valid
|
232
|
+
|
233
|
+
Invoice.new(:vat_number => "GB12345678").should_not be_valid
|
234
|
+
Invoice.new(:vat_number => "GBAB1238").should_not be_valid
|
235
|
+
Invoice.new(:vat_number => "GBA0123L").should_not be_valid
|
236
|
+
Invoice.new(:vat_number => "GB0B12300").should_not be_valid
|
237
|
+
Invoice.new(:vat_number => "GB00123L").should_not be_valid
|
238
|
+
end
|
239
|
+
|
240
|
+
# RO
|
241
|
+
|
242
|
+
it "should validate well a RO vat number" do
|
243
|
+
Invoice.new(:vat_number => "RO856723965").should be_valid
|
244
|
+
end
|
245
|
+
|
246
|
+
# SK
|
247
|
+
|
248
|
+
it "should validate well a SK vat number" do
|
249
|
+
Invoice.new(:vat_number => "SK5683075682").should be_valid
|
250
|
+
end
|
251
|
+
|
252
|
+
# SI
|
253
|
+
|
254
|
+
it "should validate well a SI vat number" do
|
255
|
+
Invoice.new(:vat_number => "SI74357893").should be_valid
|
256
|
+
end
|
257
|
+
|
258
|
+
# SE
|
259
|
+
|
260
|
+
it "should validate well a SE vat number" do
|
261
|
+
Invoice.new(:vat_number => "SE567396352789").should be_valid
|
262
|
+
end
|
263
|
+
|
264
|
+
# CZ
|
265
|
+
|
266
|
+
it "should validate well a CZ vat number" do
|
267
|
+
Invoice.new(:vat_number => "CZ56389267").should be_valid
|
268
|
+
Invoice.new(:vat_number => "CZ563892670").should be_valid
|
269
|
+
Invoice.new(:vat_number => "CZ5638926790").should be_valid
|
270
|
+
end
|
271
|
+
|
272
|
+
# Blank
|
273
|
+
|
274
|
+
it "should not be valid if vat number is blank" do
|
275
|
+
Invoice.new(:vat_number => '').should_not be_valid
|
276
|
+
end
|
277
|
+
|
278
|
+
# Misc
|
279
|
+
|
280
|
+
it "should be invalid if really non-sense VAT number is passed" do
|
281
|
+
Invoice.new(:vat_number => '345678').should_not be_valid
|
282
|
+
Invoice.new(:vat_number => 'BE').should_not be_valid
|
283
|
+
Invoice.new(:vat_number => 'BEFR').should_not be_valid
|
284
|
+
Invoice.new(:vat_number => 'ZERTYU').should_not be_valid
|
285
|
+
Invoice.new(:vat_number => 'be817331995').should_not be_valid
|
286
|
+
Invoice.new(:vat_number => '&hczeà').should_not be_valid
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
describe OptionalVatInvoice do
|
291
|
+
it "should be valid if vat number is correct" do
|
292
|
+
OptionalVatInvoice.new(:vat_number => 'BE0817331995').should be_valid
|
293
|
+
end
|
294
|
+
|
295
|
+
it "should not be valid if vat number is incorrect" do
|
296
|
+
OptionalVatInvoice.new(:vat_number => 'BE081733199').should_not be_valid
|
297
|
+
end
|
298
|
+
|
299
|
+
it "should be valid if vat number is blank" do
|
300
|
+
OptionalVatInvoice.new(:vat_number => '').should be_valid
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
304
|
+
describe CountryCheckedInvoice do
|
305
|
+
it "should be valid if vat number is correct and matches country" do
|
306
|
+
CountryCheckedInvoice.new(
|
307
|
+
:vat_number => 'BE0817331995',
|
308
|
+
:country => 'Belgium'
|
309
|
+
).should be_valid
|
310
|
+
|
311
|
+
CountryCheckedInvoice.new(
|
312
|
+
:vat_number => 'FRBB123543267',
|
313
|
+
:country => 'France'
|
314
|
+
).should be_valid
|
315
|
+
end
|
316
|
+
|
317
|
+
it "should not be valid if vat number is correct and does not match country" do
|
318
|
+
CountryCheckedInvoice.new(
|
319
|
+
:vat_number => 'BE0817331995',
|
320
|
+
:country => ''
|
321
|
+
).should_not be_valid
|
322
|
+
|
323
|
+
CountryCheckedInvoice.new(
|
324
|
+
:vat_number => 'BE0817331995',
|
325
|
+
:country => 'France'
|
326
|
+
).should_not be_valid
|
327
|
+
|
328
|
+
CountryCheckedInvoice.new(
|
329
|
+
:vat_number => 'BE0817331995',
|
330
|
+
:country => 'FunkyCountry'
|
331
|
+
).should_not be_valid
|
332
|
+
end
|
333
|
+
|
334
|
+
it "should be valid if vat number is blank" do
|
335
|
+
CountryCheckedInvoice.new(
|
336
|
+
:vat_number => '',
|
337
|
+
:country => ''
|
338
|
+
).should_not be_valid
|
339
|
+
|
340
|
+
CountryCheckedInvoice.new(
|
341
|
+
:vat_number => '',
|
342
|
+
:country => 'Belgium'
|
343
|
+
).should_not be_valid
|
344
|
+
end
|
345
|
+
end
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_vat_number
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.2"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Aur\xC3\xA9lien Malisart"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-24 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Validations for european countries VAT numbers
|
17
|
+
email: aurelien.malisart@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- lib/validates_vat_number.rb
|
26
|
+
- spec/models.rb
|
27
|
+
- spec/spec_helper.rb
|
28
|
+
- spec/validates_vat_number/validations_spec.rb
|
29
|
+
- LICENSE
|
30
|
+
- README.rdoc
|
31
|
+
- Rakefile
|
32
|
+
- install.rb
|
33
|
+
- init.rb
|
34
|
+
- uninstall.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/aurels/validates_vat_number
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --line-numbers
|
42
|
+
- --inline-source
|
43
|
+
- --title
|
44
|
+
- CanCan
|
45
|
+
- --main
|
46
|
+
- README.rdoc
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "1.2"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.3.5
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Validations for european countries VAT numbers
|
68
|
+
test_files: []
|
69
|
+
|