FEATURE: show a reason why a category can't be deleted

This commit is contained in:
Neil Lalonde 2014-07-16 15:43:44 -04:00
parent fb8dda7f42
commit 5c70d878a7
5 changed files with 44 additions and 0 deletions

View File

@ -18,6 +18,10 @@
<button class='btn btn-primary' {{bind-attr disabled="disabled"}} {{action saveCategory}}>{{buttonTitle}}</button>
{{#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>
{{else}}
<div class="cannot_delete_reason">
{{{cannot_delete_reason}}}
</div>
{{/if}}
</div>
</div>

View File

@ -127,6 +127,13 @@ animation: modal .25s;
}
}
}
.cannot_delete_reason {
float: right;
text-align: right;
max-width: 380px;
color: scale-color($primary, $lightness: 60%);
}
}
.topic-bulk-actions-modal {

View File

@ -8,6 +8,7 @@ class CategorySerializer < BasicCategorySerializer
:email_in,
:email_in_allow_strangers,
:can_delete,
:cannot_delete_reason,
:allow_badges
def group_permissions
@ -38,6 +39,14 @@ class CategorySerializer < BasicCategorySerializer
scope && scope.can_delete?(object)
end
def cannot_delete_reason
scope && scope.cannot_delete_category_reason(object)
end
def include_cannot_delete_reason
!include_can_delete? && scope && scope.can_edit?(object)
end
def include_email_in?
scope && scope.can_edit?(object)
end

View File

@ -268,6 +268,13 @@ en:
uncategorized_parent: "Uncategorized can't have a parent category"
self_parent: "A subcategory's parent cannot be itself"
depth: "You can't nest a subcategory under another"
cannot_delete:
uncategorized: "Can't delete Uncategorized"
has_subcategories: "Can't delete this category because it has sub-categories."
topic_exists:
one: "Can't delete this category because is has 1 topic. Oldest topic is %{topic_link}."
other: "Can't delete this category because it has %{count} topics. Oldest topic is %{topic_link}."
topic_exists_no_oldest: "Can't delete this category because topic count is #{category.topic_count}."
trust_levels:
newuser:
title: "new user"

View File

@ -27,6 +27,23 @@ module CategoryGuardian
!category.has_children?
end
def cannot_delete_category_reason(category)
return I18n.t('category.cannot_delete.uncategorized') if category.uncategorized?
return I18n.t('category.cannot_delete.has_subcategories') if category.has_children?
if category.topic_count != 0
oldest_topic = category.topics.where.not(id: category.topic_id).order('created_at ASC').limit(1).first
if oldest_topic
return I18n.t('category.cannot_delete.topic_exists', {count: category.topic_count, topic_link: "<a href=\"#{oldest_topic.url}\">#{oldest_topic.title}</a>"})
else
# This is a weird case, probably indicating a bug.
return I18n.t('category.cannot_delete.topic_exists_no_oldest', {count: category.topic_count})
end
end
nil
end
def can_see_category?(category)
not(category.read_restricted) || secure_category_ids.include?(category.id)
end