From b510006ca827b0a7aafe92de6ae8e5af27409376 Mon Sep 17 00:00:00 2001 From: Saurabh Patel Date: Thu, 6 Jun 2019 08:25:37 +0530 Subject: [PATCH] FEATURE: show tags in crawler view of tags page for static site Previously tags page would have an empty page in crawler view --- app/controllers/tags_controller.rb | 63 +++++++++++++++--------------- app/views/tags/_tag.html.erb | 6 +++ app/views/tags/index.html.erb | 42 ++++++++++++++++++++ 3 files changed, 80 insertions(+), 31 deletions(-) create mode 100644 app/views/tags/_tag.html.erb diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index bd381bd1ae9..b45e5e2f801 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -27,6 +27,34 @@ class TagsController < ::ApplicationController @description_meta = I18n.t("tags.title") @title = @description_meta + show_all_tags = guardian.can_admin_tags? && guardian.is_admin? + + if SiteSetting.tags_listed_by_group + ungrouped_tags = Tag.where("tags.id NOT IN (SELECT tag_id FROM tag_group_memberships)") + ungrouped_tags = ungrouped_tags.where("tags.topic_count > 0") unless show_all_tags + + grouped_tag_counts = TagGroup.visible(guardian).order('name ASC').includes(:tags).map do |tag_group| + { id: tag_group.id, name: tag_group.name, tags: self.class.tag_counts_json(tag_group.tags) } + end + + @tags = self.class.tag_counts_json(ungrouped_tags) + @extras = { tag_groups: grouped_tag_counts } + else + tags = show_all_tags ? Tag.all : Tag.where("tags.topic_count > 0") + unrestricted_tags = DiscourseTagging.filter_visible(tags, guardian) + + categories = Category.where("id IN (SELECT category_id FROM category_tags)") + .where("id IN (?)", guardian.allowed_category_ids) + .includes(:tags) + + category_tag_counts = categories.map do |c| + { id: c.id, tags: self.class.tag_counts_json(c.tags) } + end + + @tags = self.class.tag_counts_json(unrestricted_tags) + @extras = { categories: category_tag_counts } + end + respond_to do |format| format.html do @@ -34,37 +62,10 @@ class TagsController < ::ApplicationController end format.json do - show_all_tags = guardian.can_admin_tags? && guardian.is_admin? - - if SiteSetting.tags_listed_by_group - ungrouped_tags = Tag.where("tags.id NOT IN (SELECT tag_id FROM tag_group_memberships)") - ungrouped_tags = ungrouped_tags.where("tags.topic_count > 0") unless show_all_tags - - grouped_tag_counts = TagGroup.visible(guardian).order('name ASC').includes(:tags).map do |tag_group| - { id: tag_group.id, name: tag_group.name, tags: self.class.tag_counts_json(tag_group.tags) } - end - - render json: { - tags: self.class.tag_counts_json(ungrouped_tags), - extras: { tag_groups: grouped_tag_counts } - } - else - tags = show_all_tags ? Tag.all : Tag.where("tags.topic_count > 0") - unrestricted_tags = DiscourseTagging.filter_visible(tags, guardian) - - categories = Category.where("id IN (SELECT category_id FROM category_tags)") - .where("id IN (?)", guardian.allowed_category_ids) - .includes(:tags) - - category_tag_counts = categories.map do |c| - { id: c.id, tags: self.class.tag_counts_json(c.tags) } - end - - render json: { - tags: self.class.tag_counts_json(unrestricted_tags), - extras: { categories: category_tag_counts } - } - end + render json: { + tags: @tags, + extras: @extras + } end end end diff --git a/app/views/tags/_tag.html.erb b/app/views/tags/_tag.html.erb new file mode 100644 index 00000000000..b8258f9a125 --- /dev/null +++ b/app/views/tags/_tag.html.erb @@ -0,0 +1,6 @@ +
+ <%= tag[:text] %> + <% if tag[:count] && tag[:count] > 0 %> + x <%= tag[:count] %> + <% end %> +
diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb index cc250d46849..823b146de06 100644 --- a/app/views/tags/index.html.erb +++ b/app/views/tags/index.html.erb @@ -1,3 +1,45 @@ <% content_for :head do %> <%= raw crawlable_meta_data(title: @title, description: @description_meta) %> <% end %> + +
+ + <% if @extras[:categories] %> + <% @extras[:categories].each do |category| %> +
+ <% if category[:name] %> +

<%= category[:name] %>

+ <% end %> + <% category[:tags].each do |tag| %> + <%= render "tag", tag: tag %> + <% end %> +
+
+ <% end %> + <% end %> + + <% if @extras[:tag_groups] %> + <% @extras[:tag_groups].each do |tag_group| %> +
+ <% if tag_group[:name] %> +

<%= tag_group[:name] %>

+ <% end %> + <% tag_group[:tags].each do |tag| %> + <%= render "tag", tag: tag %> + <% end %> +
+
+ <% end %> + <% end %> + + <% if @tags.present? %> +
+

<%= t 'js.tagging.other_tags' %>

+ <% @tags.each do |tag| %> + <%= render "tag", tag: tag %> + <% end %> +
+
+ <% end %> + +