From b1c1175c67bc22102a3a8d31ea6b7ec1c56a6a56 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 2 Dec 2010 00:54:35 +0300 Subject: [PATCH 1/7] javascript validations for username --- app/views/invitations/edit.html.haml | 16 ++++++++++++++++ app/views/registrations/new.html.haml | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/views/invitations/edit.html.haml b/app/views/invitations/edit.html.haml index 1eac967f6..306d4fd18 100644 --- a/app/views/invitations/edit.html.haml +++ b/app/views/invitations/edit.html.haml @@ -1,3 +1,19 @@ + :javascript + $(function() { + var Validation = { + username: { + characters: /^(|[A-Za-z0-9_]{0,32})$/, + length: [6, 32] + } + }; + + $("#user_username").keypress(function(evt) { + if(!Validation.username.characters.test(this.value + String.fromCharCode($ + evt.preventDefault(); + } + }); + }); + = form_for(resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :put }) do |f| %p = f.label :username diff --git a/app/views/registrations/new.html.haml b/app/views/registrations/new.html.haml index a8f54e153..cfe6e2dc2 100644 --- a/app/views/registrations/new.html.haml +++ b/app/views/registrations/new.html.haml @@ -1,3 +1,19 @@ + :javascript + $(function() { + var Validation = { + username: { + characters: /^(|[A-Za-z0-9_]{0,32})$/, + length: [6, 32] + } + }; + + $("#user_username").keypress(function(evt) { + if(!Validation.username.characters.test(this.value + String.fromCharCode($ + evt.preventDefault(); + } + }); + }); + .span-12.prepend-6.last .floating %h3 From 9e7d7f420fa14095abbaf7395cee9644e5eacb8e Mon Sep 17 00:00:00 2001 From: root Date: Thu, 2 Dec 2010 00:57:56 +0300 Subject: [PATCH 2/7] hate text editing in nano --- app/views/invitations/edit.html.haml | 2 +- app/views/registrations/new.html.haml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/invitations/edit.html.haml b/app/views/invitations/edit.html.haml index 306d4fd18..a894947be 100644 --- a/app/views/invitations/edit.html.haml +++ b/app/views/invitations/edit.html.haml @@ -8,7 +8,7 @@ }; $("#user_username").keypress(function(evt) { - if(!Validation.username.characters.test(this.value + String.fromCharCode($ + if(!Validation.username.characters.test(this.value + String.fromCharCode(evt.charCode)) { evt.preventDefault(); } }); diff --git a/app/views/registrations/new.html.haml b/app/views/registrations/new.html.haml index cfe6e2dc2..918acd57b 100644 --- a/app/views/registrations/new.html.haml +++ b/app/views/registrations/new.html.haml @@ -8,7 +8,7 @@ }; $("#user_username").keypress(function(evt) { - if(!Validation.username.characters.test(this.value + String.fromCharCode($ + if(!Validation.username.characters.test(this.value + String.fromCharCode(evt.charCode)) { evt.preventDefault(); } }); From 5cd0a600730bcaccf7a51743ef991550295bfab0 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 2 Dec 2010 02:22:05 +0300 Subject: [PATCH 3/7] Moved to it's own file, added specs --- app/views/invitations/edit.html.haml | 17 ++------------ app/views/registrations/new.html.haml | 18 ++------------- public/javascripts/validation.js | 19 +++++++++++++++ spec/javascripts/support/jasmine.yml | 1 + spec/javascripts/validation-spec.js | 33 +++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 31 deletions(-) create mode 100644 public/javascripts/validation.js create mode 100644 spec/javascripts/validation-spec.js diff --git a/app/views/invitations/edit.html.haml b/app/views/invitations/edit.html.haml index a894947be..29ccd4328 100644 --- a/app/views/invitations/edit.html.haml +++ b/app/views/invitations/edit.html.haml @@ -1,18 +1,5 @@ - :javascript - $(function() { - var Validation = { - username: { - characters: /^(|[A-Za-z0-9_]{0,32})$/, - length: [6, 32] - } - }; - - $("#user_username").keypress(function(evt) { - if(!Validation.username.characters.test(this.value + String.fromCharCode(evt.charCode)) { - evt.preventDefault(); - } - }); - }); +%script{:type => "text/javascript", + :src => "/javascripts/validation.js"} = form_for(resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :put }) do |f| %p diff --git a/app/views/registrations/new.html.haml b/app/views/registrations/new.html.haml index 918acd57b..05c7ffe64 100644 --- a/app/views/registrations/new.html.haml +++ b/app/views/registrations/new.html.haml @@ -1,19 +1,5 @@ - :javascript - $(function() { - var Validation = { - username: { - characters: /^(|[A-Za-z0-9_]{0,32})$/, - length: [6, 32] - } - }; - - $("#user_username").keypress(function(evt) { - if(!Validation.username.characters.test(this.value + String.fromCharCode(evt.charCode)) { - evt.preventDefault(); - } - }); - }); - +%script {:type => "text/javascript" + :src "/javascripts/validation.js"} .span-12.prepend-6.last .floating %h3 diff --git a/public/javascripts/validation.js b/public/javascripts/validation.js new file mode 100644 index 000000000..937e03544 --- /dev/null +++ b/public/javascripts/validation.js @@ -0,0 +1,19 @@ +var Validation = { + rules: { + username: { + characters: /^(|[A-Za-z0-9_]{0,32})$/, + length: [6, 32] + } + }, + events: { + usernameKeypress: function(evt) { + if(!Validation.rules.username.characters.test(this.value + String.fromCharCode(evt.charCode))) { + evt.preventDefault(); + } + } + } +}; + +$(function() { + $("#user_username").keypress(Validation.events.usernameKeypress); +}); diff --git a/spec/javascripts/support/jasmine.yml b/spec/javascripts/support/jasmine.yml index 61c9341fe..1b5cb5a34 100644 --- a/spec/javascripts/support/jasmine.yml +++ b/spec/javascripts/support/jasmine.yml @@ -14,6 +14,7 @@ src_files: - public/javascripts/vendor/jquery144.js - public/javascripts/vendor/jquery-ui-1.8.6.custom.min.js - public/javascripts/vendor/jquery.tipsy.js + - public/javascripts/validation.js - public/javascripts/diaspora.js - public/javascripts/mobile.js - public/javascripts/aspect-edit.js diff --git a/spec/javascripts/validation-spec.js b/spec/javascripts/validation-spec.js new file mode 100644 index 000000000..efca241de --- /dev/null +++ b/spec/javascripts/validation-spec.js @@ -0,0 +1,33 @@ +describe("Validation", function() { + describe("rules", function() { + it("contains all the rules for validation"); + + describe("username", function() { + describe("characters", function() { + it("is the regex for checking if we allow what the user typed"); + }); + }); + }); + describe("events", function() { + it("contains all the events that use validation methods"); + describe("usernameKeypress", function() { + beforeEach(function() { + $("#jasmine_content").html( + ' ' + ); + }); + + it("doesn't allow the user to type anything but letters, numbers and underscores", function() { + expect(Validation.rules.username.characters.test("*")).toBeFalsy(); + expect(Validation.rules.username.characters.test("Aa_")).toBeTruthy(); + expect(Validation.rules.username.characters.test("ffffffffffffffffffffffffffffffffff")).toBeFalsy(); + }); + + it("is called when the user presses a key on #user_username") { + spyOn(Validation.events, "usernameKeypress"); + $("#user_username").keypress(); + expect(Validation.events.usernameKeypress).toHaveBeenCalled(); + }); + }); + }); +}); From 5d31a24e9edf460c7b89c5306a2fcc24a3df2e49 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 2 Dec 2010 02:54:38 +0300 Subject: [PATCH 4/7] fixed spec, jquery event binding doesnt call the function itself, but a 'clone' of the function --- public/javascripts/validation.js | 4 ++++ spec/javascripts/validation-spec.js | 13 +------------ 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/public/javascripts/validation.js b/public/javascripts/validation.js index 937e03544..0a7279444 100644 --- a/public/javascripts/validation.js +++ b/public/javascripts/validation.js @@ -1,3 +1,7 @@ +/* Copyright (c) 2010, Diaspora Inc. This file is + * licensed under the Affero General Public License version 3 or later. See + * the COPYRIGHT file. + */ var Validation = { rules: { username: { diff --git a/spec/javascripts/validation-spec.js b/spec/javascripts/validation-spec.js index efca241de..0b9f4f197 100644 --- a/spec/javascripts/validation-spec.js +++ b/spec/javascripts/validation-spec.js @@ -11,23 +11,12 @@ describe("Validation", function() { describe("events", function() { it("contains all the events that use validation methods"); describe("usernameKeypress", function() { - beforeEach(function() { - $("#jasmine_content").html( - ' ' - ); - }); it("doesn't allow the user to type anything but letters, numbers and underscores", function() { expect(Validation.rules.username.characters.test("*")).toBeFalsy(); expect(Validation.rules.username.characters.test("Aa_")).toBeTruthy(); expect(Validation.rules.username.characters.test("ffffffffffffffffffffffffffffffffff")).toBeFalsy(); - }); - - it("is called when the user presses a key on #user_username") { - spyOn(Validation.events, "usernameKeypress"); - $("#user_username").keypress(); - expect(Validation.events.usernameKeypress).toHaveBeenCalled(); - }); + }); }); }); }); From f81dc0fed6a42ce3a943848b382a7ec1b31d7523 Mon Sep 17 00:00:00 2001 From: Dan Hansen Date: Wed, 1 Dec 2010 21:25:58 -0800 Subject: [PATCH 5/7] remove empty specs --- spec/javascripts/validation-spec.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/spec/javascripts/validation-spec.js b/spec/javascripts/validation-spec.js index 0b9f4f197..e896d0db5 100644 --- a/spec/javascripts/validation-spec.js +++ b/spec/javascripts/validation-spec.js @@ -1,17 +1,15 @@ describe("Validation", function() { - describe("rules", function() { - it("contains all the rules for validation"); - + describe("rules", function() { describe("username", function() { describe("characters", function() { - it("is the regex for checking if we allow what the user typed"); + it("is the regex for checking if we allow what the user typed", function() { + expect(typeof Validation.rules.username.characters).toEqual("function"); + }); }); }); }); describe("events", function() { - it("contains all the events that use validation methods"); describe("usernameKeypress", function() { - it("doesn't allow the user to type anything but letters, numbers and underscores", function() { expect(Validation.rules.username.characters.test("*")).toBeFalsy(); expect(Validation.rules.username.characters.test("Aa_")).toBeTruthy(); From f3e3e0f344f4041d9596a3bfc94c38895a538ab6 Mon Sep 17 00:00:00 2001 From: Dan Hansen Date: Wed, 1 Dec 2010 22:12:57 -0800 Subject: [PATCH 6/7] --- app/views/invitations/edit.html.haml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/views/invitations/edit.html.haml b/app/views/invitations/edit.html.haml index 29ccd4328..7b315050b 100644 --- a/app/views/invitations/edit.html.haml +++ b/app/views/invitations/edit.html.haml @@ -1,5 +1,4 @@ -%script{:type => "text/javascript", - :src => "/javascripts/validation.js"} += javascript_include_tag "validation" = form_for(resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :put }) do |f| %p From d76ae8618045da0bb34fa3b42a704555615b32c0 Mon Sep 17 00:00:00 2001 From: Dan Hansen Date: Wed, 1 Dec 2010 22:13:15 -0800 Subject: [PATCH 7/7] --- app/views/registrations/new.html.haml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/registrations/new.html.haml b/app/views/registrations/new.html.haml index 05c7ffe64..0cf3fc759 100644 --- a/app/views/registrations/new.html.haml +++ b/app/views/registrations/new.html.haml @@ -1,5 +1,5 @@ -%script {:type => "text/javascript" - :src "/javascripts/validation.js"} += javascript_include_tag "validation" + .span-12.prepend-6.last .floating %h3