UX: Hide "Create Tag" option if user cannot create tag. (#7723)

UX: Hide “Create Tag” option if user cannot create tag. (#7723)

diff --git a/app/assets/javascripts/select-kit/components/mini-tag-chooser.js.es6 b/app/assets/javascripts/select-kit/components/mini-tag-chooser.js.es6
index 5da4bce..6f3baa3 100644
--- a/app/assets/javascripts/select-kit/components/mini-tag-chooser.js.es6
+++ b/app/assets/javascripts/select-kit/components/mini-tag-chooser.js.es6
@@ -1,3 +1,4 @@
+import Category from "discourse/models/category";
 import ComboBox from "select-kit/components/combo-box";
 import TagsMixin from "select-kit/mixins/tags";
 import { default as computed } from "ember-addons/ember-computed-decorators";
@@ -51,6 +52,27 @@ export default ComboBox.extend(TagsMixin, {
     );
   },
 
+  @computed(
+    "computedValue",
+    "filter",
+    "collectionComputedContent.[]",
+    "hasReachedMaximum",
+    "hasReachedMinimum",
+    "categoryId"
+  )
+  shouldDisplayCreateRow() {
+    if (this.categoryId) {
+      const category = Category.findById(this.categoryId);
+      if (
+        (category.allowed_tags && category.allowed_tags.length > 0) ||
+        (category.allowed_tag_groups && category.allowed_tag_groups.length > 0)
+      ) {
+        return category.allow_global_tags && this._super(...arguments);
+      }
+    }
+    return this._super(...arguments);
+  },
+
   didInsertElement() {
     this._super(...arguments);
 
diff --git a/app/models/site.rb b/app/models/site.rb
index 622a6d6..efddc9c 100644
--- a/app/models/site.rb
+++ b/app/models/site.rb
@@ -34,7 +34,7 @@ class Site
   def categories
     @categories ||= begin
       categories = Category
-        .includes(:uploaded_logo, :uploaded_background)
+        .includes(:uploaded_logo, :uploaded_background, :tags, :tag_groups)
         .secured(@guardian)
         .joins('LEFT JOIN topics t on t.id = categories.topic_id')
         .select('categories.*, t.slug topic_slug')
diff --git a/app/serializers/category_serializer.rb b/app/serializers/category_serializer.rb
index aa3e46a..b5bae43 100644
--- a/app/serializers/category_serializer.rb
+++ b/app/serializers/category_serializer.rb
@@ -1,6 +1,6 @@
 # frozen_string_literal: true
 
-class CategorySerializer < BasicCategorySerializer
+class CategorySerializer < SiteCategorySerializer
 
   attributes :read_restricted,
              :available_groups,
@@ -18,9 +18,6 @@ class CategorySerializer < BasicCategorySerializer
              :is_special,
              :allow_badges,
              :custom_fields,
-             :allowed_tags,
-             :allowed_tag_groups,
-             :allow_global_tags,
              :topic_featured_link_allowed,
              :search_priority,
              :reviewable_by_group_name
@@ -95,22 +92,6 @@ class CategorySerializer < BasicCategorySerializer
      (user && CategoryUser.where(user: user, category: object).first.try(:notification_level))
   end
 
-  def include_allowed_tags?
-    SiteSetting.tagging_enabled
-  end
-
-  def allowed_tags
-    object.tags.pluck(:name)
-  end
-
-  def include_allowed_tag_groups?
-    SiteSetting.tagging_enabled
-  end
-
-  def allowed_tag_groups
-    object.tag_groups.pluck(:name)
-  end
-
   def custom_fields
     object.custom_fields
   end
diff --git a/app/serializers/site_category_serializer.rb b/app/serializers/site_category_serializer.rb
new file mode 100644
index 0000000..ed02fa6
--- /dev/null
+++ b/app/serializers/site_category_serializer.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class SiteCategorySerializer < BasicCategorySerializer
+
+  attributes :allowed_tags,
+             :allowed_tag_groups,
+             :allow_global_tags
+
+  def include_allowed_tags?
+    SiteSetting.tagging_enabled
+  end
+
+  def allowed_tags
+    object.tags.pluck(:name)
+  end
+
+  def include_allowed_tag_groups?
+    SiteSetting.tagging_enabled
+  end
+
+  def allowed_tag_groups
+    object.tag_groups.pluck(:name)
+  end
+
+  def include_allow_global_tags?
+    SiteSetting.tagging_enabled
+  end
+
+end
diff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rb
index 13859d7..6e579d7 100644
--- a/app/serializers/site_serializer.rb
+++ b/app/serializers/site_serializer.rb
@@ -34,7 +34,7 @@ class SiteSerializer < ApplicationSerializer
     :shared_drafts_category_id
   )
 
-  has_many :categories, serializer: BasicCategorySerializer, embed: :objects
+  has_many :categories, serializer: SiteCategorySerializer, embed: :objects
   has_many :trust_levels, embed: :objects
   has_many :archetypes, embed: :objects, serializer: ArchetypeSerializer
   has_many :user_fields, embed: :objects, serializer: UserFieldSerializer

GitHub sha: 0b1146ad

1 Like