valvat 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.md +9 -2
- data/lib/valvat/active_model.rb +13 -6
- data/lib/valvat/version.rb +1 -1
- data/spec/valvat/active_model_spec.rb +58 -21
- metadata +2 -2
data/CHANGES.md
CHANGED
@@ -1,12 +1,19 @@
|
|
1
1
|
### dev
|
2
2
|
|
3
|
-
[full changelog](http://github.com/yolk/valvat/compare/v0.3.
|
3
|
+
[full changelog](http://github.com/yolk/valvat/compare/v0.3.1...master)
|
4
|
+
|
5
|
+
### 0.3.1 / 2011-01-12
|
6
|
+
|
7
|
+
[full changelog](http://github.com/yolk/valvat/compare/v0.3,0...v0.3.1)
|
8
|
+
|
9
|
+
* ActiveModel validation: Failed validations with _match_country_ now use error message with country from given attribute
|
10
|
+
* ActiveModel validation: Failed validations with _match_country_ skip lookup and syntax checks
|
4
11
|
|
5
12
|
### 0.3.0 / 2011-01-12
|
6
13
|
|
7
14
|
[full changelog](http://github.com/yolk/valvat/compare/v0.2.3...v0.3.0)
|
8
15
|
|
9
|
-
* ActiveModel validation: added
|
16
|
+
* ActiveModel validation: added _match_country_ option to validate if iso country code of vat number matches another attribute.
|
10
17
|
|
11
18
|
### 0.2.3 / 2011-01-10
|
12
19
|
|
data/lib/valvat/active_model.rb
CHANGED
@@ -8,21 +8,28 @@ module ActiveModel
|
|
8
8
|
|
9
9
|
def validate_each(record, attribute, value)
|
10
10
|
vat = Valvat(value)
|
11
|
-
|
11
|
+
iso_country_code = vat.iso_country_code
|
12
|
+
is_valid = true
|
12
13
|
|
13
|
-
if
|
14
|
-
|
14
|
+
if options[:match_country]
|
15
|
+
iso_country_code = (record.send(options[:match_country]) || "").upcase
|
16
|
+
is_valid = iso_country_code == vat.iso_country_code
|
15
17
|
end
|
16
18
|
|
17
|
-
if is_valid
|
18
|
-
is_valid =
|
19
|
+
if is_valid
|
20
|
+
is_valid = options[:lookup] ? vat.valid? && vat.exists? : vat.valid?
|
21
|
+
|
22
|
+
if is_valid.nil?
|
23
|
+
is_valid = options[:lookup] != :fail_if_down
|
24
|
+
end
|
19
25
|
end
|
20
26
|
|
21
27
|
unless is_valid
|
28
|
+
iso_country_code = "eu" if iso_country_code.blank?
|
22
29
|
record.errors.add(attribute, :invalid_vat,
|
23
30
|
:message => options[:message],
|
24
31
|
:country_adjective => I18n.t(
|
25
|
-
:"valvat.country_adjectives.#{
|
32
|
+
:"valvat.country_adjectives.#{iso_country_code.downcase}",
|
26
33
|
:default => [:"valvat.country_adjectives.eu", "european"]
|
27
34
|
)
|
28
35
|
)
|
data/lib/valvat/version.rb
CHANGED
@@ -28,6 +28,14 @@ class InvoiceCheckCountry < ModelBase
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
+
class InvoiceCheckCountryWithLookup < ModelBase
|
32
|
+
validates :vat_number, :valvat => {:match_country => :country, :lookup => true}
|
33
|
+
|
34
|
+
def country
|
35
|
+
@attributes[:country]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
31
39
|
describe Invoice do
|
32
40
|
context "with valid vat number" do
|
33
41
|
it "should be valid" do
|
@@ -160,26 +168,55 @@ describe InvoiceAllowBlankOnAll do
|
|
160
168
|
end
|
161
169
|
|
162
170
|
describe InvoiceCheckCountry do
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
171
|
+
it "should be not valid on blank country" do
|
172
|
+
InvoiceCheckCountry.new(:country => nil, :vat_number => "DE259597697").should_not be_valid
|
173
|
+
InvoiceCheckCountry.new(:country => "", :vat_number => "DE259597697").should_not be_valid
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should be not valid on wired country" do
|
177
|
+
InvoiceCheckCountry.new(:country => "XAXXX", :vat_number => "DE259597697").should_not be_valid
|
178
|
+
InvoiceCheckCountry.new(:country => "ZO", :vat_number => "DE259597697").should_not be_valid
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should be not valid on mismatching (eu) country" do
|
182
|
+
InvoiceCheckCountry.new(:country => "FR", :vat_number => "DE259597697").should_not be_valid
|
183
|
+
InvoiceCheckCountry.new(:country => "AT", :vat_number => "DE259597697").should_not be_valid
|
184
|
+
InvoiceCheckCountry.new(:country => "DE", :vat_number => "ATU65931334").should_not be_valid
|
185
|
+
end
|
186
|
+
|
187
|
+
it "should be valid on matching country" do
|
188
|
+
InvoiceCheckCountry.new(:country => "DE", :vat_number => "DE259597697").should be_valid
|
189
|
+
InvoiceCheckCountry.new(:country => "AT", :vat_number => "ATU65931334").should be_valid
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should give back error message with country from :country_match" do
|
193
|
+
invoice = InvoiceCheckCountry.new(:country => "FR", :vat_number => "DE259597697")
|
194
|
+
invoice.valid?
|
195
|
+
invoice.errors[:vat_number].should eql(["is not a valid french vat number"])
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should give back error message with country from :country_match even on invalid vat number" do
|
199
|
+
invoice = InvoiceCheckCountry.new(:country => "FR", :vat_number => "DE259597697123")
|
200
|
+
invoice.valid?
|
201
|
+
invoice.errors[:vat_number].should eql(["is not a valid french vat number"])
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
describe InvoiceCheckCountryWithLookup do
|
206
|
+
before do
|
207
|
+
Valvat::Syntax.stub(:validate => true)
|
208
|
+
Valvat::Lookup.stub(:validate => true)
|
209
|
+
end
|
210
|
+
|
211
|
+
it "avoids lookup or syntax check on failed because of mismatching country" do
|
212
|
+
Valvat::Syntax.should_not_receive(:validate)
|
213
|
+
Valvat::Lookup.should_not_receive(:validate)
|
214
|
+
InvoiceCheckCountryWithLookup.new(:country => "FR", :vat_number => "DE259597697").valid?
|
215
|
+
end
|
216
|
+
|
217
|
+
it "check syntax and looup on matching country" do
|
218
|
+
Valvat::Syntax.should_receive(:validate).and_return(true)
|
219
|
+
Valvat::Lookup.should_receive(:validate).and_return(true)
|
220
|
+
InvoiceCheckCountryWithLookup.new(:country => "DE", :vat_number => "DE259597697").valid?
|
184
221
|
end
|
185
222
|
end
|