nimboids-client_side_validations 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/client_side_validations.gemspec +45 -0
- data/javascript/rails.validations.js +393 -0
- data/lib/client_side_validations.rb +8 -0
- data/lib/client_side_validations/action_view.rb +13 -0
- data/lib/client_side_validations/action_view/form_builder.rb +64 -0
- data/lib/client_side_validations/action_view/form_helper.rb +69 -0
- data/lib/client_side_validations/action_view/form_tag_helper.rb +12 -0
- data/lib/client_side_validations/active_model.rb +52 -0
- data/lib/client_side_validations/active_model/acceptance.rb +10 -0
- data/lib/client_side_validations/active_model/exclusion.rb +15 -0
- data/lib/client_side_validations/active_model/format.rb +10 -0
- data/lib/client_side_validations/active_model/inclusion.rb +15 -0
- data/lib/client_side_validations/active_model/length.rb +22 -0
- data/lib/client_side_validations/active_model/numericality.rb +26 -0
- data/lib/client_side_validations/active_model/presence.rb +10 -0
- data/lib/client_side_validations/active_record.rb +11 -0
- data/lib/client_side_validations/active_record/middleware.rb +25 -0
- data/lib/client_side_validations/active_record/uniqueness.rb +26 -0
- data/lib/client_side_validations/core_ext.rb +3 -0
- data/lib/client_side_validations/core_ext/range.rb +10 -0
- data/lib/client_side_validations/core_ext/regexp.rb +14 -0
- data/lib/client_side_validations/formtastic.rb +21 -0
- data/lib/client_side_validations/middleware.rb +83 -0
- data/lib/client_side_validations/mongoid.rb +9 -0
- data/lib/client_side_validations/mongoid/middleware.rb +20 -0
- data/lib/client_side_validations/mongoid/uniqueness.rb +26 -0
- data/lib/client_side_validations/simple_form.rb +24 -0
- data/lib/client_side_validations/version.rb +3 -0
- data/lib/generators/client_side_validations/install_generator.rb +22 -0
- data/lib/generators/templates/README +7 -0
- data/lib/generators/templates/client_side_validations.rb +14 -0
- data/test/action_view/cases/helper.rb +152 -0
- data/test/action_view/cases/test_helpers.rb +237 -0
- data/test/action_view/cases/test_legacy_helpers.rb +150 -0
- data/test/action_view/models.rb +3 -0
- data/test/action_view/models/comment.rb +35 -0
- data/test/action_view/models/post.rb +35 -0
- data/test/active_model/cases/helper.rb +4 -0
- data/test/active_model/cases/test_acceptance_validator.rb +16 -0
- data/test/active_model/cases/test_base.rb +11 -0
- data/test/active_model/cases/test_confirmation_validator.rb +16 -0
- data/test/active_model/cases/test_exclusion_validator.rb +20 -0
- data/test/active_model/cases/test_format_validator.rb +21 -0
- data/test/active_model/cases/test_inclusion_validator.rb +21 -0
- data/test/active_model/cases/test_length_validator.rb +61 -0
- data/test/active_model/cases/test_numericality_validator.rb +46 -0
- data/test/active_model/cases/test_presence_validator.rb +16 -0
- data/test/active_model/cases/test_validations.rb +151 -0
- data/test/active_model/models/person.rb +17 -0
- data/test/active_record/cases/helper.rb +12 -0
- data/test/active_record/cases/test_base.rb +11 -0
- data/test/active_record/cases/test_middleware.rb +150 -0
- data/test/active_record/cases/test_uniqueness_validator.rb +45 -0
- data/test/active_record/models/guid.rb +7 -0
- data/test/active_record/models/user.rb +10 -0
- data/test/base_helper.rb +6 -0
- data/test/core_ext/cases/test_core_ext.rb +45 -0
- data/test/formtastic/cases/helper.rb +2 -0
- data/test/formtastic/cases/test_form_builder.rb +11 -0
- data/test/formtastic/cases/test_form_helper.rb +22 -0
- data/test/generators/cases/test_install_generator.rb +15 -0
- data/test/javascript/config.ru +3 -0
- data/test/javascript/public/test/callbacks/elementAfter.js +54 -0
- data/test/javascript/public/test/callbacks/elementBefore.js +54 -0
- data/test/javascript/public/test/callbacks/elementFail.js +70 -0
- data/test/javascript/public/test/callbacks/elementPass.js +70 -0
- data/test/javascript/public/test/callbacks/formAfter.js +45 -0
- data/test/javascript/public/test/callbacks/formBefore.js +45 -0
- data/test/javascript/public/test/callbacks/formFail.js +51 -0
- data/test/javascript/public/test/callbacks/formPass.js +50 -0
- data/test/javascript/public/test/form_builders/validateForm.js +66 -0
- data/test/javascript/public/test/form_builders/validateFormtastic.js +54 -0
- data/test/javascript/public/test/form_builders/validateSimpleForm.js +57 -0
- data/test/javascript/public/test/settings.js +15 -0
- data/test/javascript/public/test/validateElement.js +144 -0
- data/test/javascript/public/test/validators/acceptance.js +42 -0
- data/test/javascript/public/test/validators/confirmation.js +25 -0
- data/test/javascript/public/test/validators/exclusion.js +41 -0
- data/test/javascript/public/test/validators/format.js +27 -0
- data/test/javascript/public/test/validators/inclusion.js +42 -0
- data/test/javascript/public/test/validators/length.js +70 -0
- data/test/javascript/public/test/validators/numericality.js +140 -0
- data/test/javascript/public/test/validators/presence.js +15 -0
- data/test/javascript/public/test/validators/uniqueness.js +89 -0
- data/test/javascript/public/vendor/jquery.metadata.js +122 -0
- data/test/javascript/public/vendor/qunit.css +196 -0
- data/test/javascript/public/vendor/qunit.js +1374 -0
- data/test/javascript/server.rb +78 -0
- data/test/javascript/views/index.erb +20 -0
- data/test/javascript/views/layout.erb +21 -0
- data/test/middleware/cases/helper.rb +15 -0
- data/test/middleware/cases/test_middleware.rb +8 -0
- data/test/mongoid/cases/helper.rb +16 -0
- data/test/mongoid/cases/test_base.rb +15 -0
- data/test/mongoid/cases/test_middleware.rb +68 -0
- data/test/mongoid/cases/test_uniqueness_validator.rb +44 -0
- data/test/mongoid/models/book.rb +8 -0
- data/test/simple_form/cases/helper.rb +2 -0
- data/test/simple_form/cases/test_form_builder.rb +14 -0
- data/test/simple_form/cases/test_form_helper.rb +22 -0
- metadata +435 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require 'active_model/cases/test_base'
|
|
2
|
+
|
|
3
|
+
class ActiveModel::InclusionValidatorTest < ClientSideValidations::ActiveModelTestBase
|
|
4
|
+
|
|
5
|
+
def test_presence_client_side_hash
|
|
6
|
+
expected_hash = { :message => "can't be blank" }
|
|
7
|
+
assert_equal expected_hash, PresenceValidator.new(:attributes => [:name]).client_side_hash(@person, :age)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def test_presence_client_side_hash_with_custom_message
|
|
11
|
+
expected_hash = { :message => "is required" }
|
|
12
|
+
assert_equal expected_hash, PresenceValidator.new(:attributes => [:name], :message => "is required").client_side_hash(@person, :age)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
end
|
|
16
|
+
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
require 'active_model/cases/test_base'
|
|
2
|
+
|
|
3
|
+
class ActiveModel::ValidationsTest < ClientSideValidations::ActiveModelTestBase
|
|
4
|
+
|
|
5
|
+
class Person
|
|
6
|
+
include ::ActiveModel::Validations
|
|
7
|
+
attr_accessor :first_name, :last_name, :age, :weight
|
|
8
|
+
|
|
9
|
+
def self.name
|
|
10
|
+
"Person"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def new_record?
|
|
14
|
+
true
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def new_person
|
|
19
|
+
person = Class.new(Person)
|
|
20
|
+
yield(person)
|
|
21
|
+
person.new
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_validations_to_client_side_hash
|
|
25
|
+
person = new_person do |p|
|
|
26
|
+
p.validates_presence_of :first_name
|
|
27
|
+
end
|
|
28
|
+
expected_hash = {
|
|
29
|
+
:first_name => {
|
|
30
|
+
:presence => {
|
|
31
|
+
:message => "can't be blank"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
assert_equal expected_hash, person.client_side_validation_hash
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_validations_to_client_side_hash_with_validations_allow_blank
|
|
39
|
+
person = new_person do |p|
|
|
40
|
+
p.validates_length_of :first_name, :is => 10, :allow_blank => true
|
|
41
|
+
p.validates_format_of :first_name, :with => //, :allow_blank => true
|
|
42
|
+
end
|
|
43
|
+
expected_hash = {
|
|
44
|
+
:first_name => {
|
|
45
|
+
:length => {
|
|
46
|
+
:messages => { :is => 'is the wrong length (should be 10 characters)'},
|
|
47
|
+
:is => 10,
|
|
48
|
+
:allow_blank => true
|
|
49
|
+
},
|
|
50
|
+
:format => {
|
|
51
|
+
:message => 'is invalid',
|
|
52
|
+
:with => //,
|
|
53
|
+
:allow_blank => true
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
assert_equal expected_hash, person.client_side_validation_hash
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def test_validations_to_client_side_hash_with_validations_on_create
|
|
61
|
+
person = new_person do |p|
|
|
62
|
+
p.validates_length_of :first_name, :is => 10, :on => :create
|
|
63
|
+
p.validates_length_of :last_name, :is => 10, :on => :update
|
|
64
|
+
p.validates_format_of :first_name, :with => //, :on => :update
|
|
65
|
+
p.validates_format_of :last_name, :with => //, :on => :create
|
|
66
|
+
p.validates_numericality_of :age, :on => :create
|
|
67
|
+
p.validates_numericality_of :weight, :on => :update
|
|
68
|
+
p.class_eval do
|
|
69
|
+
def new_record?
|
|
70
|
+
true
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
expected_hash = {
|
|
75
|
+
:first_name => {
|
|
76
|
+
:length => {
|
|
77
|
+
:messages => { :is => 'is the wrong length (should be 10 characters)'},
|
|
78
|
+
:is => 10,
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
:last_name => {
|
|
82
|
+
:format => {
|
|
83
|
+
:message => 'is invalid',
|
|
84
|
+
:with => //,
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
:age => {
|
|
88
|
+
:numericality => {
|
|
89
|
+
:messages => { :numericality => 'is not a number' },
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
assert_equal expected_hash, person.client_side_validation_hash
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def test_validations_to_client_side_hash_with_validations_on_update
|
|
97
|
+
person = new_person do |p|
|
|
98
|
+
p.validates_length_of :first_name, :is => 10, :on => :update
|
|
99
|
+
p.validates_length_of :last_name, :is => 10, :on => :create
|
|
100
|
+
p.validates_format_of :first_name, :with => //, :on => :create
|
|
101
|
+
p.validates_format_of :last_name, :with => //, :on => :update
|
|
102
|
+
p.validates_numericality_of :age, :on => :update
|
|
103
|
+
p.validates_numericality_of :weight, :on => :create
|
|
104
|
+
p.class_eval do
|
|
105
|
+
def new_record?
|
|
106
|
+
false
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
expected_hash = {
|
|
111
|
+
:first_name => {
|
|
112
|
+
:length => {
|
|
113
|
+
:messages => { :is => 'is the wrong length (should be 10 characters)'},
|
|
114
|
+
:is => 10,
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
:last_name => {
|
|
118
|
+
:format => {
|
|
119
|
+
:message => 'is invalid',
|
|
120
|
+
:with => //,
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
:age => {
|
|
124
|
+
:numericality => {
|
|
125
|
+
:messages => { :numericality => 'is not a number' },
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
assert_equal expected_hash, person.client_side_validation_hash
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def test_validates_with_should_be_ignored
|
|
133
|
+
person = new_person do |p|
|
|
134
|
+
p.validates_with PersonValidator
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
expected_hash = {}
|
|
138
|
+
assert_equal expected_hash, person.client_side_validation_hash
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def test_validators_with_if_or_unless_are_ignored
|
|
142
|
+
person = new_person do |p|
|
|
143
|
+
p.validates_presence_of :first_name, :if => Proc.new { |p| true }
|
|
144
|
+
p.validates_presence_of :last_name, :unless => Proc.new { |p| true }
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
expected_hash = {}
|
|
148
|
+
assert_equal expected_hash, person.client_side_validation_hash
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
class PersonValidator < ActiveModel::Validator
|
|
2
|
+
def validate(record)
|
|
3
|
+
end
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
class Person
|
|
7
|
+
include ActiveModel::Validations
|
|
8
|
+
|
|
9
|
+
attr_accessor :first_name, :last_name, :email, :age
|
|
10
|
+
|
|
11
|
+
validates_presence_of :first_name
|
|
12
|
+
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
|
|
13
|
+
|
|
14
|
+
def new_record?
|
|
15
|
+
true
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
require 'base_helper'
|
|
2
|
+
require 'active_record'
|
|
3
|
+
require 'client_side_validations/active_record'
|
|
4
|
+
|
|
5
|
+
# Connection must be establised before anything else
|
|
6
|
+
ActiveRecord::Base.establish_connection(
|
|
7
|
+
:adapter => defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3',
|
|
8
|
+
:database => ':memory:'
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
require 'active_record/models/user'
|
|
12
|
+
require 'active_record/models/guid'
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'middleware/cases/helper'
|
|
4
|
+
require 'active_record/cases/helper'
|
|
5
|
+
|
|
6
|
+
class ClientSideValidationsActiveRecordMiddlewareTest < Test::Unit::TestCase
|
|
7
|
+
include Rack::Test::Methods
|
|
8
|
+
|
|
9
|
+
def teardown
|
|
10
|
+
User.delete_all
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def with_kcode(kcode)
|
|
14
|
+
if RUBY_VERSION < '1.9'
|
|
15
|
+
orig_kcode, $KCODE = $KCODE, kcode
|
|
16
|
+
begin
|
|
17
|
+
yield
|
|
18
|
+
ensure
|
|
19
|
+
$KCODE = orig_kcode
|
|
20
|
+
end
|
|
21
|
+
else
|
|
22
|
+
yield
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def app
|
|
27
|
+
app = Proc.new { |env| [200, {}, ['success']] }
|
|
28
|
+
ClientSideValidations::Middleware::Validators.new(app)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_uniqueness_when_resource_exists
|
|
32
|
+
User.create(:email => 'user@test.com')
|
|
33
|
+
get '/validators/uniqueness.json', { 'user[email]' => 'user@test.com', 'case_sensitive' => true }
|
|
34
|
+
|
|
35
|
+
assert_equal 'false', last_response.body
|
|
36
|
+
assert last_response.ok?
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_uniqueness_when_resource_exists_and_param_order_is_backwards
|
|
40
|
+
User.create(:email => 'user@test.com')
|
|
41
|
+
get '/validators/uniqueness.json', { 'case_sensitive' => true, 'user[email]' => 'user@test.com' }
|
|
42
|
+
|
|
43
|
+
assert_equal 'false', last_response.body
|
|
44
|
+
assert last_response.ok?
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_uniqueness_when_resource_does_not_exist
|
|
48
|
+
get '/validators/uniqueness.json', { 'user[email]' => 'user@test.com', 'case_sensitive' => true }
|
|
49
|
+
|
|
50
|
+
assert_equal 'true', last_response.body
|
|
51
|
+
assert last_response.not_found?
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def test_uniqueness_when_id_is_given
|
|
55
|
+
user = User.create(:email => 'user@test.com')
|
|
56
|
+
get '/validators/uniqueness.json', { 'user[email]' => 'user@test.com', 'id' => user.id, 'case_sensitive' => true }
|
|
57
|
+
|
|
58
|
+
assert_equal 'true', last_response.body
|
|
59
|
+
assert last_response.not_found?
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_uniqueness_when_scope_is_given
|
|
63
|
+
User.create(:email => 'user@test.com', :age => 25)
|
|
64
|
+
get '/validators/uniqueness.json', { 'user[email]' => 'user@test.com', 'scope' => { 'age' => 30 }, 'case_sensitive' => true }
|
|
65
|
+
|
|
66
|
+
assert_equal 'true', last_response.body
|
|
67
|
+
assert last_response.not_found?
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def test_uniqueness_when_multiple_scopes_are_given
|
|
71
|
+
User.create(:email => 'user@test.com', :age => 30, :name => 'Brian')
|
|
72
|
+
get '/validators/uniqueness.json', { 'user[email]' => 'user@test.com', 'scope' => { 'age' => 30, 'name' => 'Robert' }, 'case_sensitive' => true }
|
|
73
|
+
|
|
74
|
+
assert_equal 'true', last_response.body
|
|
75
|
+
assert last_response.not_found?
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def test_uniqueness_when_case_insensitive
|
|
79
|
+
User.create(:name => 'Brian')
|
|
80
|
+
get '/validators/uniqueness.json', { 'user[name]' => 'BRIAN', 'case_sensitive' => false }
|
|
81
|
+
|
|
82
|
+
assert_equal 'false', last_response.body
|
|
83
|
+
assert last_response.ok?
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def test_uniqueness_when_attribute_passes_as_an_integer
|
|
87
|
+
User.create(:name => 123)
|
|
88
|
+
get '/validators/uniqueness.json', { 'user[name]' => 123, 'case_sensitive' => true }
|
|
89
|
+
|
|
90
|
+
assert_equal 'false', last_response.body
|
|
91
|
+
assert last_response.ok?
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def test_uniqueness_when_attribute_passes_as_an_integer
|
|
95
|
+
User.create(:name => 123)
|
|
96
|
+
get '/validators/uniqueness.json', { 'user[name]' => 123, 'case_sensitive' => true }
|
|
97
|
+
|
|
98
|
+
assert_equal 'false', last_response.body
|
|
99
|
+
assert last_response.ok?
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def test_uniqueness_with_columns_which_are_sql_keywords
|
|
103
|
+
Guid.validates_uniqueness_of :key
|
|
104
|
+
assert_nothing_raised do
|
|
105
|
+
get '/validators/uniqueness.json', { 'guid[key]' => 'test', 'case_sensitive' => true }
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def test_uniqueness_with_limit
|
|
110
|
+
# User.title is limited to 5 characters
|
|
111
|
+
User.create(:title => "abcde")
|
|
112
|
+
get '/validators/uniqueness.json', { 'user[title]' => 'abcdefgh', 'case_sensitive' => true }
|
|
113
|
+
|
|
114
|
+
assert_equal 'false', last_response.body
|
|
115
|
+
assert last_response.ok?
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def test_uniqueness_with_limit_and_utf8
|
|
119
|
+
with_kcode('UTF8') do
|
|
120
|
+
# User.title is limited to 5 characters
|
|
121
|
+
User.create(:title => "一二三四五")
|
|
122
|
+
get '/validators/uniqueness.json', { 'user[title]' => '一二三四五六七八', 'case_sensitive' => true }
|
|
123
|
+
|
|
124
|
+
assert_equal 'false', last_response.body
|
|
125
|
+
assert last_response.ok?
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def test_validate_straight_inheritance_uniqueness
|
|
130
|
+
get '/validators/uniqueness.json', { 'inept_wizard[name]' => 'Rincewind', 'case_sensitive' => true }
|
|
131
|
+
assert_equal 'true', last_response.body
|
|
132
|
+
assert last_response.not_found?
|
|
133
|
+
|
|
134
|
+
IneptWizard.create(:name => 'Rincewind')
|
|
135
|
+
get '/validators/uniqueness.json', { 'inept_wizard[name]' => 'Rincewind', 'case_sensitive' => true }
|
|
136
|
+
assert_equal 'false', last_response.body
|
|
137
|
+
assert last_response.ok?
|
|
138
|
+
|
|
139
|
+
get '/validators/uniqueness.json', { 'conjurer[name]' => 'Rincewind', 'case_sensitive' => true }
|
|
140
|
+
assert_equal 'false', last_response.body
|
|
141
|
+
assert last_response.ok?
|
|
142
|
+
|
|
143
|
+
Conjurer.create(:name => 'The Amazing Bonko')
|
|
144
|
+
get '/validators/uniqueness.json', { 'thaumaturgist[name]' => 'The Amazing Bonko', 'case_sensitive' => true }
|
|
145
|
+
assert_equal 'false', last_response.body
|
|
146
|
+
assert last_response.ok?
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
end
|
|
150
|
+
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require 'active_record/cases/test_base'
|
|
2
|
+
|
|
3
|
+
class ActiveRecord::UniquenessValidatorTest < ClientSideValidations::ActiveRecordTestBase
|
|
4
|
+
|
|
5
|
+
def test_uniqueness_client_side_hash
|
|
6
|
+
expected_hash = { :message => "has already been taken", :case_sensitive => true }
|
|
7
|
+
assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@user, :name)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def test_uniqueness_client_side_hash_with_custom_message
|
|
11
|
+
expected_hash = { :message => "is not available", :case_sensitive => true }
|
|
12
|
+
assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name], :message => "is not available").client_side_hash(@user, :name)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_uniqueness_client_side_hash
|
|
16
|
+
@user.stubs(:new_record?).returns(false)
|
|
17
|
+
@user.stubs(:id).returns(1)
|
|
18
|
+
expected_hash = { :message => "has already been taken", :case_sensitive => true, :id => 1 }
|
|
19
|
+
assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@user, :name)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def test_uniqueness_client_side_hash_with_single_scope_item
|
|
23
|
+
@user.stubs(:age).returns(30)
|
|
24
|
+
@user.stubs(:title).returns("test title")
|
|
25
|
+
expected_hash = { :message => "has already been taken", :case_sensitive => true, :scope => {:title => "test title"} }
|
|
26
|
+
result_hash = UniquenessValidator.new(:attributes => [:name], :scope => :title).client_side_hash(@user, :name)
|
|
27
|
+
assert_equal expected_hash, result_hash
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def test_uniqueness_client_side_hash_with_multiple_scope_items
|
|
31
|
+
@user.stubs(:age).returns(30)
|
|
32
|
+
@user.stubs(:title).returns("test title")
|
|
33
|
+
expected_hash = { :message => "has already been taken", :case_sensitive => true, :scope => {:age => 30, :title => "test title"} }
|
|
34
|
+
result_hash = UniquenessValidator.new(:attributes => [:name], :scope => [:age, :title]).client_side_hash(@user, :name)
|
|
35
|
+
assert_equal expected_hash, result_hash
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_uniqueness_client_side_hash_with_empty_scope_array
|
|
39
|
+
expected_hash = { :message => "has already been taken", :case_sensitive => true }
|
|
40
|
+
result_hash = UniquenessValidator.new(:attributes => [:name], :scope => []).client_side_hash(@user, :name)
|
|
41
|
+
assert_equal expected_hash, result_hash
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
users_table = %{CREATE TABLE users (id INTEGER PRIMARY KEY, age INTEGER, name TEXT, email TEXT, title VARCHAR(5));}
|
|
2
|
+
ActiveRecord::Base.connection.execute(users_table)
|
|
3
|
+
|
|
4
|
+
class User < ActiveRecord::Base
|
|
5
|
+
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
class IneptWizard < User; end
|
|
9
|
+
class Conjurer < IneptWizard; end
|
|
10
|
+
class Thaumaturgist < Conjurer; end
|
data/test/base_helper.rb
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require 'client_side_validations/core_ext'
|
|
2
|
+
|
|
3
|
+
class CoreExtTest < Test::Unit::TestCase
|
|
4
|
+
def test_regexp_as_json
|
|
5
|
+
regexp = //
|
|
6
|
+
assert_equal regexp, regexp.as_json
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def test_regexp_replace_A_and_Z
|
|
10
|
+
test_regexp = /\A\Z/
|
|
11
|
+
expected_regexp = /^$/
|
|
12
|
+
assert_equal expected_regexp, test_regexp.as_json
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_regexp_replace_a_and_z
|
|
16
|
+
test_regexp = /\A\z/
|
|
17
|
+
expected_regexp = /^$/
|
|
18
|
+
assert_equal expected_regexp, test_regexp.as_json
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_regexp_to_json
|
|
22
|
+
assert_equal "/^$/", /\A\Z/.to_json(nil)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def test_regexp_encode_json
|
|
26
|
+
assert_equal "//", //.encode_json(nil)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_regexp_as_jason_with_options
|
|
30
|
+
assert_equal //i, //i.as_json
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_range_as_json
|
|
34
|
+
assert_equal [1,3], (1..3).as_json
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_range_to_json
|
|
38
|
+
assert_equal '[1, 3]', (1..3).to_json(nil)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_range_as_json_with_floats
|
|
42
|
+
assert_equal [0.5,5.5], (0.5..5.5).as_json
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|