diff --git a/app/assets/javascripts/discourse/controllers/edit_category_controller.js b/app/assets/javascripts/discourse/controllers/edit_category_controller.js index 69258bf01..a431135ea 100644 --- a/app/assets/javascripts/discourse/controllers/edit_category_controller.js +++ b/app/assets/javascripts/discourse/controllers/edit_category_controller.js @@ -58,10 +58,6 @@ Discourse.EditCategoryController = Discourse.ObjectController.extend(Discourse.M return false; }.property('saving', 'name', 'color', 'deleting'), - deleteVisible: function() { - return (this.get('id') && this.get('topic_count') === 0 && !this.get("isUncategorizedCategory")); - }.property('id', 'topic_count'), - deleteDisabled: function() { return (this.get('deleting') || this.get('saving') || false); }.property('disabled', 'saving', 'deleting'), diff --git a/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars b/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars index 5fe53e825..cc45b6b69 100644 --- a/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars +++ b/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars @@ -119,7 +119,7 @@ diff --git a/app/models/category.rb b/app/models/category.rb index 25f6c6fbf..acb1794e3 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -350,6 +350,10 @@ SQL self.where(id: slug.to_i, parent_category_id: parent_category_id).includes(:featured_users).first end + def has_children? + id && Category.where(parent_category_id: id).exists? + end + def uncategorized? id == SiteSetting.uncategorized_category_id end diff --git a/app/serializers/category_serializer.rb b/app/serializers/category_serializer.rb index 1e0cbf70b..caa26fa95 100644 --- a/app/serializers/category_serializer.rb +++ b/app/serializers/category_serializer.rb @@ -1,6 +1,11 @@ class CategorySerializer < BasicCategorySerializer - attributes :read_restricted, :available_groups, :auto_close_hours, :group_permissions, :position + attributes :read_restricted, + :available_groups, + :auto_close_hours, + :group_permissions, + :position, + :can_delete def group_permissions @group_permissions ||= begin @@ -21,4 +26,13 @@ class CategorySerializer < BasicCategorySerializer Group.order(:name).pluck(:name) - group_permissions.map{|g| g[:group_name]} end + + def can_delete + true + end + + def include_can_delete? + scope && scope.can_delete?(object) + end + end diff --git a/lib/guardian/category_guardian.rb b/lib/guardian/category_guardian.rb index 1c6f2ef83..66ce6102f 100644 --- a/lib/guardian/category_guardian.rb +++ b/lib/guardian/category_guardian.rb @@ -11,7 +11,10 @@ module CategoryGuardian end def can_delete_category?(category) - is_admin? && category.topic_count == 0 && !category.uncategorized? + is_admin? && + category.topic_count == 0 && + !category.uncategorized? && + !category.has_children? end def can_see_category?(category) diff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rb index 96e6321f9..f26177bf7 100644 --- a/spec/components/guardian_spec.rb +++ b/spec/components/guardian_spec.rb @@ -882,6 +882,11 @@ describe Guardian do Guardian.new(admin).can_delete?(uncategorized_category).should be_false end + it "can't be deleted if it has children" do + category.expects(:has_children?).returns(true) + Guardian.new(admin).can_delete?(category).should be_false + end + end context 'can_suspend?' do