Skip to content

Commit

Permalink
ActiveModel validation: added _match_counrty_ option to validate if i…
Browse files Browse the repository at this point in the history
…so country code of vat number matches another attribute.
  • Loading branch information
yolk committed Jan 12, 2011
1 parent aec7ef6 commit 68d9531
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[full changelog](http://github.com/yolk/valvat/compare/v0.2.3...master)

* ActiveModel validation: added _match_counrty_ option to validate if iso country code of vat number matches another attribute.

### 0.2.3 / 2011-01-10

[full changelog](http://github.com/yolk/valvat/compare/v0.2.2...v0.2.3)
Expand Down
10 changes: 8 additions & 2 deletions lib/valvat/active_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ class ValvatValidator < ::ActiveModel::EachValidator

def validate_each(record, attribute, value)
vat = Valvat(value)

is_valid = options[:lookup] ? vat.valid? && vat.exists? : vat.valid?
is_valid.nil? && is_valid = (options[:lookup] != :fail_if_down)

if is_valid.nil?
is_valid = options[:lookup] != :fail_if_down
end

if is_valid && options[:match_country]
is_valid = (record.send(options[:match_country]) || "").upcase == vat.iso_country_code
end

unless is_valid
record.errors.add(attribute, :invalid_vat,
Expand Down
67 changes: 46 additions & 21 deletions spec/valvat/active_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,31 @@ class InvoiceAllowBlankOnAll < ModelBase
validates :vat_number, :valvat => true, :allow_blank => true
end

class InvoiceCheckCountry < ModelBase
validates :vat_number, :valvat => {:match_country => :country}

def country
@attributes[:country]
end
end

describe Invoice do
context "with valid vat number" do
before do
Valvat::Syntax.stub(:validate => true)
end

it "should be valid" do
Invoice.new(:vat_number => "DE123").should be_valid
Invoice.new(:vat_number => "DE259597697").should be_valid
end
end

context "with invalid vat number" do
before do
Valvat::Syntax.stub(:validate => false)
end
let(:invoice) { Invoice.new(:vat_number => "DE259597697123") }

it "should not be valid" do
Invoice.new(:vat_number => "DE123").should_not be_valid
invoice.should_not be_valid
end

it "should add default (country specific) error message" do
i = Invoice.new(:vat_number => "DE123")
i.valid?
i.errors[:vat_number].should eql(["is not a valid german vat number"])
invoice.valid?
invoice.errors[:vat_number].should eql(["is not a valid german vat number"])
end

context "with i18n translation in place" do
Expand All @@ -56,9 +57,8 @@ class InvoiceAllowBlankOnAll < ModelBase
after { I18n.reload! }

it "should use translation" do
i = Invoice.new(:vat_number => "DE123")
i.valid?
i.errors[:vat_number].should eql(["is ugly."])
invoice.valid?
invoice.errors[:vat_number].should eql(["is ugly."])
end
end

Expand All @@ -72,15 +72,15 @@ class InvoiceAllowBlankOnAll < ModelBase
after { I18n.reload! }

it "should replace country adjective placeholder" do
i = Invoice.new(:vat_number => "IE123")
i.valid?
i.errors[:vat_number].should eql(["is not a irish vat"])
invoice = Invoice.new(:vat_number => "IE123")
invoice.valid?
invoice.errors[:vat_number].should eql(["is not a irish vat"])
end

it "should fall back to 'european' if country is missing" do
i = Invoice.new(:vat_number => "XX123")
i.valid?
i.errors[:vat_number].should eql(["is not a european vat"])
invoice = Invoice.new(:vat_number => "XX123")
invoice.valid?
invoice.errors[:vat_number].should eql(["is not a european vat"])
end
end
end
Expand Down Expand Up @@ -157,4 +157,29 @@ class InvoiceAllowBlankOnAll < ModelBase
InvoiceAllowBlankOnAll.new(:vat_number => nil).should be_valid
end
end
end

describe InvoiceCheckCountry do
context "with blank vat number" do
it "should be not valid on blank country" do
InvoiceCheckCountry.new(:country => nil, :vat_number => "DE259597697").should_not be_valid
InvoiceCheckCountry.new(:country => "", :vat_number => "DE259597697").should_not be_valid
end

it "should be not valid on wired country" do
InvoiceCheckCountry.new(:country => "XAXXX", :vat_number => "DE259597697").should_not be_valid
InvoiceCheckCountry.new(:country => "ZO", :vat_number => "DE259597697").should_not be_valid
end

it "should be not valid on mismatching (eu) country" do
InvoiceCheckCountry.new(:country => "FR", :vat_number => "DE259597697").should_not be_valid
InvoiceCheckCountry.new(:country => "AT", :vat_number => "DE259597697").should_not be_valid
InvoiceCheckCountry.new(:country => "DE", :vat_number => "ATU65931334").should_not be_valid
end

it "should be valid on matching country" do
InvoiceCheckCountry.new(:country => "DE", :vat_number => "DE259597697").should be_valid
InvoiceCheckCountry.new(:country => "AT", :vat_number => "ATU65931334").should be_valid
end
end
end

0 comments on commit 68d9531

Please sign in to comment.