valvat 0.2.3 → 0.3.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.
- data/CHANGES.md +7 -1
- data/README.md +32 -14
- data/lib/valvat/active_model.rb +8 -2
- data/lib/valvat/version.rb +1 -1
- data/spec/valvat/active_model_spec.rb +46 -21
- metadata +3 -3
data/CHANGES.md
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
### dev
|
2
2
|
|
3
|
-
[full changelog](http://github.com/yolk/valvat/compare/v0.
|
3
|
+
[full changelog](http://github.com/yolk/valvat/compare/v0.3.0...master)
|
4
|
+
|
5
|
+
### 0.3.0 / 2011-01-12
|
6
|
+
|
7
|
+
[full changelog](http://github.com/yolk/valvat/compare/v0.2.3...v0.3.0)
|
8
|
+
|
9
|
+
* ActiveModel validation: added _match_counrty_ option to validate if iso country code of vat number matches another attribute.
|
4
10
|
|
5
11
|
### 0.2.3 / 2011-01-10
|
6
12
|
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
|
1
|
+
# valvat
|
2
2
|
|
3
3
|
Validates european vat numbers. Standalone or as a ActiveModel validator.
|
4
4
|
|
5
|
-
|
5
|
+
## Features
|
6
6
|
|
7
7
|
* Simple syntax verification
|
8
8
|
* Lookup via the VIES web service
|
@@ -12,11 +12,11 @@ Validates european vat numbers. Standalone or as a ActiveModel validator.
|
|
12
12
|
|
13
13
|
valvat is tested and works with ruby 1.8.7/1.9.2 and ActiveModel 3.0.
|
14
14
|
|
15
|
-
|
15
|
+
## Installation
|
16
16
|
|
17
17
|
gem install valvat
|
18
18
|
|
19
|
-
|
19
|
+
## Basic Usage
|
20
20
|
|
21
21
|
To verify the syntax of a vat number:
|
22
22
|
|
@@ -42,19 +42,25 @@ Or to lookup a vat number string directly via VIES web service:
|
|
42
42
|
Valvat::Lookup.validate("DE345789003")
|
43
43
|
=> true or false or nil
|
44
44
|
|
45
|
-
|
45
|
+
## ActiveModel/Rails3 Usage
|
46
|
+
|
47
|
+
### Loading
|
46
48
|
|
47
49
|
When the valvat gem is required and ActiveModel is already loaded, everything will work fine out of the box. If your load order differs just add
|
48
50
|
|
49
51
|
require 'valvat/active_model'
|
50
52
|
|
51
53
|
after ActiveModel has been loaded.
|
54
|
+
|
55
|
+
### Simple syntax validation
|
52
56
|
|
53
57
|
To validate the attribute `vat_number` add this to your model:
|
54
58
|
|
55
59
|
class MyModel < ActiveRecord::Base
|
56
60
|
validates :vat_number, :valvat => true
|
57
61
|
end
|
62
|
+
|
63
|
+
### Additional lookup validation
|
58
64
|
|
59
65
|
To additionally perform a lookup via VIES:
|
60
66
|
|
@@ -63,12 +69,24 @@ To additionally perform a lookup via VIES:
|
|
63
69
|
By default this will validate to true if the VIES web service is down. To fail in this case simply add the `:fail_if_down` option:
|
64
70
|
|
65
71
|
validates :vat_number, :valvat => {:lookup => :fail_if_down}
|
66
|
-
|
72
|
+
|
73
|
+
### Additional ISO country code validation
|
74
|
+
|
75
|
+
If you want the vat number’s (ISO) country to match another country attribute, use the _match_country_ option:
|
76
|
+
|
77
|
+
validates :vat_number, :valvat => {:match_country => :country}
|
78
|
+
|
79
|
+
where it is supposed that your model has a method named _country_ which returns the country ISO code you want to match.
|
80
|
+
|
81
|
+
### Allow blank
|
82
|
+
|
67
83
|
By default blank vat numbers validate to false. To change this add the `:allow_blank` option:
|
68
84
|
|
69
85
|
validates :vat_number, :valvat => {:allow_blank => true}
|
70
|
-
|
71
|
-
|
86
|
+
|
87
|
+
### Allow vat numbers outside of europe
|
88
|
+
|
89
|
+
To allow vat numbers from outside of europe, add something like this to your model (country_code should return a upcase ISO country code):
|
72
90
|
|
73
91
|
class MyModel < ActiveRecord::Base
|
74
92
|
validates :vat_number, :valvat => true, :if => :eu?
|
@@ -77,8 +95,8 @@ To allow vat numbers from outside of europe, add something like this to your mod
|
|
77
95
|
Valvat::Utils::EU_COUNTRIES.include?(country_code)
|
78
96
|
end
|
79
97
|
end
|
80
|
-
|
81
|
-
|
98
|
+
|
99
|
+
## Utilities
|
82
100
|
|
83
101
|
To split a vat number into the country code and the remaining chars:
|
84
102
|
|
@@ -90,9 +108,9 @@ or
|
|
90
108
|
Valvat.new("ATU345789003").to_a
|
91
109
|
=> ["AT", "U345789003"]
|
92
110
|
|
93
|
-
Both methods always return an array. If it can not detect the country or the given country is located outside of europe it returns `[nil, nil]`. Please note that this does not strictly return the
|
111
|
+
Both methods always return an array. If it can not detect the country or the given country is located outside of europe it returns `[nil, nil]`. Please note that this does not strictly return the ISO country code: for greek vat numbers this returns the ISO language code 'EL' instead of the ISO country code 'GR'.
|
94
112
|
|
95
|
-
To extract the
|
113
|
+
To extract the ISO country code of a given vat number:
|
96
114
|
|
97
115
|
Valvat.new("EL7345789003").iso_country_code
|
98
116
|
=> "GR"
|
@@ -109,13 +127,13 @@ To normalize a vat number:
|
|
109
127
|
|
110
128
|
This basically just removes trailing spaces and ensures all chars are uppercase.
|
111
129
|
|
112
|
-
|
130
|
+
## Links
|
113
131
|
|
114
132
|
* [VIES web service](http://ec.europa.eu/taxation_customs/vies)
|
115
133
|
* [European vat number formats (german)](http://bzst.de/DE/Steuern_International/USt_Identifikationsnummer/Merkblaetter/Aufbau_USt_IdNr.html)
|
116
134
|
* [European vat number formats on Wikipedia](http://en.wikipedia.org/wiki/European_Union_Value_Added_Tax)
|
117
135
|
|
118
|
-
|
136
|
+
## BlaBla
|
119
137
|
|
120
138
|
Copyright (c) 2011 Yolk Sebastian Munz & Julia Soergel GbR
|
121
139
|
|
data/lib/valvat/active_model.rb
CHANGED
@@ -8,9 +8,15 @@ module ActiveModel
|
|
8
8
|
|
9
9
|
def validate_each(record, attribute, value)
|
10
10
|
vat = Valvat(value)
|
11
|
-
|
12
11
|
is_valid = options[:lookup] ? vat.valid? && vat.exists? : vat.valid?
|
13
|
-
|
12
|
+
|
13
|
+
if is_valid.nil?
|
14
|
+
is_valid = options[:lookup] != :fail_if_down
|
15
|
+
end
|
16
|
+
|
17
|
+
if is_valid && options[:match_country]
|
18
|
+
is_valid = (record.send(options[:match_country]) || "").upcase == vat.iso_country_code
|
19
|
+
end
|
14
20
|
|
15
21
|
unless is_valid
|
16
22
|
record.errors.add(attribute, :invalid_vat,
|
data/lib/valvat/version.rb
CHANGED
@@ -20,30 +20,31 @@ class InvoiceAllowBlankOnAll < ModelBase
|
|
20
20
|
validates :vat_number, :valvat => true, :allow_blank => true
|
21
21
|
end
|
22
22
|
|
23
|
+
class InvoiceCheckCountry < ModelBase
|
24
|
+
validates :vat_number, :valvat => {:match_country => :country}
|
25
|
+
|
26
|
+
def country
|
27
|
+
@attributes[:country]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
23
31
|
describe Invoice do
|
24
32
|
context "with valid vat number" do
|
25
|
-
before do
|
26
|
-
Valvat::Syntax.stub(:validate => true)
|
27
|
-
end
|
28
|
-
|
29
33
|
it "should be valid" do
|
30
|
-
Invoice.new(:vat_number => "
|
34
|
+
Invoice.new(:vat_number => "DE259597697").should be_valid
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
34
38
|
context "with invalid vat number" do
|
35
|
-
|
36
|
-
Valvat::Syntax.stub(:validate => false)
|
37
|
-
end
|
39
|
+
let(:invoice) { Invoice.new(:vat_number => "DE259597697123") }
|
38
40
|
|
39
41
|
it "should not be valid" do
|
40
|
-
|
42
|
+
invoice.should_not be_valid
|
41
43
|
end
|
42
44
|
|
43
45
|
it "should add default (country specific) error message" do
|
44
|
-
|
45
|
-
|
46
|
-
i.errors[:vat_number].should eql(["is not a valid german vat number"])
|
46
|
+
invoice.valid?
|
47
|
+
invoice.errors[:vat_number].should eql(["is not a valid german vat number"])
|
47
48
|
end
|
48
49
|
|
49
50
|
context "with i18n translation in place" do
|
@@ -56,9 +57,8 @@ describe Invoice do
|
|
56
57
|
after { I18n.reload! }
|
57
58
|
|
58
59
|
it "should use translation" do
|
59
|
-
|
60
|
-
|
61
|
-
i.errors[:vat_number].should eql(["is ugly."])
|
60
|
+
invoice.valid?
|
61
|
+
invoice.errors[:vat_number].should eql(["is ugly."])
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
@@ -72,15 +72,15 @@ describe Invoice do
|
|
72
72
|
after { I18n.reload! }
|
73
73
|
|
74
74
|
it "should replace country adjective placeholder" do
|
75
|
-
|
76
|
-
|
77
|
-
|
75
|
+
invoice = Invoice.new(:vat_number => "IE123")
|
76
|
+
invoice.valid?
|
77
|
+
invoice.errors[:vat_number].should eql(["is not a irish vat"])
|
78
78
|
end
|
79
79
|
|
80
80
|
it "should fall back to 'european' if country is missing" do
|
81
|
-
|
82
|
-
|
83
|
-
|
81
|
+
invoice = Invoice.new(:vat_number => "XX123")
|
82
|
+
invoice.valid?
|
83
|
+
invoice.errors[:vat_number].should eql(["is not a european vat"])
|
84
84
|
end
|
85
85
|
end
|
86
86
|
end
|
@@ -157,4 +157,29 @@ describe InvoiceAllowBlankOnAll do
|
|
157
157
|
InvoiceAllowBlankOnAll.new(:vat_number => nil).should be_valid
|
158
158
|
end
|
159
159
|
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe InvoiceCheckCountry do
|
163
|
+
context "with blank vat number" do
|
164
|
+
it "should be not valid on blank country" do
|
165
|
+
InvoiceCheckCountry.new(:country => nil, :vat_number => "DE259597697").should_not be_valid
|
166
|
+
InvoiceCheckCountry.new(:country => "", :vat_number => "DE259597697").should_not be_valid
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should be not valid on wired country" do
|
170
|
+
InvoiceCheckCountry.new(:country => "XAXXX", :vat_number => "DE259597697").should_not be_valid
|
171
|
+
InvoiceCheckCountry.new(:country => "ZO", :vat_number => "DE259597697").should_not be_valid
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should be not valid on mismatching (eu) country" do
|
175
|
+
InvoiceCheckCountry.new(:country => "FR", :vat_number => "DE259597697").should_not be_valid
|
176
|
+
InvoiceCheckCountry.new(:country => "AT", :vat_number => "DE259597697").should_not be_valid
|
177
|
+
InvoiceCheckCountry.new(:country => "DE", :vat_number => "ATU65931334").should_not be_valid
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should be valid on matching country" do
|
181
|
+
InvoiceCheckCountry.new(:country => "DE", :vat_number => "DE259597697").should be_valid
|
182
|
+
InvoiceCheckCountry.new(:country => "AT", :vat_number => "ATU65931334").should be_valid
|
183
|
+
end
|
184
|
+
end
|
160
185
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
- 2
|
8
7
|
- 3
|
9
|
-
|
8
|
+
- 0
|
9
|
+
version: 0.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Sebastian Munz
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-12 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|