From 96772af35b4640f4592d6c837f839c5809cb7555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 27 Aug 2013 23:01:35 +0200 Subject: [PATCH] FIX: avatar thumbnail won't save after upload --- .../controllers/avatar_selector_controller.js | 14 +++++-- .../javascripts/discourse/models/user.js | 23 ++++++---- .../discourse/routes/preferences_routes.js | 42 ++++++++----------- .../modal/avatar_selector.js.handlebars | 4 +- .../avatar_selector_controller_test.js | 28 +++++++++++++ 5 files changed, 73 insertions(+), 38 deletions(-) create mode 100644 test/javascripts/controllers/avatar_selector_controller_test.js diff --git a/app/assets/javascripts/discourse/controllers/avatar_selector_controller.js b/app/assets/javascripts/discourse/controllers/avatar_selector_controller.js index 861853ca4..91ce9648b 100644 --- a/app/assets/javascripts/discourse/controllers/avatar_selector_controller.js +++ b/app/assets/javascripts/discourse/controllers/avatar_selector_controller.js @@ -8,7 +8,15 @@ @module Discourse **/ Discourse.AvatarSelectorController = Discourse.Controller.extend(Discourse.ModalFunctionality, { - toggleUseUploadedAvatar: function(toggle) { - this.set("use_uploaded_avatar", toggle); - } + useUploadedAvatar: function() { + this.set("use_uploaded_avatar", true); + }, + + useGravatar: function() { + this.set("use_uploaded_avatar", false); + }, + + avatarTemplate: function() { + return this.get("use_uploaded_avatar") ? this.get("uploaded_avatar_template") : this.get("gravatar_template"); + }.property("use_uploaded_avatar", "uploaded_avatar_template", "gravatar_template") }); diff --git a/app/assets/javascripts/discourse/models/user.js b/app/assets/javascripts/discourse/models/user.js index 4abb65886..c367dd875 100644 --- a/app/assets/javascripts/discourse/models/user.js +++ b/app/assets/javascripts/discourse/models/user.js @@ -28,7 +28,11 @@ Discourse.User = Discourse.Model.extend({ searchContext: function() { - return ({ type: 'user', id: this.get('username_lower'), user: this }); + return { + type: 'user', + id: this.get('username_lower'), + user: this + }; }.property('username_lower'), /** @@ -101,7 +105,7 @@ Discourse.User = Discourse.Model.extend({ @returns Result of ajax call **/ changeUsername: function(newUsername) { - return Discourse.ajax("/users/" + (this.get('username_lower')) + "/preferences/username", { + return Discourse.ajax("/users/" + this.get('username_lower') + "/preferences/username", { type: 'PUT', data: { new_username: newUsername } }); @@ -115,7 +119,7 @@ Discourse.User = Discourse.Model.extend({ @returns Result of ajax call **/ changeEmail: function(email) { - return Discourse.ajax("/users/" + (this.get('username_lower')) + "/preferences/email", { + return Discourse.ajax("/users/" + this.get('username_lower') + "/preferences/email", { type: 'PUT', data: { email: email } }); @@ -173,9 +177,7 @@ Discourse.User = Discourse.Model.extend({ changePassword: function() { return Discourse.ajax("/session/forgot_password", { dataType: 'json', - data: { - login: this.get('username') - }, + data: { login: this.get('username') }, type: 'POST' }); }, @@ -266,11 +268,14 @@ Discourse.User = Discourse.Model.extend({ Change avatar selection @method toggleAvatarSelection + @param {Boolean} useUploadedAvatar true if the user is using the uploaded avatar @returns {Promise} the result of the toggle avatar selection */ - toggleAvatarSelection: function() { - var data = { use_uploaded_avatar: this.get("use_uploaded_avatar") }; - return Discourse.ajax("/users/" + this.get("username") + "/preferences/avatar/toggle", { type: 'PUT', data: data }); + toggleAvatarSelection: function(useUploadedAvatar) { + return Discourse.ajax("/users/" + this.get("username_lower") + "/preferences/avatar/toggle", { + type: 'PUT', + data: { use_uploaded_avatar: useUploadedAvatar } + }); } }); diff --git a/app/assets/javascripts/discourse/routes/preferences_routes.js b/app/assets/javascripts/discourse/routes/preferences_routes.js index e623cf3c4..e337a43a8 100644 --- a/app/assets/javascripts/discourse/routes/preferences_routes.js +++ b/app/assets/javascripts/discourse/routes/preferences_routes.js @@ -18,35 +18,29 @@ Discourse.PreferencesRoute = Discourse.RestrictedUserRoute.extend({ events: { showAvatarSelector: function() { Discourse.Route.showModal(this, 'avatarSelector'); - var user = this.modelFor("user"); - console.log(user); - this.controllerFor("avatarSelector").setProperties(user.getProperties( - "username", - "email", - "has_uploaded_avatar", - "use_uploaded_avatar", - "gravatar_template", - "uploaded_avatar_template" - )); + // all the properties needed for displaying the avatar selector modal + var avatarSelector = this.modelFor('user').getProperties( + 'username', 'email', + 'has_uploaded_avatar', 'use_uploaded_avatar', + 'gravatar_template', 'uploaded_avatar_template'); + this.controllerFor('avatarSelector').setProperties(avatarSelector); }, saveAvatarSelection: function() { - var user = this.modelFor("user"); - var avatar = this.controllerFor("avatarSelector"); + var user = this.modelFor('user'); + var avatarSelector = this.controllerFor('avatarSelector'); // sends the information to the server if it has changed - if (avatar.get("use_uploaded_avatar") !== user.get("use_uploaded_avatar")) { user.toggleAvatarSelection(); } - // saves the data back - user.setProperties(avatar.getProperties( - "has_uploaded_avatar", - "use_uploaded_avatar", - "gravatar_template", - "uploaded_avatar_template" - )); - if (avatar.get("use_uploaded_avatar")) { - user.set("avatar_template", avatar.get("uploaded_avatar_template")); - } else { - user.set("avatar_template", avatar.get("gravatar_template")); + if (avatarSelector.get('use_uploaded_avatar') !== user.get('use_uploaded_avatar')) { + user.toggleAvatarSelection(avatarSelector.get('use_uploaded_avatar')); } + // saves the data back + user.setProperties(avatarSelector.getProperties( + 'has_uploaded_avatar', + 'use_uploaded_avatar', + 'gravatar_template', + 'uploaded_avatar_template' + )); + user.set('avatar_template', avatarSelector.get('avatarTemplate')); } } }); diff --git a/app/assets/javascripts/discourse/templates/modal/avatar_selector.js.handlebars b/app/assets/javascripts/discourse/templates/modal/avatar_selector.js.handlebars index 4548c091b..63fcf8e34 100644 --- a/app/assets/javascripts/discourse/templates/modal/avatar_selector.js.handlebars +++ b/app/assets/javascripts/discourse/templates/modal/avatar_selector.js.handlebars @@ -1,12 +1,12 @@