evelpidon_validators 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -0
- data/README.md +2 -0
- data/evelpidon_validators.gemspec +1 -0
- data/lib/assets/javascripts/evelpidon_validators/credit_card.js +7 -0
- data/lib/assets/javascripts/evelpidon_validators.js +1 -1
- data/lib/evelpidon_validators/all.rb +1 -0
- data/lib/evelpidon_validators/credit_card.rb +31 -0
- data/lib/evelpidon_validators/version.rb +1 -1
- data/test/credit_card_test.rb +98 -0
- metadata +26 -12
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -0,0 +1,7 @@
|
|
1
|
+
clientSideValidations.validators.remote['credit_card'] = function(element, options) {
|
2
|
+
if ($.ajax({
|
3
|
+
url: '/validators/credit_card.json',
|
4
|
+
data: { value: element.val(), type: clientSideValidations.helpers.getSiblingField(element, options.type).val() },
|
5
|
+
async: false
|
6
|
+
}).status == 404) { return options.message; }
|
7
|
+
};
|
@@ -4,6 +4,6 @@
|
|
4
4
|
clientSideValidations.helpers = clientSideValidations.helpers || {};
|
5
5
|
|
6
6
|
clientSideValidations.helpers.getSiblingField = function(field, siblingName) {
|
7
|
-
var thanName = field.attr('name').replace(/\[[^\]]*\]
|
7
|
+
var thanName = field.attr('name').replace(/\[[^\]]*\]$/, '[' + siblingName + ']');
|
8
8
|
return field.parents('form').find("[name='" + thanName + "']");
|
9
9
|
};
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'credit_card_validator'
|
2
|
+
|
3
|
+
if defined?(Rails) && Rails.env == 'development'
|
4
|
+
::CreditCardValidator::Validator.options[:test_numbers_are_valid] = true
|
5
|
+
end
|
6
|
+
|
7
|
+
module ActiveModel
|
8
|
+
module Validations
|
9
|
+
class CreditCardValidator < EachValidator
|
10
|
+
def self.valid_credit_card?(number, type)
|
11
|
+
::CreditCardValidator::Validator.options[:allowed_card_types] = [(type || '').underscore.to_sym]
|
12
|
+
::CreditCardValidator::Validator.valid?(number)
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate_each(record, attribute, value)
|
16
|
+
unless self.class.valid_credit_card? value, record.send(options[:type])
|
17
|
+
record.errors.add(attribute, :credit_card, options)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClientSideValidations::Middleware
|
25
|
+
class CreditCard < Base
|
26
|
+
def response
|
27
|
+
self.status = ActiveModel::Validations::CreditCardValidator.valid_credit_card?(request.params[:value], request.params[:type]) ? 200 : 404
|
28
|
+
super
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end if defined?(ClientSideValidations)
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'evelpidon_validators/credit_card'
|
3
|
+
|
4
|
+
class CreditCardValidatorTest < ActiveSupport::TestCase
|
5
|
+
|
6
|
+
class Payment
|
7
|
+
include ActiveModel::Validations
|
8
|
+
attr_accessor :card_type, :card_number
|
9
|
+
validates :card_number, :credit_card => { :type => :card_type }
|
10
|
+
end
|
11
|
+
|
12
|
+
setup do
|
13
|
+
CreditCardValidator::Validator.options[:test_numbers_are_valid] = true
|
14
|
+
@payment = Payment.new
|
15
|
+
end
|
16
|
+
|
17
|
+
test "visa cards" do
|
18
|
+
assert_valid_card_type_and_number 'Visa', '4111111111111111'
|
19
|
+
assert_valid_card_type_and_number 'visa', '4111111111111111'
|
20
|
+
|
21
|
+
assert_invalid_card_type_and_number 'MasterCard', '4111111111111111'
|
22
|
+
assert_invalid_card_type_and_number 'DinersClub', '4111111111111111'
|
23
|
+
assert_invalid_card_type_and_number 'Amex', '4111111111111111'
|
24
|
+
assert_invalid_card_type_and_number 'Discover', '4111111111111111'
|
25
|
+
assert_invalid_card_type_and_number 'Maestro', '4111111111111111'
|
26
|
+
end
|
27
|
+
|
28
|
+
test "master card cards" do
|
29
|
+
assert_valid_card_type_and_number 'MasterCard', '5555555555554444'
|
30
|
+
assert_valid_card_type_and_number 'master_card', '5555555555554444'
|
31
|
+
|
32
|
+
assert_invalid_card_type_and_number 'Visa', '5555555555554444'
|
33
|
+
assert_invalid_card_type_and_number 'DinersClub', '5555555555554444'
|
34
|
+
assert_invalid_card_type_and_number 'Amex', '5555555555554444'
|
35
|
+
assert_invalid_card_type_and_number 'Discover', '5555555555554444'
|
36
|
+
assert_invalid_card_type_and_number 'Maestro', '5555555555554444'
|
37
|
+
end
|
38
|
+
|
39
|
+
test "diners club cards" do
|
40
|
+
assert_valid_card_type_and_number 'DinersClub', '30569309025904'
|
41
|
+
assert_valid_card_type_and_number 'diners_club', '30569309025904'
|
42
|
+
|
43
|
+
assert_invalid_card_type_and_number 'Visa', '30569309025904'
|
44
|
+
assert_invalid_card_type_and_number 'MasterCard', '30569309025904'
|
45
|
+
assert_invalid_card_type_and_number 'Amex', '30569309025904'
|
46
|
+
assert_invalid_card_type_and_number 'Discover', '30569309025904'
|
47
|
+
assert_invalid_card_type_and_number 'Maestro', '30569309025904'
|
48
|
+
end
|
49
|
+
|
50
|
+
test "amex cards" do
|
51
|
+
assert_valid_card_type_and_number 'Amex', '371449635398431'
|
52
|
+
assert_valid_card_type_and_number 'amex', '371449635398431'
|
53
|
+
|
54
|
+
assert_invalid_card_type_and_number 'Visa', '371449635398431'
|
55
|
+
assert_invalid_card_type_and_number 'MasterCard', '371449635398431'
|
56
|
+
assert_invalid_card_type_and_number 'DinersClub', '371449635398431'
|
57
|
+
assert_invalid_card_type_and_number 'Discover', '371449635398431'
|
58
|
+
assert_invalid_card_type_and_number 'Maestro', '371449635398431'
|
59
|
+
end
|
60
|
+
|
61
|
+
test "discover cards" do
|
62
|
+
assert_valid_card_type_and_number 'Discover', '6011000990139424'
|
63
|
+
assert_valid_card_type_and_number 'discover', '6011000990139424'
|
64
|
+
|
65
|
+
assert_invalid_card_type_and_number 'Visa', '6011000990139424'
|
66
|
+
assert_invalid_card_type_and_number 'MasterCard', '6011000990139424'
|
67
|
+
assert_invalid_card_type_and_number 'DinersClub', '6011000990139424'
|
68
|
+
assert_invalid_card_type_and_number 'Amex', '6011000990139424'
|
69
|
+
assert_invalid_card_type_and_number 'Maestro', '6011000990139424'
|
70
|
+
end
|
71
|
+
|
72
|
+
test "maestro cards" do
|
73
|
+
assert_valid_card_type_and_number 'Maestro', '6759671431256542'
|
74
|
+
assert_valid_card_type_and_number 'maestro', '6759671431256542'
|
75
|
+
|
76
|
+
assert_invalid_card_type_and_number 'Visa', '6759671431256542'
|
77
|
+
assert_invalid_card_type_and_number 'MasterCard', '6759671431256542'
|
78
|
+
assert_invalid_card_type_and_number 'DinersClub', '6759671431256542'
|
79
|
+
assert_invalid_card_type_and_number 'Discover', '6759671431256542'
|
80
|
+
assert_invalid_card_type_and_number 'Discover', '6759671431256542'
|
81
|
+
end
|
82
|
+
|
83
|
+
#########
|
84
|
+
protected
|
85
|
+
#########
|
86
|
+
|
87
|
+
def assert_valid_card_type_and_number(type, number)
|
88
|
+
@payment.card_type = type
|
89
|
+
@payment.card_number = number
|
90
|
+
assert_valid_attribute @payment, :card_number
|
91
|
+
end
|
92
|
+
|
93
|
+
def assert_invalid_card_type_and_number(type, number)
|
94
|
+
@payment.card_type = type
|
95
|
+
@payment.card_number = number
|
96
|
+
assert_invalid_attribute @payment, :card_number
|
97
|
+
end
|
98
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: evelpidon_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2012-01-13 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
17
|
-
requirement: &
|
17
|
+
requirement: &16770240 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *16770240
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: activesupport
|
28
|
-
requirement: &
|
28
|
+
requirement: &16769800 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *16769800
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: evelpidon_test_helpers
|
39
|
-
requirement: &
|
39
|
+
requirement: &16769340 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: '0'
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *16769340
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: test-unit
|
50
|
-
requirement: &
|
50
|
+
requirement: &16768860 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,10 +55,10 @@ dependencies:
|
|
55
55
|
version: '0'
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *16768860
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
60
|
name: activemodel
|
61
|
-
requirement: &
|
61
|
+
requirement: &16768380 !ruby/object:Gem::Requirement
|
62
62
|
none: false
|
63
63
|
requirements:
|
64
64
|
- - ! '>='
|
@@ -66,7 +66,18 @@ dependencies:
|
|
66
66
|
version: '0'
|
67
67
|
type: :runtime
|
68
68
|
prerelease: false
|
69
|
-
version_requirements: *
|
69
|
+
version_requirements: *16768380
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: credit_card_validator
|
72
|
+
requirement: &16767920 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
type: :runtime
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: *16767920
|
70
81
|
description: Useful ActiveModel validators with ClientSideValidations support.
|
71
82
|
email:
|
72
83
|
- n.dimitrakopoulos@pamediakopes.gr
|
@@ -83,18 +94,21 @@ files:
|
|
83
94
|
- Rakefile
|
84
95
|
- evelpidon_validators.gemspec
|
85
96
|
- lib/assets/javascripts/evelpidon_validators.js
|
97
|
+
- lib/assets/javascripts/evelpidon_validators/credit_card.js
|
86
98
|
- lib/assets/javascripts/evelpidon_validators/different.js
|
87
99
|
- lib/assets/javascripts/evelpidon_validators/greater.js
|
88
100
|
- lib/assets/javascripts/evelpidon_validators/less.js
|
89
101
|
- lib/evelpidon_validators.rb
|
90
102
|
- lib/evelpidon_validators/all.rb
|
91
103
|
- lib/evelpidon_validators/associated.rb
|
104
|
+
- lib/evelpidon_validators/credit_card.rb
|
92
105
|
- lib/evelpidon_validators/different.rb
|
93
106
|
- lib/evelpidon_validators/greater.rb
|
94
107
|
- lib/evelpidon_validators/less.rb
|
95
108
|
- lib/evelpidon_validators/rails.rb
|
96
109
|
- lib/evelpidon_validators/version.rb
|
97
110
|
- test/associated_test.rb
|
111
|
+
- test/credit_card_test.rb
|
98
112
|
- test/different_test.rb
|
99
113
|
- test/greater_test.rb
|
100
114
|
- test/less_test.rb
|