common_numbers_rails 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,12 +24,21 @@ For standalone model use [common_numbers](http://github.com/marioosh/common_numb
24
24
  For ActiveRecord Models:
25
25
 
26
26
  class Item < ActiveRecord::Base
27
- validates :nip, :presence => true, :nip => true
27
+ validates :nip, :nip => true
28
+ validates :pesel, :pesel => true
29
+ validates :regon, :regon => true
28
30
  end
29
31
 
32
+ You can also use helpers:
33
+
34
+ class Item < ActiveRecord::Base
35
+ validates_pesel_of :pesel_field
36
+ validates_pesel_of :pesel_field, :message => "Our custom message"
37
+ end
38
+
39
+
30
40
  ###TODO:
31
41
 
32
- - I18n messages
33
42
  - validators for other numbers
34
43
 
35
44
 
@@ -4,7 +4,24 @@ require 'active_model'
4
4
  class NipValidator < ActiveModel::EachValidator
5
5
 
6
6
  def validate_each(record, attribute, value)
7
- record.errors[attribute] << "invalid format" unless CommonNumbers::Polish::Nip.new(value).valid?
7
+ @message = options[:message] || "invalid format"
8
+ record.errors[attribute] << @message unless CommonNumbers::Polish::Nip.new(value).valid?
9
+ end
10
+
11
+ end
12
+
13
+ module ActiveModel
14
+
15
+ module Validations
16
+
17
+ module HelperMethods
18
+
19
+ def validates_nip_of(*attr_names)
20
+ validates_with NipValidator, _merge_attributes(attr_names)
21
+ end
22
+
23
+ end
24
+
8
25
  end
9
26
 
10
27
  end
@@ -4,7 +4,24 @@ require 'active_model'
4
4
  class PeselValidator < ActiveModel::EachValidator
5
5
 
6
6
  def validate_each(record, attribute, value)
7
- record.errors[attribute] << "invalid format" unless CommonNumbers::Polish::Pesel.new(value).valid?
7
+ @message = options[:message] || "invalid format"
8
+ record.errors.add(attribute, @message) unless CommonNumbers::Polish::Pesel.new(value).valid?
9
+ end
10
+
11
+ end
12
+
13
+ module ActiveModel
14
+
15
+ module Validations
16
+
17
+ module HelperMethods
18
+
19
+ def validates_pesel_of(*attr_names)
20
+ validates_with PeselValidator, _merge_attributes(attr_names)
21
+ end
22
+
23
+ end
24
+
8
25
  end
9
26
 
10
27
  end
@@ -4,7 +4,24 @@ require 'active_model'
4
4
  class RegonValidator < ActiveModel::EachValidator
5
5
 
6
6
  def validate_each(record, attribute, value)
7
+ @message = options[:message] || "invalid format"
7
8
  record.errors[attribute] << "invalid format" unless CommonNumbers::Polish::Regon.new(value).valid?
8
9
  end
9
10
 
10
11
  end
12
+
13
+ module ActiveModel
14
+
15
+ module Validations
16
+
17
+ module HelperMethods
18
+
19
+ def validates_regon_of(*attr_names)
20
+ validates_with RegonValidator, _merge_attributes(attr_names)
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -1,3 +1,3 @@
1
1
  module CommonNumbersRails
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -5,25 +5,66 @@ require 'active_model'
5
5
  class BasicNipModel
6
6
  include ActiveModel::Validations
7
7
 
8
- attr_accessor :nip
8
+ attr_accessor :nip, :nip2
9
9
 
10
10
  validates :nip, :presence => true, :nip => true
11
+
12
+ validates_nip_of :nip2
11
13
  end
12
14
 
13
15
 
14
16
  describe "NipValidator" do
15
17
  before(:each) do
18
+ @valid_nip = "123-456-32-18"
19
+ @invalid_nip = "123-456-32-12"
20
+
16
21
  @model = BasicNipModel.new
22
+
17
23
  end
18
24
 
19
- it "should be valid" do
20
- @model.should_not be_valid
21
- @model.nip = "123-456-32-18"
22
- @model.should be_valid
25
+ describe "with nip1" do
26
+ before(:each) do
27
+ @model.nip2 = @valid_nip
28
+ end
29
+
30
+ it "should be valid" do
31
+ @model.should_not be_valid
32
+ @model.nip = @valid_nip
33
+ @model.should be_valid
34
+ end
35
+
36
+ it "should be invalid" do
37
+ @model.nip = @invalid_nip
38
+ @model.should_not be_valid
39
+ end
23
40
  end
24
-
25
- it "should be invalid" do
26
- @model.nip = "123-456-32-12"
27
- @model.should_not be_valid
41
+
42
+ describe "with nip2" do
43
+ before(:each) do
44
+ @model.nip = @valid_nip
45
+ end
46
+
47
+ it "should be valid" do
48
+ @model.should be_invalid
49
+ @model.nip2 = @valid_nip
50
+ @model.should be_valid
51
+ end
52
+
53
+ it "should be invalid when nil" do
54
+ @model.nip2 = nil
55
+ @model.should be_invalid
56
+ end
57
+
58
+ it "should be invalid when empty" do
59
+ @model.nip2 = ""
60
+ @model.should be_invalid
61
+ end
62
+
63
+ it "should be invalid when invalid NIP" do
64
+ @model.nip2 = @invalid_nip
65
+ @model.should be_invalid
66
+ end
67
+
28
68
  end
69
+
29
70
  end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'spec_helper'
2
3
  require 'rspec'
3
4
  require 'active_model'
@@ -5,25 +6,69 @@ require 'active_model'
5
6
  class BasicPeselModel
6
7
  include ActiveModel::Validations
7
8
 
8
- attr_accessor :pesel
9
+ attr_accessor :pesel, :pesel2
9
10
 
10
11
  validates :pesel, :presence => true, :pesel => true
12
+
13
+ validates_pesel_of :pesel2
14
+
11
15
  end
12
16
 
13
17
 
14
18
  describe "PeselValidator" do
15
19
  before(:each) do
20
+ @valid_pesel = "44051401359"
21
+ @invalid_pesel = "44051401353"
22
+
16
23
  @model = BasicPeselModel.new
24
+
17
25
  end
26
+ describe "with pesel" do
18
27
 
19
- it "should be valid" do
20
- @model.should_not be_valid
21
- @model.pesel = "44051401359"
22
- @model.should be_valid
28
+ before(:each) do
29
+ @model.pesel2 = @valid_pesel
30
+ end
31
+
32
+ it "should be valid" do
33
+ @model.should_not be_valid
34
+ @model.pesel = @valid_pesel
35
+ @model.should be_valid
36
+ end
37
+
38
+ it "should be invalid" do
39
+ @model.pesel = @invalid_pesel
40
+ @model.should_not be_valid
41
+ end
23
42
  end
24
-
25
- it "should be invalid" do
26
- @model.pesel = "44051401353"
27
- @model.should_not be_valid
43
+
44
+ describe "with pesel2" do
45
+ before(:each) do
46
+ @model.pesel = @valid_pesel
47
+ end
48
+
49
+ it "should be valid" do
50
+ @model.should be_invalid
51
+ @model.pesel2 = @valid_pesel
52
+ @model.should be_valid
53
+ end
54
+
55
+ it "should be invalid when nil" do
56
+ @model.pesel2 = nil
57
+ @model.should be_invalid
58
+ end
59
+ it "should be invalid when empty" do
60
+ @model.pesel2 = ""
61
+ @model.should be_invalid
62
+ end
63
+
64
+ it "should be invalid when invalid number" do
65
+ @model.pesel2 = @invalid_pesel
66
+ @model.should be_invalid
67
+ end
68
+
69
+
28
70
  end
71
+
72
+
29
73
  end
74
+
@@ -5,25 +5,64 @@ require 'active_model'
5
5
  class BasicRegonModel
6
6
  include ActiveModel::Validations
7
7
 
8
- attr_accessor :regon
8
+ attr_accessor :regon, :regon2
9
9
 
10
10
  validates :regon, :presence => true, :regon => true
11
+
12
+ validates_regon_of :regon2
11
13
  end
12
14
 
13
15
 
14
16
  describe "RegonValidator" do
15
17
  before(:each) do
18
+ @valid_regon = "192598184"
19
+ @invalid_regon = "192598134"
20
+
16
21
  @model = BasicRegonModel.new
17
22
  end
18
23
 
19
- it "should be valid" do
20
- @model.should_not be_valid
21
- @model.regon = "192598184"
22
- @model.should be_valid
24
+ describe "with regon 1" do
25
+ before(:each) do
26
+ @model.regon2 = @valid_regon
27
+ end
28
+
29
+ it "should be valid" do
30
+ @model.should_not be_valid
31
+ @model.regon = @valid_regon
32
+ @model.should be_valid
33
+ end
34
+
35
+ it "should be invalid" do
36
+ @model.regon = @invalid_regon
37
+ @model.should_not be_valid
38
+ end
23
39
  end
24
-
25
- it "should be invalid" do
26
- @model.regon = "192598183"
27
- @model.should_not be_valid
40
+
41
+ describe "with regon 2" do
42
+ before(:each) do
43
+ @model.regon = @valid_regon
44
+ end
45
+
46
+ it "should be valid" do
47
+ @model.should be_invalid
48
+ @model.regon2 = @valid_regon
49
+ @model.should be_valid
50
+ end
51
+
52
+ it "should be invalid when nil" do
53
+ @model.regon2 = nil
54
+ @model.should be_invalid
55
+ end
56
+
57
+ it "should be invalid when empty" do
58
+ @model.regon2 == ""
59
+ @model.should be_invalid
60
+ end
61
+
62
+ it "should be invalid when invalid regon" do
63
+ @model.regon2 == @invalid_regon
64
+ @model.should be_invalid
65
+ end
66
+
28
67
  end
29
68
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: common_numbers_rails
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
5
+ version: 0.1.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mariusz Nosinski