FIX: Don't allow parent categories to be deleted. Also, remove

duplicated logic and rely on the server response for `can_delete`
status.
This commit is contained in:
Robin Ward 2014-02-12 17:24:25 -05:00
parent a963dd9081
commit f73a3f252a
6 changed files with 29 additions and 7 deletions

View file

@ -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'),

View file

@ -119,7 +119,7 @@
</div>
<div class="modal-footer">
<button class='btn btn-primary' {{bind-attr disabled="disabled"}} {{action saveCategory}}>{{buttonTitle}}</button>
{{#if deleteVisible}}
{{#if can_delete}}
<button class='btn btn-danger pull-right' {{bind-attr disabled="deleteDisabled"}} {{action deleteCategory}}><i class="fa fa-trash-o"></i>{{deleteButtonTitle}}</button>
{{/if}}
</div>

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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