Make it possible to edit a category with an empty slug

This commit is contained in:
Neil Lalonde 2013-04-18 17:07:06 -04:00
parent cbe0168922
commit 372442bd1c
3 changed files with 11 additions and 11 deletions
app
assets/javascripts/discourse
controllers

View file

@ -38,14 +38,14 @@ Discourse.Category = Discourse.Model.extend({
},
destroy: function(callback) {
return Discourse.ajax(Discourse.getURL("/categories/") + (this.get('slug')), { type: 'DELETE' });
return Discourse.ajax(Discourse.getURL("/categories/") + (this.get('slug') || this.get('id')), { type: 'DELETE' });
}
});
Discourse.Category.reopenClass({
findBySlug: function(categorySlug) {
return Discourse.ajax({url: Discourse.getURL("/categories/") + categorySlug + ".json"}).then(function (result) {
findBySlugOrId: function(slugOrId) {
return Discourse.ajax({url: Discourse.getURL("/categories/") + slugOrId + ".json"}).then(function (result) {
return Discourse.Category.create(result.category);
});
}

View file

@ -72,7 +72,7 @@ Discourse.EditCategoryView = Discourse.ModalBodyView.extend({
var categoryView = this;
// We need the topic_count to be correct, so get the most up-to-date info about this category from the server.
Discourse.Category.findBySlug( this.get('category.slug') ).then( function(cat) {
Discourse.Category.findBySlugOrId( this.get('category.slug') || this.get('category.id') ).then( function(cat) {
categoryView.set('category', cat);
Discourse.get('site').updateCategory(cat);
categoryView.set('id', categoryView.get('category.slug'));

View file

@ -3,6 +3,7 @@ require_dependency 'category_serializer'
class CategoriesController < ApplicationController
before_filter :ensure_logged_in, except: [:index, :show]
before_filter :fetch_category, only: [:show, :update, :destroy]
def index
list = CategoryList.new(current_user)
@ -11,7 +12,6 @@ class CategoriesController < ApplicationController
end
def show
@category = Category.where(slug: params[:id]).first
render_serialized(@category, CategorySerializer)
end
@ -27,17 +27,13 @@ class CategoriesController < ApplicationController
def update
requires_parameters(*required_param_keys)
@category = Category.where(id: params[:id]).first
guardian.ensure_can_edit!(@category)
json_result(@category, serializer: CategorySerializer) { |cat| cat.update_attributes(category_params) }
end
def destroy
category = Category.where(slug: params[:id]).first
guardian.ensure_can_delete!(category)
category.destroy
guardian.ensure_can_delete!(@category)
@category.destroy
render nothing: true
end
@ -54,4 +50,8 @@ class CategoriesController < ApplicationController
def category_params
params.slice(*category_param_keys)
end
def fetch_category
@category = Category.where(slug: params[:id]).first || Category.where(id: params[:id].to_i).first
end
end