From b8fd5aa8e20d9ed3eec0d01886e4504a8d2a6437 Mon Sep 17 00:00:00 2001 From: Justin DiRose Date: Tue, 2 Jul 2019 16:46:18 -0500 Subject: [PATCH] =?UTF-8?q?Return=20tag=20count=20I'm=20probably=20doing?= =?UTF-8?q?=20this=20wrong=20=F0=9F=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../knowledge_explorer_controller.rb | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/app/controllers/knowledge_explorer/knowledge_explorer_controller.rb b/app/controllers/knowledge_explorer/knowledge_explorer_controller.rb index b4d3037..6f2d57a 100644 --- a/app/controllers/knowledge_explorer/knowledge_explorer_controller.rb +++ b/app/controllers/knowledge_explorer/knowledge_explorer_controller.rb @@ -23,7 +23,7 @@ module KnowledgeExplorer category_topic_lists.each do |list| list[:topic_list][:topics].each do |t| - if !topics.any?{|item| item[:id] == t[:id]} + if topics.none?{|item| item[:id] == t[:id]} if t[:id] != Category.find(t[:category_id]).topic_id topics << t end @@ -32,15 +32,34 @@ module KnowledgeExplorer end tag_topic_lists.each do |list| list[:topic_list][:topics].each do |t| - if !topics.any?{|item| item[:id] == t[:id]} + if topics.none?{|item| item[:id] == t[:id]} topics << t end end end + topics = count_tags(topics) + render json: topics end + def count_tags(topics) + tags = [] + + topics.each do |topic| + topic[:tags].each do |tag| + if tags.none? { |item| item[:id].to_s == tag } + tags << { id: tag, count: 1 } + else + tag_index = tags.index(tags.find { |item| item[:id].to_s == tag }) + tags[tag_index][:count] += 1 + end + end + end + + { tags: tags, topics: topics } + end + private def init_guardian @@ -58,7 +77,7 @@ module KnowledgeExplorer def knowledge_explorer_tags selected_tags = SiteSetting.knowledge_explorer_tags.split("|") - tags = Tag.where('name IN (?)', selected_tags) + Tag.where('name IN (?)', selected_tags) end end end