From 8a830fb8e39aa3ef25d30368e12561660feed11e Mon Sep 17 00:00:00 2001 From: verg Date: Tue, 17 Dec 2013 15:36:15 -0500 Subject: [PATCH] Prevent deleting 'uncategorized' category --- .../discourse/controllers/edit_category_controller.js | 2 +- app/assets/javascripts/discourse/models/category.js | 5 ++++- app/models/category.rb | 4 ++++ lib/guardian.rb | 2 +- spec/components/guardian_spec.rb | 6 ++++++ 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/edit_category_controller.js b/app/assets/javascripts/discourse/controllers/edit_category_controller.js index 7dcf4d7f93b..90501549415 100644 --- a/app/assets/javascripts/discourse/controllers/edit_category_controller.js +++ b/app/assets/javascripts/discourse/controllers/edit_category_controller.js @@ -53,7 +53,7 @@ Discourse.EditCategoryController = Discourse.ObjectController.extend(Discourse.M }.property('saving', 'name', 'color', 'deleting'), deleteVisible: function() { - return (this.get('id') && this.get('topic_count') === 0); + return (this.get('id') && this.get('topic_count') === 0 && !this.get("isUncategorizedCategory")); }.property('id', 'topic_count'), deleteDisabled: function() { diff --git a/app/assets/javascripts/discourse/models/category.js b/app/assets/javascripts/discourse/models/category.js index fd517a64ece..a42fa72e2d8 100644 --- a/app/assets/javascripts/discourse/models/category.js +++ b/app/assets/javascripts/discourse/models/category.js @@ -168,8 +168,11 @@ Discourse.Category = Discourse.Model.extend({ if (stats.length === 2) return false; }, this); return stats; - } + }, + isUncategorizedCategory: function() { + return this.get('id') === Discourse.Site.currentProp("uncategorized_category_id"); + }.property('id') }); Discourse.Category.reopenClass({ diff --git a/app/models/category.rb b/app/models/category.rb index 2aae060a439..33573b0c0be 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -338,6 +338,10 @@ SQL [read_restricted, mapped] end + + def uncatgorized? + id == SiteSetting.uncategorized_category_id + end end # == Schema Information diff --git a/lib/guardian.rb b/lib/guardian.rb index e261d492ace..d59459ed68b 100644 --- a/lib/guardian.rb +++ b/lib/guardian.rb @@ -320,7 +320,7 @@ class Guardian end def can_delete_category?(category) - is_staff? && category.topic_count == 0 + is_staff? && category.topic_count == 0 && !category.uncatgorized? end def can_delete_topic?(topic) diff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rb index deaebd000b9..7334f743363 100644 --- a/spec/components/guardian_spec.rb +++ b/spec/components/guardian_spec.rb @@ -800,6 +800,12 @@ describe Guardian do Guardian.new(moderator).can_delete?(category).should be_false end + it "can't be deleted if it is the Uncategorizied Category" do + uncategorized_cat_id = SiteSetting.uncategorized_category_id + uncategorized_category = Category.find(uncategorized_cat_id) + Guardian.new(admin).can_delete?(uncategorized_category).should be_false + end + end context 'can_suspend?' do