validatious-on-rails 0.4.6 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- data/generators/validatious/templates/v2.rails.js +52 -42
- data/lib/validatious-on-rails/model_validations.rb +275 -277
- data/lib/validatious-on-rails/validatious/remote_validator.rb +4 -1
- data/lib/validatious-on-rails/validatious/validator.rb +3 -3
- data/lib/validatious-on-rails/validatious/validators/uniqueness_validator.rb +1 -2
- data/test/validatious_on_rails/controller_test.rb +7 -8
- metadata +2 -2
@@ -20,7 +20,14 @@ v2.blank = function blank(value) {
|
|
20
20
|
*/
|
21
21
|
v2.bool = function bool(value) {
|
22
22
|
value += '';
|
23
|
-
return
|
23
|
+
return value === 'true' || +value > 0;
|
24
|
+
};
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Checks if a string is null/undefined.
|
28
|
+
*/
|
29
|
+
v2.present = function present(value) {
|
30
|
+
return !(typeof value === 'undefined' || value === null);
|
24
31
|
};
|
25
32
|
|
26
33
|
/**
|
@@ -44,54 +51,57 @@ v2.trimField = function trimField(field) {
|
|
44
51
|
/**
|
45
52
|
* Generic validator that acts as a client-side validator/helper for remote validator responses.
|
46
53
|
*/
|
47
|
-
v2.Validator.add({acceptEmpty:
|
54
|
+
v2.Validator.add({acceptEmpty: false, fn: function(field, value, params) { return !!params[0]; }, message: null, name: 'remote-client'});
|
48
55
|
|
49
56
|
/**
|
50
57
|
* Perform a remote validation on a given field the Validatious way, slightly modified
|
51
58
|
*/
|
52
|
-
v2.Rails.performRemoteValidation = function performRemoteValidation(name, field, value, params, message) {
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
};
|
59
|
+
v2.Rails.performRemoteValidation = function performRemoteValidation(name, field, value, params, message) {
|
60
|
+
var field_element = field.__elements[0];
|
61
|
+
var url = v2.Rails.remoteValidationUrlFor(name, field_element, value, []);
|
62
|
+
|
63
|
+
v2.Rails.initializeLastResult(name, field_element.id);
|
64
|
+
|
65
|
+
console.log(v2.Rails.lastRemoteValidationResult[name][field_element.id]);
|
66
|
+
|
67
|
+
var xmlHttpRequest = new XMLHttpRequest;
|
68
|
+
xmlHttpRequest.open('GET', url, true);
|
69
|
+
xmlHttpRequest.onreadystatechange = function() {
|
70
|
+
if (this.readyState == XMLHttpRequest.DONE) {
|
71
|
+
var validationResult = v2.bool(this.responseText);
|
72
|
+
v2.Rails.lastRemoteValidationResult[name][field_element.id] = validationResult;
|
73
|
+
|
74
|
+
/* Get all validators for this field, except the current validator. */
|
75
|
+
var fieldClasses = v2.trim(field_element.getAttribute('class').replace(new RegExp(name + '\w*'), ''));
|
76
|
+
var theOtherValidators = v2.html.validatorsFromString(fieldClasses);
|
77
|
+
|
78
|
+
/* Make remote-client validator trigger validation failure or not. */
|
79
|
+
var thisValidator = field_element.id.is('remote-client', validationResult).explain(message);
|
80
|
+
v2.html.applyValidators(theOtherValidators, thisValidator);
|
81
|
+
|
82
|
+
/* Trigger validation. */
|
83
|
+
thisValidator.item.validate();
|
84
|
+
};
|
85
|
+
};
|
86
|
+
xmlHttpRequest.send(null);
|
87
|
+
return v2.Rails.lastRemoteValidationResult[name][field_element.id];
|
88
|
+
};
|
79
89
|
|
80
90
|
/**
|
81
91
|
* Initialize data structure for holding info about last remote AJAX validation result.
|
82
92
|
* We need this to make Validatious play well with remote validations.
|
83
93
|
*/
|
84
|
-
v2.Rails.initializeLastResult = function initializeLastResult(validator_name, field_id) {
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
};
|
94
|
+
v2.Rails.initializeLastResult = function initializeLastResult(validator_name, field_id) {
|
95
|
+
if (!v2.present(v2.Rails.lastRemoteValidationResult)) {
|
96
|
+
v2.Rails.lastRemoteValidationResult = new Array();
|
97
|
+
};
|
98
|
+
if (!v2.present(v2.Rails.lastRemoteValidationResult[validator_name])) {
|
99
|
+
v2.Rails.lastRemoteValidationResult[validator_name] = new Array();
|
100
|
+
};
|
101
|
+
if (!v2.present(v2.Rails.lastRemoteValidationResult[validator_name][field_id])) {
|
102
|
+
v2.Rails.lastRemoteValidationResult[validator_name][field_id] = false;
|
103
|
+
};
|
104
|
+
};
|
95
105
|
|
96
106
|
/**
|
97
107
|
* Generate a remote validation poll URL the validatious-on-rails-way,
|
@@ -112,11 +122,11 @@ v2.Rails.remoteValidationUrlFor = function remoteValidationUrlFor(name, field, v
|
|
112
122
|
[
|
113
123
|
['model', escape(modelName)].join('='),
|
114
124
|
['attribute', escape(attributeName)].join('='),
|
115
|
-
['id', recordId].join('='),
|
125
|
+
(v2.blank(recordId) ? null : ['id', recordId].join('=')),
|
116
126
|
['value', escape(value)].join('='),
|
117
127
|
paramsString.join('&')
|
118
128
|
].join('&')
|
119
|
-
].join('').replace(/\&$/, '');
|
129
|
+
].join('').replace(/\&\&/, '&').replace(/\&$/, '');
|
120
130
|
return url;
|
121
131
|
};
|
122
132
|
|
@@ -11,316 +11,314 @@ require File.join(File.dirname(__FILE__), *%w[validatious validators])
|
|
11
11
|
# Validatious-Rails validation translator.
|
12
12
|
#
|
13
13
|
module ValidatiousOnRails
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
#
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
14
|
+
module ModelValidations
|
15
|
+
|
16
|
+
extend self
|
17
|
+
|
18
|
+
IGNORED_VALIDATIONS = [:each, :associated]
|
19
|
+
|
20
|
+
def validation_methods
|
21
|
+
::ActiveRecord::Base.methods.sort.collect do |m|
|
22
|
+
$1.to_s.to_sym if (m.to_s =~ /^validates_(.*)/)
|
23
|
+
end.compact - IGNORED_VALIDATIONS
|
24
|
+
end
|
25
|
+
|
26
|
+
CORE_VALIDATIONS = self.validation_methods
|
27
|
+
|
28
|
+
# References:
|
29
|
+
# http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html
|
30
|
+
# http://github.com/rails/rails/blob/13fb26b714dec0874303f51cc125ff62f65a2729/activerecord/lib/active_record/validations.rb
|
31
|
+
|
32
|
+
# Generates form field helper options for a specified object-attribute to
|
33
|
+
# reflect on it's validations.
|
34
|
+
#
|
35
|
+
# Input may be an ActiveRecord class, a class name (string), or an object
|
36
|
+
# name along with a method/field.
|
37
|
+
#
|
38
|
+
def options_for(object_name, attribute_method, options = {}, existing_validators = nil)
|
39
|
+
# Handle Nested form.
|
40
|
+
object_name = options[:object].present? ? options[:object].class.name : object_name
|
41
|
+
|
42
|
+
validators = self.from_active_record(object_name, attribute_method)
|
43
|
+
validator_classes, validator_js = [options[:class]], []
|
44
|
+
|
45
|
+
# Only attach validators that are not already attached.
|
46
|
+
validators.flatten.compact.uniq.each do |v|
|
47
|
+
validator_js << v.to_js unless existing_validators.present? && /#{v.name}/ =~ existing_validators
|
48
|
+
validator_classes << v.to_class
|
49
|
+
end
|
50
|
+
classes = validator_classes.compact.join(' ').strip
|
51
|
+
js = validator_js.compact.join("\n").strip
|
52
|
+
options.merge!(:class => (classes unless classes.blank?), :js => (js unless js.blank?))
|
53
|
+
end
|
54
|
+
|
55
|
+
# Groks Rails validations, and is able to convert a rails validation to
|
56
|
+
# a Validatious 2.0 compatible class string, and a Validatous validator
|
57
|
+
# for more complex validations. Even some of the Rails core validations
|
58
|
+
# with certain options requires this.
|
59
|
+
#
|
60
|
+
# Input may be an ActiveRecord class, a class name (string), or an object
|
61
|
+
# name along with a method/field.
|
62
|
+
#
|
63
|
+
# Returns a string that will be recognized by Validatious as a class name in
|
64
|
+
# form markup.
|
65
|
+
#
|
66
|
+
def from_active_record(object_or_class, attribute_method)
|
67
|
+
validators = []
|
68
|
+
begin
|
69
|
+
klass = if [::String, ::Symbol].any? { |c| object_or_class.is_a?(c) }
|
70
|
+
object_or_class.to_s.classify.constantize
|
71
|
+
elsif object_or_class.is_a?(::Object)
|
72
|
+
object_or_class.class
|
73
|
+
else
|
74
|
+
object_or_class
|
53
75
|
end
|
54
|
-
|
55
|
-
|
56
|
-
|
76
|
+
return validators unless klass.respond_to?(:reflect_on_validations_for)
|
77
|
+
rescue
|
78
|
+
::ValidatiousOnRails.log "Missing constant: #{object_or_class}", :debug
|
79
|
+
return validators
|
57
80
|
end
|
58
81
|
|
59
|
-
#
|
60
|
-
#
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
# name along with a method/field.
|
66
|
-
#
|
67
|
-
# Returns a string that will be recognized by Validatious as a class name in
|
68
|
-
# form markup.
|
69
|
-
#
|
70
|
-
def from_active_record(object_or_class, attribute_method)
|
71
|
-
validators = []
|
72
|
-
begin
|
73
|
-
klass = if [::String, ::Symbol].any? { |c| object_or_class.is_a?(c) }
|
74
|
-
object_or_class.to_s.classify.constantize
|
75
|
-
elsif object_or_class.is_a?(::Object)
|
76
|
-
object_or_class.class
|
77
|
-
else
|
78
|
-
object_or_class
|
79
|
-
end
|
80
|
-
return validators unless klass.respond_to?(:reflect_on_validations_for)
|
81
|
-
rescue
|
82
|
-
::ValidatiousOnRails.log "Missing constant: #{object_or_class}", :debug
|
83
|
-
return validators
|
82
|
+
# Iterate thorugh the validations for the current class,
|
83
|
+
# and collect validation options.
|
84
|
+
klass.reflect_on_validations_for(attribute_method.to_sym).each do |validation|
|
85
|
+
validates_type = validation.macro.to_s.sub(/^validates?_/, '')
|
86
|
+
if validation.options[:client_side].nil?
|
87
|
+
validation.options[:client_side] = ::ValidatiousOnRails.client_side_validations_by_default
|
84
88
|
end
|
85
89
|
|
86
|
-
#
|
87
|
-
#
|
88
|
-
|
89
|
-
|
90
|
+
# Skip "confirmation_of"-validation info for the attribute that
|
91
|
+
# needs to be confirmed. Validatious expects this validation rule
|
92
|
+
# on the confirmation field. *
|
93
|
+
unless validates_type =~ /^confirmation_of$/
|
94
|
+
validators << self.send(validates_type.to_sym, validation) if validation.options[:client_side]
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Special case for "confirmation_of"-validation (see * above).
|
99
|
+
if attribute_method.to_s =~ /(.+)_confirmation$/
|
100
|
+
confirm_attribute_method = $1
|
101
|
+
# Check if validates_confirmation_of(:hello) actually exists,
|
102
|
+
# if :hello_confirmation field exists - just to be safe.
|
103
|
+
klass.reflect_on_validations_for(confirm_attribute_method.to_sym).each do |validation|
|
90
104
|
if validation.options[:client_side].nil?
|
91
105
|
validation.options[:client_side] = ::ValidatiousOnRails.client_side_validations_by_default
|
92
106
|
end
|
93
107
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
unless validates_type =~ /^confirmation_of$/
|
98
|
-
validators << self.send(validates_type.to_sym, validation) if validation.options[:client_side]
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
# Special case for "confirmation_of"-validation (see * above).
|
103
|
-
if attribute_method.to_s =~ /(.+)_confirmation$/
|
104
|
-
confirm_attribute_method = $1
|
105
|
-
# Check if validates_confirmation_of(:hello) actually exists,
|
106
|
-
# if :hello_confirmation field exists - just to be safe.
|
107
|
-
klass.reflect_on_validations_for(confirm_attribute_method.to_sym).each do |validation|
|
108
|
-
if validation.options[:client_side].nil?
|
109
|
-
validation.options[:client_side] = ::ValidatiousOnRails.client_side_validations_by_default
|
110
|
-
end
|
111
|
-
|
112
|
-
if validation.macro.to_s =~ /^validates_confirmation_of$/
|
113
|
-
validators << self.confirmation_of(validation) if validation.options[:client_side]
|
114
|
-
break
|
115
|
-
end
|
108
|
+
if validation.macro.to_s =~ /^validates_confirmation_of$/
|
109
|
+
validators << self.confirmation_of(validation) if validation.options[:client_side]
|
110
|
+
break
|
116
111
|
end
|
117
112
|
end
|
118
|
-
validators.flatten.compact
|
119
113
|
end
|
114
|
+
validators.flatten.compact
|
115
|
+
end
|
120
116
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
117
|
+
# Resolve validation from validates_acceptance_of.
|
118
|
+
#
|
119
|
+
# Alias, but might change: acceptance_of <=> presence_of
|
120
|
+
#
|
121
|
+
# NOTE: Not supported:
|
122
|
+
# * :on - TODO.
|
123
|
+
# * :if/:unless - hard to port all to client-side JavaScript
|
124
|
+
# (impossible: procs, unaccessible valiables, etc.).
|
125
|
+
#
|
126
|
+
def acceptance_of(validation)
|
127
|
+
validators = []
|
128
|
+
validation.options[:allow_nil] = false if validation.options[:allow_nil].nil?
|
129
|
+
validation.options[:accept] ||= '1' # Rails default.
|
130
|
+
validators << Validatious::AcceptanceAcceptValidator.new(validation.options[:accept],
|
131
|
+
validation.options[:allow_nil])
|
132
|
+
end
|
137
133
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
134
|
+
# Resolve validation from validates_associated.
|
135
|
+
#
|
136
|
+
# NOTE: Not supported - low prio.
|
137
|
+
#
|
138
|
+
def associated(validation)
|
139
|
+
nil
|
140
|
+
end
|
145
141
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
end
|
162
|
-
validators << Validatious::ConfirmationOfValidator.new(field_id)
|
142
|
+
# Resolve validation from validates_confirmation_of.
|
143
|
+
# This validation is treated a bit differently in compare
|
144
|
+
# to the other validations. See "from_active_record".
|
145
|
+
#
|
146
|
+
# NOTE: Not supported:
|
147
|
+
# * :on - TODO.
|
148
|
+
# * :if/:unless - hard to port all to client-side JavaScript
|
149
|
+
# (impossible: procs, unaccessible valiables, etc.).
|
150
|
+
#
|
151
|
+
def confirmation_of(validation)
|
152
|
+
validators = []
|
153
|
+
field_id = unless validation.active_record.present?
|
154
|
+
"#{validation.active_record.name.tableize.singularize.gsub('/', '_')}_#{validation.name}"
|
155
|
+
else
|
156
|
+
"#{validation.name}"
|
163
157
|
end
|
158
|
+
validators << Validatious::ConfirmationOfValidator.new(field_id)
|
159
|
+
end
|
164
160
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
161
|
+
# Resolve validation from validates_exclusion_of.
|
162
|
+
#
|
163
|
+
# Attaching custom validator - with a unique name based on the exclusion values.
|
164
|
+
#
|
165
|
+
# NOTE: Not supported:
|
166
|
+
# * :on - TODO.
|
167
|
+
# * :if/:unless - hard to port all to client-side JavaScript
|
168
|
+
# (impossible: procs, unaccessible valiables, etc.).
|
169
|
+
#
|
170
|
+
def exclusion_of(validation)
|
171
|
+
validators = []
|
172
|
+
validation.options[:allow_nil] = false if validation.options[:allow_nil].nil?
|
173
|
+
validation.options[:allow_blank] = false if validation.options[:allow_blank].nil?
|
174
|
+
validators << Validatious::ExclusionInValidator.new(validation.options[:in],
|
175
|
+
validation.options[:allow_nil], validation.options[:allow_blank])
|
176
|
+
end
|
181
177
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
178
|
+
# Resolve validation from validates_format_of.
|
179
|
+
#
|
180
|
+
# Attaching custom validator, with a unique name based on the regular expression.
|
181
|
+
# Needs regexp.inspect to get it right.
|
182
|
+
#
|
183
|
+
# NOTE: Not supported:
|
184
|
+
# * :on - TODO.
|
185
|
+
# * :if/:unless - hard to port all to client-side JavaScript
|
186
|
+
# (impossible: procs, unaccessible valiables, etc.).
|
187
|
+
#
|
188
|
+
def format_of(validation)
|
189
|
+
validators = []
|
190
|
+
validation.options[:allow_nil] = false if validation.options[:allow_nil].nil?
|
191
|
+
validation.options[:allow_blank] = false if validation.options[:allow_blank].nil?
|
192
|
+
validators << Validatious::FormatWithValidator.new(validation.options[:with],
|
193
|
+
validation.options[:allow_nil], validation.options[:allow_blank])
|
194
|
+
end
|
199
195
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
196
|
+
# Resolve validation from validates_inclusion_of.
|
197
|
+
#
|
198
|
+
# Attaching custom validator - with a unique name based on the inclusion values.
|
199
|
+
#
|
200
|
+
# NOTE: Not supported:
|
201
|
+
# * :on - TODO.
|
202
|
+
# * :if/:unless - hard to port all to client-side JavaScript
|
203
|
+
# (impossible: procs, unaccessible valiables, etc.).
|
204
|
+
#
|
205
|
+
def inclusion_of(validation)
|
206
|
+
validators = []
|
207
|
+
validation.options[:allow_nil] = false if validation.options[:allow_nil].nil?
|
208
|
+
validation.options[:allow_blank] = false if validation.options[:allow_blank].nil?
|
209
|
+
validators << Validatious::InclusionInValidator.new(validation.options[:in],
|
210
|
+
validation.options[:allow_nil], validation.options[:allow_blank])
|
211
|
+
end
|
216
212
|
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
if validation.options[:is].present?
|
234
|
-
validators << Validatious::Length::IsValidator.new(validation.options[:is],
|
235
|
-
(validation.options[:allow_nil] || false),
|
236
|
-
(validation.options[:allow_blank] || false))
|
237
|
-
elsif [:in, :within, :minimum, :maximum].any? { |k| validation.options[k].present? }
|
238
|
-
validation.options[:within] ||= validation.options[:in]
|
239
|
-
validation.options[:minimum] ||= validation.options[:within].min rescue nil
|
240
|
-
validation.options[:maximum] ||= validation.options[:within].max rescue nil
|
241
|
-
|
242
|
-
if validation.options[:minimum].present?
|
243
|
-
validators << Validatious::Length::MinimumValidator.new(validation.options[:minimum],
|
244
|
-
(validation.options[:allow_nil] || false),
|
245
|
-
(validation.options[:allow_blank] || false))
|
246
|
-
end
|
213
|
+
# Resolve validation from validates_length_of.
|
214
|
+
#
|
215
|
+
# Example (of generated field classes):
|
216
|
+
# length-is_5, length-maximum_2, length-minimum_2, etc.
|
217
|
+
#
|
218
|
+
# NOTE: Not supported:
|
219
|
+
# * :tokenizer - see: :if/:unless
|
220
|
+
# * :on - TODO.
|
221
|
+
# * :if/:unless - hard to port all to client-side JavaScript
|
222
|
+
# (impossible: procs, unaccessible valiables, etc.).
|
223
|
+
#
|
224
|
+
def length_of(validation)
|
225
|
+
validators = []
|
226
|
+
validation.options[:allow_nil] = false if validation.options[:allow_nil].nil?
|
227
|
+
validation.options[:allow_blank] = false if validation.options[:allow_blank].nil?
|
247
228
|
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
alias :size_of :length_of
|
257
|
-
|
258
|
-
# Resolve validation from validates_numericality_of.
|
259
|
-
#
|
260
|
-
# Example (of generated field classes):
|
261
|
-
# numericality-odd, numericality-only-integer, numericality-equal-to_5, etc.
|
262
|
-
#
|
263
|
-
# NOTE: Not supported:
|
264
|
-
# * :on - TODO.en
|
265
|
-
# * :if/:unless - hard to port all to client-side JavaScript
|
266
|
-
# (impossible: procs, unaccessible valiables, etc.).
|
267
|
-
#
|
268
|
-
def numericality_of(validation)
|
269
|
-
validators = []
|
270
|
-
validation.options[:allow_nil] = false if validation.options[:allow_nil].nil?
|
271
|
-
|
272
|
-
if validation.options[:odd] && !validation.options[:even]
|
273
|
-
validators << Validatious::Numericality::OddValidator.new(validation.options[:allow_nil])
|
274
|
-
end
|
229
|
+
if validation.options[:is].present?
|
230
|
+
validators << Validatious::Length::IsValidator.new(validation.options[:is],
|
231
|
+
(validation.options[:allow_nil] || false),
|
232
|
+
(validation.options[:allow_blank] || false))
|
233
|
+
elsif [:in, :within, :minimum, :maximum].any? { |k| validation.options[k].present? }
|
234
|
+
validation.options[:within] ||= validation.options[:in]
|
235
|
+
validation.options[:minimum] ||= validation.options[:within].min rescue nil
|
236
|
+
validation.options[:maximum] ||= validation.options[:within].max rescue nil
|
275
237
|
|
276
|
-
if validation.options[:
|
277
|
-
validators << Validatious::
|
238
|
+
if validation.options[:minimum].present?
|
239
|
+
validators << Validatious::Length::MinimumValidator.new(validation.options[:minimum],
|
240
|
+
(validation.options[:allow_nil] || false),
|
241
|
+
(validation.options[:allow_blank] || false))
|
278
242
|
end
|
279
243
|
|
280
|
-
if validation.options[:
|
281
|
-
validators << Validatious::
|
244
|
+
if validation.options[:maximum].present?
|
245
|
+
validators << Validatious::Length::MaximumValidator.new(validation.options[:maximum],
|
246
|
+
(validation.options[:allow_nil] || false),
|
247
|
+
(validation.options[:allow_blank] || false))
|
282
248
|
end
|
283
|
-
|
284
|
-
(validation.options.keys & [:equal_to, :less_than, :less_than_or_equal_to,
|
285
|
-
:greater_than, :greater_than_or_equal_to]).each { |name|
|
286
|
-
validator_klass = "::ValidatiousOnRails::Validatious::Numericality::#{name.to_s.classify}Validator".constantize
|
287
|
-
value = validation.options[name]
|
288
|
-
if value.is_a?(::Numeric)
|
289
|
-
validators << validator_klass.new(validation.options[name], validation.options[:allow_nil])
|
290
|
-
end
|
291
|
-
}
|
292
|
-
validators
|
293
249
|
end
|
250
|
+
validators
|
251
|
+
end
|
252
|
+
alias :size_of :length_of
|
294
253
|
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
254
|
+
# Resolve validation from validates_numericality_of.
|
255
|
+
#
|
256
|
+
# Example (of generated field classes):
|
257
|
+
# numericality-odd, numericality-only-integer, numericality-equal-to_5, etc.
|
258
|
+
#
|
259
|
+
# NOTE: Not supported:
|
260
|
+
# * :on - TODO.en
|
261
|
+
# * :if/:unless - hard to port all to client-side JavaScript
|
262
|
+
# (impossible: procs, unaccessible valiables, etc.).
|
263
|
+
#
|
264
|
+
def numericality_of(validation)
|
265
|
+
validators = []
|
266
|
+
validation.options[:allow_nil] = false if validation.options[:allow_nil].nil?
|
267
|
+
|
268
|
+
if validation.options[:odd] && !validation.options[:even]
|
269
|
+
validators << Validatious::Numericality::OddValidator.new(validation.options[:allow_nil])
|
307
270
|
end
|
308
271
|
|
309
|
-
|
310
|
-
|
311
|
-
def uniqueness_of(validation)
|
312
|
-
validators = []
|
313
|
-
validators << Validatious::UniquenessValidator.new
|
272
|
+
if validation.options[:even] && !validation.options[:odd]
|
273
|
+
validators << Validatious::Numericality::EvenValidator.new(validation.options[:allow_nil])
|
314
274
|
end
|
315
275
|
|
316
|
-
|
317
|
-
|
318
|
-
def method_missing(sym, *args, &block)
|
319
|
-
::ValidatiousOnRails.log "Unknown validation: #{sym}." <<
|
320
|
-
" No custom Validatious validator found for this validation makro. ", :warn
|
321
|
-
nil
|
276
|
+
if validation.options[:only_integer]
|
277
|
+
validators << Validatious::Numericality::OnlyIntegerValidator.new(validation.options[:allow_nil])
|
322
278
|
end
|
323
279
|
|
280
|
+
(validation.options.keys & [:equal_to, :less_than, :less_than_or_equal_to,
|
281
|
+
:greater_than, :greater_than_or_equal_to]).each { |name|
|
282
|
+
validator_klass = "::ValidatiousOnRails::Validatious::Numericality::#{name.to_s.classify}Validator".constantize
|
283
|
+
value = validation.options[name]
|
284
|
+
if value.is_a?(::Numeric)
|
285
|
+
validators << validator_klass.new(validation.options[name], validation.options[:allow_nil])
|
286
|
+
end
|
287
|
+
}
|
288
|
+
validators
|
289
|
+
end
|
290
|
+
|
291
|
+
# Resolve validation from validates_presence_of.
|
292
|
+
#
|
293
|
+
# Alias, but might change: acceptance_of <=> presence_of
|
294
|
+
#
|
295
|
+
# NOTE: Not supported:
|
296
|
+
# * :on - TODO.
|
297
|
+
# * :if/:unless - hard to port all to client-side JavaScript
|
298
|
+
# (impossible: procs, unaccessible valiables, etc.).
|
299
|
+
#
|
300
|
+
def presence_of(validation)
|
301
|
+
validators = []
|
302
|
+
validators << Validatious::PresenceValidator.new
|
303
|
+
end
|
304
|
+
|
305
|
+
# Resolve validation from validates_uniqueness_of.
|
306
|
+
#
|
307
|
+
def uniqueness_of(validation)
|
308
|
+
validators = []
|
309
|
+
validation.options[:allow_nil] = false if validation.options[:allow_nil].nil?
|
310
|
+
validation.options[:allow_blank] = false if validation.options[:allow_blank].nil?
|
311
|
+
validators << Validatious::UniquenessValidator.new(validation.options[:allow_nil], validation.options[:allow_blank])
|
324
312
|
end
|
313
|
+
|
314
|
+
# Unknown validations - if no matching custom validator is found/registered.
|
315
|
+
#
|
316
|
+
def method_missing(sym, *args, &block)
|
317
|
+
::ValidatiousOnRails.log "Unknown validation: #{sym}." <<
|
318
|
+
" No custom Validatious validator found for this validation makro. ", :warn
|
319
|
+
nil
|
320
|
+
end
|
321
|
+
|
322
|
+
#end
|
325
323
|
end
|
326
324
|
end
|
@@ -17,7 +17,10 @@ module ValidatiousOnRails
|
|
17
17
|
def fn
|
18
18
|
self.class.truncate_whitespace(@fn ||= %{
|
19
19
|
function(field, value, params) {
|
20
|
-
|
20
|
+
value += '';
|
21
|
+
#{self.class.handle_nil(0)}
|
22
|
+
#{self.class.handle_blank(1)}
|
23
|
+
return v2.Rails.performRemoteValidation(#{self.name.to_json}, field, value, params, #{self.message.to_json});
|
21
24
|
}
|
22
25
|
})
|
23
26
|
end
|
@@ -168,7 +168,7 @@ module ValidatiousOnRails
|
|
168
168
|
|
169
169
|
def handle_nil(index = 1)
|
170
170
|
%{
|
171
|
-
if (v2.bool(params[#{index}]) && v2.empty(value)) {
|
171
|
+
if (v2.present(params[#{index}]) && v2.bool(params[#{index}]) && v2.empty(value)) {
|
172
172
|
return true;
|
173
173
|
};
|
174
174
|
}
|
@@ -176,10 +176,10 @@ module ValidatiousOnRails
|
|
176
176
|
|
177
177
|
def handle_blank(index = 2)
|
178
178
|
%{
|
179
|
-
if (v2.bool(params[#{index}]) && v2.blank(value)) {
|
179
|
+
if (v2.present(params[#{index}]) && v2.bool(params[#{index}]) && v2.blank(value)) {
|
180
180
|
return true;
|
181
181
|
};
|
182
|
-
if (!v2.bool(params[#{index}])) {
|
182
|
+
if (v2.present(params[#{index}]) && !v2.bool(params[#{index}])) {
|
183
183
|
v2.trimField(field);
|
184
184
|
value = v2.trim(value);
|
185
185
|
};
|
@@ -64,23 +64,22 @@ class ControllerTest < ::ActionController::TestCase
|
|
64
64
|
# assert_response 405
|
65
65
|
# assert_equal 'false', @response.body
|
66
66
|
# end
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
67
|
+
#
|
68
|
+
# test ":value is allowed to be blank - should succeed" do
|
69
|
+
# get :uniqueness_of, :model => 'bogus_item', :attribute => 'name'
|
70
|
+
# assert_response 200
|
71
|
+
# assert_equal 'true', @response.body
|
72
|
+
# end
|
73
73
|
end
|
74
74
|
|
75
75
|
context "with :value" do
|
76
|
-
# FIXME: Fails, but why? Works "in practice".
|
77
76
|
test "invalid value - should fail" do
|
78
77
|
@existing_bogus_item = ::BogusItem.new(:name => 'carrot')
|
79
78
|
@existing_bogus_item.save(false)
|
80
79
|
|
81
80
|
get :uniqueness_of, :model => 'bogus_item', :attribute => 'name', :value => 'carrot'
|
82
81
|
assert_response 200
|
83
|
-
assert_equal '
|
82
|
+
assert_equal 'false', @response.body
|
84
83
|
end
|
85
84
|
|
86
85
|
test "valid :value - should succeed" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validatious-on-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonas Grimfelt
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-10-
|
13
|
+
date: 2009-10-31 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|