REFACTOR: Use common path for RESTful DELETE action from upload image

component
This commit is contained in:
Robin Ward 2014-06-27 14:48:39 -04:00
parent 3cbb32cc20
commit 9000c358d1
9 changed files with 45 additions and 39 deletions

View File

@ -13,10 +13,7 @@ export default Em.Component.extend(UploadMixin, {
this.set('imageUrl', data.result.url); this.set('imageUrl', data.result.url);
}, },
actions: { deleteDone: function() {
trash: function() { this.set('imageUrl', null);
this.set('imageUrl', null);
this.sendAction('clear');
}
} }
}); });

View File

@ -70,10 +70,6 @@ export default Discourse.ObjectController.extend({
actions: { actions: {
clearProfileBackground: function() {
this.get('model').clearProfileBackground();
},
save: function() { save: function() {
var self = this; var self = this;
this.setProperties({ saving: true, saved: false }); this.setProperties({ saving: true, saved: false });

View File

@ -12,17 +12,15 @@ export default Discourse.Controller.extend(Discourse.ModalFunctionality, {
local: false, local: false,
showMore: false, showMore: false,
init: function() { _initialize: function() {
this._super();
this.setProperties({ this.setProperties({
local: this.get("allowLocal"), local: this.get("allowLocal"),
showMore: false showMore: false
}); });
}, }.on('init'),
allowLocal: function() { maxSize: Discourse.computed.setting('max_attachment_size_kb'),
return Discourse.SiteSettings.max_attachment_size_kb > 0; allowLocal: Em.computed.gt('maxSize', 0),
}.property(),
actions: { actions: {
useLocal: function() { this.setProperties({ local: true, showMore: false}); }, useLocal: function() { this.setProperties({ local: true, showMore: false}); },

View File

@ -6,6 +6,10 @@ export default Em.Mixin.create({
Em.warn("You should implement `uploadDone`"); Em.warn("You should implement `uploadDone`");
}, },
deleteDone: function() {
Em.warn("You should implement `deleteDone`");
},
_initializeUploader: function() { _initializeUploader: function() {
var $upload = this.$('input[type=file]'), // note: we can't cache this as fileupload replaces the input after upload var $upload = this.$('input[type=file]'), // note: we can't cache this as fileupload replaces the input after upload
self = this; self = this;
@ -49,6 +53,18 @@ export default Em.Mixin.create({
actions: { actions: {
selectFile: function() { selectFile: function() {
this.$('input[type=file]').click(); this.$('input[type=file]').click();
},
trash: function() {
var self = this;
Discourse.ajax(this.get('uploadUrl'), {
type: 'DELETE',
data: { image_type: this.get('type') }
}).then(function() {
self.deleteDone();
}).catch(function() {
bootbox.alert(I18n.t('generic_error'));
});
} }
} }
}); });

View File

@ -343,19 +343,6 @@ Discourse.User = Discourse.Model.extend({
}); });
}, },
/*
Clear profile background
@method clearProfileBackground
@returns {Promise} the result of the clear profile background request
*/
clearProfileBackground: function() {
return Discourse.ajax("/users/" + this.get("username_lower") + "/preferences/profile_background/clear", {
type: 'PUT',
data: { }
});
},
/** /**
Determines whether the current user is allowed to upload a file. Determines whether the current user is allowed to upload a file.

View File

@ -94,8 +94,7 @@
<div class="controls"> <div class="controls">
{{image-uploader uploadUrl=imageUploadUrl {{image-uploader uploadUrl=imageUploadUrl
imageUrl=profile_background imageUrl=profile_background
type="profile_background" type="profile_background"}}
clear="clearProfileBackground"}}
</div> </div>
</div> </div>
{{/if}} {{/if}}

View File

@ -7,7 +7,7 @@ class UsersController < ApplicationController
skip_before_filter :authorize_mini_profiler, only: [:avatar] skip_before_filter :authorize_mini_profiler, only: [:avatar]
skip_before_filter :check_xhr, only: [:show, :password_reset, :update, :activate_account, :authorize_email, :user_preferences_redirect, :avatar, :my_redirect] skip_before_filter :check_xhr, only: [:show, :password_reset, :update, :activate_account, :authorize_email, :user_preferences_redirect, :avatar, :my_redirect]
before_filter :ensure_logged_in, only: [:username, :update, :change_email, :user_preferences_redirect, :upload_user_image, :pick_avatar, :clear_profile_background, :destroy] before_filter :ensure_logged_in, only: [:username, :update, :change_email, :user_preferences_redirect, :upload_user_image, :pick_avatar, :destroy_user_image, :destroy]
before_filter :respond_to_suspicious_request, only: [:create] before_filter :respond_to_suspicious_request, only: [:create]
# we need to allow account creation with bad CSRF tokens, if people are caching, the CSRF token on the # we need to allow account creation with bad CSRF tokens, if people are caching, the CSRF token on the
@ -380,11 +380,16 @@ class UsersController < ApplicationController
render nothing: true render nothing: true
end end
def clear_profile_background def destroy_user_image
user = fetch_user_from_params user = fetch_user_from_params
guardian.ensure_can_edit!(user) guardian.ensure_can_edit!(user)
user.user_profile.clear_profile_background image_type = params.require(:image_type)
if image_type == 'profile_background'
user.user_profile.clear_profile_background
else
raise Discourse::InvalidParameters.new(:image_type)
end
render nothing: true render nothing: true
end end

View File

@ -203,8 +203,8 @@ Discourse::Application.routes.draw do
get "users/:username/avatar(/:size)" => "users#avatar", constraints: {username: USERNAME_ROUTE_FORMAT} # LEGACY ROUTE get "users/:username/avatar(/:size)" => "users#avatar", constraints: {username: USERNAME_ROUTE_FORMAT} # LEGACY ROUTE
post "users/:username/preferences/avatar" => "users#upload_avatar", constraints: {username: USERNAME_ROUTE_FORMAT} # LEGACY ROUTE post "users/:username/preferences/avatar" => "users#upload_avatar", constraints: {username: USERNAME_ROUTE_FORMAT} # LEGACY ROUTE
post "users/:username/preferences/user_image" => "users#upload_user_image", constraints: {username: USERNAME_ROUTE_FORMAT} post "users/:username/preferences/user_image" => "users#upload_user_image", constraints: {username: USERNAME_ROUTE_FORMAT}
delete "users/:username/preferences/user_image" => "users#destroy_user_image", constraints: {username: USERNAME_ROUTE_FORMAT}
put "users/:username/preferences/avatar/pick" => "users#pick_avatar", constraints: {username: USERNAME_ROUTE_FORMAT} put "users/:username/preferences/avatar/pick" => "users#pick_avatar", constraints: {username: USERNAME_ROUTE_FORMAT}
put "users/:username/preferences/profile_background/clear" => "users#clear_profile_background", constraints: {username: USERNAME_ROUTE_FORMAT}
get "users/:username/invited" => "users#invited", constraints: {username: USERNAME_ROUTE_FORMAT} get "users/:username/invited" => "users#invited", constraints: {username: USERNAME_ROUTE_FORMAT}
post "users/:username/send_activation_email" => "users#send_activation_email", constraints: {username: USERNAME_ROUTE_FORMAT} post "users/:username/send_activation_email" => "users#send_activation_email", constraints: {username: USERNAME_ROUTE_FORMAT}
get "users/:username/activity" => "users#show", constraints: {username: USERNAME_ROUTE_FORMAT} get "users/:username/activity" => "users#show", constraints: {username: USERNAME_ROUTE_FORMAT}

View File

@ -1269,10 +1269,10 @@ describe UsersController do
end end
describe '.clear_profile_background' do describe '.destroy_user_image' do
it 'raises an error when not logged in' do it 'raises an error when not logged in' do
lambda { xhr :put, :clear_profile_background, username: 'asdf' }.should raise_error(Discourse::NotLoggedIn) lambda { xhr :put, :destroy_user_image, type: 'profile_background', username: 'asdf' }.should raise_error(Discourse::NotLoggedIn)
end end
context 'while logged in' do context 'while logged in' do
@ -1281,12 +1281,20 @@ describe UsersController do
it 'raises an error when you don\'t have permission to clear the profile background' do it 'raises an error when you don\'t have permission to clear the profile background' do
Guardian.any_instance.expects(:can_edit?).with(user).returns(false) Guardian.any_instance.expects(:can_edit?).with(user).returns(false)
xhr :put, :clear_profile_background, username: user.username xhr :put, :destroy_user_image, username: user.username, image_type: 'profile_background'
response.should be_forbidden response.should be_forbidden
end end
it 'it successful' do it "requires the `image_type` param" do
xhr :put, :clear_profile_background, username: user.username -> { xhr :put, :destroy_user_image, username: user.username }.should raise_error(ActionController::ParameterMissing)
end
it "only allows certain `image_types`" do
-> { xhr :put, :destroy_user_image, username: user.username, image_type: 'wat' }.should raise_error(Discourse::InvalidParameters)
end
it 'can clear the profile background' do
xhr :put, :destroy_user_image, image_type: 'profile_background', username: user.username
user.reload.user_profile.profile_background.should == "" user.reload.user_profile.profile_background.should == ""
response.should be_success response.should be_success
end end