client_side_validations 2.2.0 → 2.2.2
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.
@@ -6,17 +6,22 @@ if (typeof(jQuery) != "undefined") {
|
|
6
6
|
|
7
7
|
$.extend($.fn, {
|
8
8
|
clientSideValidations: function() {
|
9
|
-
var form
|
10
|
-
var object
|
11
|
-
var
|
12
|
-
var
|
9
|
+
var form = this;
|
10
|
+
var object = form.attr('object-csv');
|
11
|
+
var edit_pattern = new RegExp('^edit_' + object + '_(.+)', 'i');
|
12
|
+
var url = '/validations.json?model=' + object;
|
13
|
+
var id = form[0].id;
|
14
|
+
var object_id = null;
|
15
|
+
if (edit_pattern.test(id)) {
|
16
|
+
object_id = Number(edit_pattern.exec(id)[1]);
|
17
|
+
}
|
13
18
|
var adapter = 'jquery.validate';
|
14
19
|
if (/new/.test(id)) {
|
15
20
|
id = /new_(\w+)/.exec(id)[1]
|
16
21
|
} else if (/edit/.test(id)) {
|
17
22
|
id = /edit_(\w+)_\d+/.exec(id)[1]
|
18
23
|
}
|
19
|
-
var client = new ClientSideValidations(id, adapter)
|
24
|
+
var client = new ClientSideValidations(id, adapter, object_id)
|
20
25
|
$.getJSON(url, function(json) {
|
21
26
|
var validations = client.adaptValidations(json);
|
22
27
|
form.validate({
|
@@ -32,7 +37,7 @@ if (typeof(jQuery) != "undefined") {
|
|
32
37
|
});
|
33
38
|
}
|
34
39
|
|
35
|
-
ClientSideValidations = function(id, adapter) {
|
40
|
+
ClientSideValidations = function(id, adapter, object_id) {
|
36
41
|
this.id = id;
|
37
42
|
this.adapter = adapter;
|
38
43
|
this.adaptValidations = function(validations) {
|
@@ -70,7 +75,13 @@ ClientSideValidations = function(id, adapter) {
|
|
70
75
|
case 'uniqueness':
|
71
76
|
rule = 'remote';
|
72
77
|
value = {
|
73
|
-
url: '/validations/uniqueness.json'
|
78
|
+
url: '/validations/uniqueness.json',
|
79
|
+
data: {}
|
80
|
+
}
|
81
|
+
if(object_id) {
|
82
|
+
value['data'][this.id + '[id]'] = function() {
|
83
|
+
return String(object_id);
|
84
|
+
}
|
74
85
|
}
|
75
86
|
break;
|
76
87
|
|
@@ -8,7 +8,9 @@ class ClientSideValidations
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def call(env)
|
11
|
-
|
11
|
+
# By default CGI::parse will instantize a hash that defaults nil elements to [].
|
12
|
+
# We need to override this
|
13
|
+
params = {}.merge!(CGI::parse(env['QUERY_STRING']))
|
12
14
|
case env['PATH_INFO']
|
13
15
|
when %r{^/validations.json}
|
14
16
|
body = get_validations(params['model'][0])
|
@@ -17,7 +19,10 @@ class ClientSideValidations
|
|
17
19
|
field = params.keys.first
|
18
20
|
resource, attribute = field.split(/[^\w]/)
|
19
21
|
value = params[field][0]
|
20
|
-
|
22
|
+
# Because params returns an array for each field value we want to always grab
|
23
|
+
# the first element of the array for id, even if it is nil
|
24
|
+
id = [params["#{resource}[id]"]].flatten.first
|
25
|
+
body = is_unique?(resource, attribute, value, id).to_s
|
21
26
|
[200, {'Content-Type' => 'application/json', 'Content-Length' => "#{body.length}"}, body]
|
22
27
|
else
|
23
28
|
@app.call(env)
|
@@ -30,8 +35,24 @@ class ClientSideValidations
|
|
30
35
|
constantize_resource(resource).new.validations_to_json
|
31
36
|
end
|
32
37
|
|
33
|
-
def is_unique?(resource, attribute, value)
|
34
|
-
constantize_resource(resource)
|
38
|
+
def is_unique?(resource, attribute, value, id = nil)
|
39
|
+
klass = constantize_resource(resource)
|
40
|
+
instance = nil
|
41
|
+
if id
|
42
|
+
if instance = klass.find(id)
|
43
|
+
if instance.send(attribute) == value
|
44
|
+
return true
|
45
|
+
else
|
46
|
+
instance = nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
unless instance
|
52
|
+
instance = klass.send("find_by_#{attribute}", value)
|
53
|
+
end
|
54
|
+
|
55
|
+
instance == nil
|
35
56
|
end
|
36
57
|
|
37
58
|
def constantize_resource(resource)
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 2
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 2.2.
|
8
|
+
- 2
|
9
|
+
version: 2.2.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Brian Cardarella
|
@@ -45,7 +45,7 @@ dependencies:
|
|
45
45
|
version: 1.4.3
|
46
46
|
type: :runtime
|
47
47
|
version_requirements: *id002
|
48
|
-
description:
|
48
|
+
description: Client Side Validations for Rails 2.x and 3.x
|
49
49
|
email: cardarellab@dnc.org
|
50
50
|
executables: []
|
51
51
|
|