judge 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/docs/judge.html +7 -8
- data/judge.gemspec +2 -2
- data/lib/generators/judge/templates/judge.js +6 -7
- data/lib/judge/utils.rb +1 -1
- data/spec/javascripts/JudgeSpec.js +2 -2
- data/test/dummy/app/models/foo.rb +11 -11
- data/test/judge_test.rb +5 -0
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
data/docs/judge.html
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
<!DOCTYPE html> <html> <head> <title>judge.js</title>
|
1
|
+
<!DOCTYPE html> <html> <head> <title>judge.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> judge.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <p>This is the JavaScript part of Judge, a client-side validation gem for Rails 3.
|
2
2
|
You can find a guide and some more traditional API documentation at <a href="http://joecorcoran.github.com/judge/">http://joecorcoran.github.com/judge/</a>.
|
3
3
|
Hopefully this page will help you understand what's happening under the hood.</p> </td> <td class="code"> <div class="highlight"><pre><span class="cm">/*jshint curly: true, evil: true, newcap: true, noarg: true */</span>
|
4
4
|
<span class="cm">/*global _: false, JSON: false */</span> </pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <p>The judge namespace.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">judge</span> <span class="o">=</span> <span class="nx">judge</span> <span class="o">||</span> <span class="p">{};</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <p>A judge.Watcher is a DOM element wrapper that judge uses to store validation info and instance methods.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">judge</span><span class="p">.</span><span class="nx">Watcher</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">element</span><span class="p">)</span> <span class="p">{</span>
|
@@ -21,9 +21,8 @@ Hopefully this page will help you understand what's happening under the hood.</p
|
|
21
21
|
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <p>Convenient access to this Watcher.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">instance</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>Watcher instance properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">element</span> <span class="o">=</span> <span class="nx">element</span><span class="p">;</span>
|
22
22
|
<span class="k">this</span><span class="p">.</span><span class="nx">validators</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">element</span><span class="p">.</span><span class="nx">getAttribute</span><span class="p">(</span><span class="s1">'data-validate'</span><span class="p">));</span>
|
23
23
|
<span class="k">this</span><span class="p">.</span><span class="nx">defaultMessages</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">element</span><span class="p">.</span><span class="nx">form</span><span class="p">.</span><span class="nx">getAttribute</span><span class="p">(</span><span class="s1">'data-error-messages'</span><span class="p">));</span>
|
24
|
-
</pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <p>
|
25
|
-
represented as an
|
26
|
-
for each validator found on the element.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">validate</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
24
|
+
</pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <p>The <code>validate</code> instance method returns the validity of the watched element,
|
25
|
+
represented as an object containing the element itself and some validity information.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">validate</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
27
26
|
<span class="nx">instance</span><span class="p">.</span><span class="nx">errorMessages</span> <span class="o">=</span> <span class="p">[];</span>
|
28
27
|
<span class="kd">var</span> <span class="nx">validators</span> <span class="o">=</span> <span class="nx">instance</span><span class="p">.</span><span class="nx">validators</span><span class="p">,</span>
|
29
28
|
<span class="nx">validity</span> <span class="o">=</span> <span class="kc">true</span><span class="p">,</span>
|
@@ -254,15 +253,15 @@ but the created Watchers will not be returned, so it's less flexible.</p>
|
|
254
253
|
<span class="kd">var</span> <span class="nx">string</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">toString</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">object</span><span class="p">);</span>
|
255
254
|
<span class="k">return</span> <span class="nx">string</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\[|\]/g</span><span class="p">,</span> <span class="s1">''</span><span class="p">).</span><span class="nx">split</span><span class="p">(</span><span class="s1">' '</span><span class="p">)[</span><span class="mi">1</span><span class="p">];</span>
|
256
255
|
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">¶</a> </div> <p>OMG! How can you use eval, you monster! It's totally evil!
|
257
|
-
It's used for stuff like <code>(3, '<', 4) => '3 < 4' => true</code
|
256
|
+
(It's used here for stuff like <code>(3, '<', 4) => '3 < 4' => true</code>.)</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">operate</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">input</span><span class="p">,</span> <span class="nx">operator</span><span class="p">,</span> <span class="nx">validInput</span><span class="p">)</span> <span class="p">{</span>
|
258
257
|
<span class="k">return</span> <span class="nb">eval</span><span class="p">(</span><span class="nx">input</span><span class="o">+</span><span class="s1">' '</span><span class="o">+</span><span class="nx">operator</span><span class="o">+</span><span class="s1">' '</span><span class="o">+</span><span class="nx">validInput</span><span class="p">);</span>
|
259
258
|
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">¶</a> </div> <p>Sub the expected value into an error message.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">countMsg</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">message</span><span class="p">,</span> <span class="nx">count</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">message</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\%\{count\}/</span><span class="p">,</span> <span class="nx">count</span><span class="p">);</span> <span class="p">},</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">¶</a> </div> <p>Some nifty numerical helpers.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">isInt</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">value</span> <span class="o">===</span> <span class="o">+</span><span class="nx">value</span> <span class="o">&&</span> <span class="nx">value</span> <span class="o">===</span> <span class="p">(</span><span class="nx">value</span><span class="o">|</span><span class="mi">0</span><span class="p">);</span> <span class="p">},</span>
|
260
259
|
<span class="nx">isFloat</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">value</span> <span class="o">===</span> <span class="o">+</span><span class="nx">value</span> <span class="o">&&</span> <span class="nx">value</span> <span class="o">!==</span> <span class="p">(</span><span class="nx">value</span><span class="o">|</span><span class="mi">0</span><span class="p">);</span> <span class="p">},</span>
|
261
260
|
<span class="nx">isEven</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="p">(</span><span class="nx">value</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">===</span> <span class="mi">0</span><span class="p">)</span> <span class="o">?</span> <span class="kc">true</span> <span class="o">:</span> <span class="kc">false</span><span class="p">;</span> <span class="p">},</span>
|
262
261
|
<span class="nx">isOdd</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="o">!</span><span class="nx">judge</span><span class="p">.</span><span class="nx">utils</span><span class="p">.</span><span class="nx">isEven</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span> <span class="p">},</span></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">¶</a> </div> <p>Converts a ruby flag-first formatted regular expression, given as a string, into JavaScript.
|
263
|
-
This is rudimentary at best, as there are many, many
|
262
|
+
This is rudimentary at best, as there are many, many differences between Ruby
|
264
263
|
and JavaScript when it comes to regexp-fu. Basically, if you validate your records
|
265
|
-
|
264
|
+
using complex regular expressions AND you hope that this will "just work"
|
266
265
|
on the client-side too… you are going to be pretty disappointed.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">convertRegExp</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">string</span><span class="p">)</span> <span class="p">{</span>
|
267
266
|
<span class="kd">var</span> <span class="nx">p</span> <span class="o">=</span> <span class="nx">string</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">).</span><span class="nx">split</span><span class="p">(</span><span class="s1">':'</span><span class="p">),</span>
|
268
267
|
<span class="nx">o</span> <span class="o">=</span> <span class="nx">p</span><span class="p">.</span><span class="nx">shift</span><span class="p">(),</span>
|
@@ -281,4 +280,4 @@ on the client-side too… you are going to be pretty disappointed.</p>
|
|
281
280
|
<span class="p">}</span>
|
282
281
|
<span class="p">};</span>
|
283
282
|
|
284
|
-
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|
283
|
+
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|
data/judge.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{judge}
|
8
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Joe Corcoran"]
|
12
|
-
s.date = %q{2011-08-
|
12
|
+
s.date = %q{2011-08-15}
|
13
13
|
s.description = %q{Validate forms in-place using your model validations}
|
14
14
|
s.email = %q{joe@tribesports.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -42,9 +42,8 @@ judge.Watcher = function (element) {
|
|
42
42
|
this.validators = JSON.parse(this.element.getAttribute('data-validate'));
|
43
43
|
this.defaultMessages = JSON.parse(this.element.form.getAttribute('data-error-messages'));
|
44
44
|
|
45
|
-
//
|
46
|
-
// represented as an
|
47
|
-
// for each validator found on the element.
|
45
|
+
// The `validate` instance method returns the validity of the watched element,
|
46
|
+
// represented as an object containing the element itself and some validity information.
|
48
47
|
this.validate = function() {
|
49
48
|
instance.errorMessages = [];
|
50
49
|
var validators = instance.validators,
|
@@ -335,7 +334,7 @@ judge.utils = {
|
|
335
334
|
},
|
336
335
|
|
337
336
|
// OMG! How can you use eval, you monster! It's totally evil!
|
338
|
-
// It's used for stuff like `(3, '<', 4) => '3 < 4' => true`.
|
337
|
+
// (It's used here for stuff like `(3, '<', 4) => '3 < 4' => true`.)
|
339
338
|
operate: function(input, operator, validInput) {
|
340
339
|
return eval(input+' '+operator+' '+validInput);
|
341
340
|
},
|
@@ -350,14 +349,14 @@ judge.utils = {
|
|
350
349
|
isOdd: function(value) { return !judge.utils.isEven(value); },
|
351
350
|
|
352
351
|
// Converts a ruby flag-first formatted regular expression, given as a string, into JavaScript.
|
353
|
-
// This is rudimentary at best, as there are many, many
|
352
|
+
// This is rudimentary at best, as there are many, many differences between Ruby
|
354
353
|
// and JavaScript when it comes to regexp-fu. Basically, if you validate your records
|
355
|
-
//
|
354
|
+
// using complex regular expressions AND you hope that this will "just work"
|
356
355
|
// on the client-side too… you are going to be pretty disappointed.
|
357
356
|
convertRegExp: function(string) {
|
358
357
|
var p = string.slice(1, -1).split(':'),
|
359
358
|
o = p.shift(),
|
360
|
-
r = p.join(':');
|
359
|
+
r = p.join(':').replace(/\\\\/, '\\');
|
361
360
|
return new RegExp(r, judge.utils.convertFlags(o));
|
362
361
|
},
|
363
362
|
convertFlags: function(string) {
|
data/lib/judge/utils.rb
CHANGED
@@ -696,9 +696,9 @@ describe('judge', function() {
|
|
696
696
|
describe('convertRegExp', function() {
|
697
697
|
|
698
698
|
it('converts string format options-first ruby regexp into RegExp object', function() {
|
699
|
-
var re = judge.utils.convertRegExp('(?mix:[A-Z0-9])');
|
699
|
+
var re = judge.utils.convertRegExp('(?mix:[A-Z0-9]\.)');
|
700
700
|
expect(re).toBeInstanceOf(RegExp);
|
701
|
-
expect(re.source).toEqual('[A-Z0-9]');
|
701
|
+
expect(re.source).toEqual('[A-Z0-9]\.');
|
702
702
|
expect(re.multiline).toEqual(true);
|
703
703
|
expect(re.global).toEqual(false);
|
704
704
|
});
|
@@ -1,17 +1,17 @@
|
|
1
1
|
class Foo < ActiveRecord::Base
|
2
2
|
|
3
|
-
validates :one,
|
4
|
-
validates :two,
|
5
|
-
validates :three,
|
6
|
-
validates :four,
|
7
|
-
|
8
|
-
validates :five,
|
9
|
-
validates :six,
|
10
|
-
validates :seven,
|
11
|
-
validates :eight,
|
3
|
+
validates :one, :presence => true
|
4
|
+
validates :two, :length => { :in => 5..10, :allow_blank => true }
|
5
|
+
validates :three, :exclusion => { :in => %w{foo bar} }
|
6
|
+
validates :four, :numericality => { :only_integer => true, :odd => true, :less_than_or_equal_to => 7 },
|
7
|
+
:exclusion => { :in => [3,12] }
|
8
|
+
validates :five, :format => { :without => /[A-Za-z]+/ }
|
9
|
+
validates :six, :acceptance => true
|
10
|
+
validates :seven, :confirmation => true
|
11
|
+
validates :eight, :confirmation => true
|
12
12
|
|
13
|
-
validates :nine,
|
14
|
-
validates :ten,
|
13
|
+
validates :nine, :presence => true, :uniqueness => true
|
14
|
+
validates :ten, :presence => true
|
15
15
|
validates :eleven, :presence => true
|
16
16
|
validates :twelve, :presence => true
|
17
17
|
|
data/test/judge_test.rb
CHANGED
@@ -70,6 +70,11 @@ class JudgeTest < ActionController::TestCase
|
|
70
70
|
assert_equal "confirmation", validator2["kind"]
|
71
71
|
end
|
72
72
|
|
73
|
+
should "not include uniqueness validator in data attribute" do
|
74
|
+
validators = validators_from("#foo_nine")
|
75
|
+
assert_equal 0, validators.select{ |v| v["kind"] == "uniqueness" }.length
|
76
|
+
end
|
77
|
+
|
73
78
|
end
|
74
79
|
|
75
80
|
context "validated tag output" do
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
8
|
+
- 1
|
9
|
+
version: 0.4.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Joe Corcoran
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-08-
|
17
|
+
date: 2011-08-15 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -239,7 +239,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
239
239
|
requirements:
|
240
240
|
- - ">="
|
241
241
|
- !ruby/object:Gem::Version
|
242
|
-
hash: -
|
242
|
+
hash: -2060341767346972339
|
243
243
|
segments:
|
244
244
|
- 0
|
245
245
|
version: "0"
|