From 66154145f1d7f9b5aa8590d87d0dbbebe8c7589b Mon Sep 17 00:00:00 2001
From: Jason Williams <jaswilli@gmail.com>
Date: Mon, 12 Jan 2015 03:15:13 +0000
Subject: [PATCH] Fixup validation engine to handle lack of proxying

Closes #4766
- Adjust ValidationEngine so it no longer assumes the properties
  it is validating are proxies via ObjectController.
- Fixup controllers, templates, and routes to use models where
  data needs to be validated.
---
 core/client/controllers/signin.js       |  3 ++-
 core/client/controllers/signup.js       |  3 ++-
 core/client/mixins/validation-engine.js | 21 +++++++++++++++++----
 core/client/routes/signin.js            | 15 ++++++++++++---
 core/client/routes/signup.js            |  6 +++---
 core/client/templates/signin.hbs        |  4 ++--
 6 files changed, 38 insertions(+), 14 deletions(-)

diff --git a/core/client/controllers/signin.js b/core/client/controllers/signin.js
index 1e22c9d2df..beca2f217e 100644
--- a/core/client/controllers/signin.js
+++ b/core/client/controllers/signin.js
@@ -7,7 +7,8 @@ var SigninController = Ember.Controller.extend(SimpleAuth.AuthenticationControll
 
     actions: {
         authenticate: function () {
-            var data = this.getProperties('identification', 'password');
+            var model = this.get('model'),
+                data = model.getProperties('identification', 'password');
 
             this._super(data).catch(function () {
                 // If simple-auth's authenticate rejects we need to catch it
diff --git a/core/client/controllers/signup.js b/core/client/controllers/signup.js
index 82843101b0..5ad8389214 100644
--- a/core/client/controllers/signup.js
+++ b/core/client/controllers/signup.js
@@ -10,7 +10,8 @@ var SignupController = Ember.Controller.extend(ValidationEngine, {
     actions: {
         signup: function () {
             var self = this,
-                data = self.getProperties('model.name', 'model.email', 'model.password', 'model.token');
+                model = this.get('model'),
+                data = model.getProperties('name', 'email', 'password', 'token');
 
             self.notifications.closePassive();
 
diff --git a/core/client/mixins/validation-engine.js b/core/client/mixins/validation-engine.js
index 089f1ac06f..bc58b71c3d 100644
--- a/core/client/mixins/validation-engine.js
+++ b/core/client/mixins/validation-engine.js
@@ -89,11 +89,24 @@ var ValidationEngine = Ember.Mixin.create({
     *                   the class that mixes in this mixin.
     */
     validate: function (opts) {
-        var model = opts.model || this,
-            type = this.get('validationType'),
-            validator = this.get('validators.' + type);
-
+        // jscs:disable safeContextKeyword
         opts = opts || {};
+
+        var model = this,
+            type,
+            validator;
+
+        if (opts.model) {
+            model = opts.model;
+        } else if (this instanceof DS.Model) {
+            model = this;
+        } else if (this.get('model')) {
+            model = this.get('model');
+        }
+
+        type = this.get('validationType') || model.get('validationType');
+        validator = this.get('validators.' + type) || model.get('validators.' + type);
+
         opts.validationType = type;
 
         return new Ember.RSVP.Promise(function (resolve, reject) {
diff --git a/core/client/routes/signin.js b/core/client/routes/signin.js
index 6db40151d0..62c0706227 100644
--- a/core/client/routes/signin.js
+++ b/core/client/routes/signin.js
@@ -12,13 +12,22 @@ var SigninRoute = Ember.Route.extend(styleBody, loadingIndicator, {
         }
     },
 
+    model: function () {
+        return Ember.Object.create({
+            identification: '',
+            password: ''
+        });
+    },
+
     // the deactivate hook is called after a route has been exited.
     deactivate: function () {
         this._super();
 
-        // clear the properties that hold the credentials from the controller
-        // when we're no longer on the signin screen
-        this.controllerFor('signin').setProperties({identification: '', password: ''});
+        var controller = this.controllerFor('signin');
+
+        // clear the properties that hold the credentials when we're no longer on the signin screen
+        controller.set('model.identification', '');
+        controller.set('model.password', '');
     }
 });
 
diff --git a/core/client/routes/signup.js b/core/client/routes/signup.js
index d49de138ed..b8a5085aa9 100644
--- a/core/client/routes/signup.js
+++ b/core/client/routes/signup.js
@@ -14,7 +14,7 @@ var SignupRoute = Ember.Route.extend(styleBody, loadingIndicator, {
         var self = this,
             tokenText,
             email,
-            model = {},
+            model = Ember.Object.create(),
             re = /^(?:[A-Za-z0-9_\-]{4})*(?:[A-Za-z0-9_\-]{2}|[A-Za-z0-9_\-]{3})?$/;
 
         return new Ember.RSVP.Promise(function (resolve) {
@@ -27,8 +27,8 @@ var SignupRoute = Ember.Route.extend(styleBody, loadingIndicator, {
             tokenText = atob(params.token);
             email = tokenText.split('|')[1];
 
-            model.email = email;
-            model.token = params.token;
+            model.set('email', email);
+            model.set('token', params.token);
 
             return ic.ajax.request({
                 url: self.get('ghostPaths.url').api('authentication', 'invitation'),
diff --git a/core/client/templates/signin.hbs b/core/client/templates/signin.hbs
index 27e2d54e1f..6b505101c1 100644
--- a/core/client/templates/signin.hbs
+++ b/core/client/templates/signin.hbs
@@ -2,12 +2,12 @@
     <form id="login" class="login-form" method="post" novalidate="novalidate" {{action 'validateAndAuthenticate' on='submit'}}>
         <div class="email-wrap">
             <span class="input-icon icon-mail">
-                {{gh-trim-focus-input class="email" type="email" placeholder="Email Address" name="identification" autocapitalize="off" autocorrect="off" value=identification}}
+                {{gh-trim-focus-input class="email" type="email" placeholder="Email Address" name="identification" autocapitalize="off" autocorrect="off" value=model.identification}}
             </span>
         </div>
         <div class="password-wrap">
             <span class="input-icon icon-lock">
-                {{input class="password" type="password" placeholder="Password" name="password" value=password}}
+                {{input class="password" type="password" placeholder="Password" name="password" value=model.password}}
             </span>
         </div>
         <button class="btn btn-blue" type="submit" {{action "validateAndAuthenticate"}} {{bind-attr disabled=submitting}}>Log in</button>