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 CHANGED
@@ -1,12 +1,19 @@
1
1
  ### dev
2
2
 
3
- [full changelog](http://github.com/yolk/valvat/compare/v0.3.0...master)
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 _match_counrty_ option to validate if iso country code of vat number matches another attribute.
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
 
@@ -8,21 +8,28 @@ module ActiveModel
8
8
 
9
9
  def validate_each(record, attribute, value)
10
10
  vat = Valvat(value)
11
- is_valid = options[:lookup] ? vat.valid? && vat.exists? : vat.valid?
11
+ iso_country_code = vat.iso_country_code
12
+ is_valid = true
12
13
 
13
- if is_valid.nil?
14
- is_valid = options[:lookup] != :fail_if_down
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 && options[:match_country]
18
- is_valid = (record.send(options[:match_country]) || "").upcase == vat.iso_country_code
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.#{(Valvat::Utils.split(value)[0] || "eu").downcase}",
32
+ :"valvat.country_adjectives.#{iso_country_code.downcase}",
26
33
  :default => [:"valvat.country_adjectives.eu", "european"]
27
34
  )
28
35
  )
@@ -1,3 +1,3 @@
1
1
  class Valvat
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -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
- 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
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
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 0
9
- version: 0.3.0
8
+ - 1
9
+ version: 0.3.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Sebastian Munz