FIX: Tags used only on deleted topics could not be used again

This commit is contained in:
Neil Lalonde
2016-10-28 15:11:43 -04:00
parent 1e4a56fe14
commit 8c9d390cac
3 changed files with 17 additions and 18 deletions

View File

@@ -12,8 +12,17 @@ class Tag < ActiveRecord::Base
has_many :tag_group_memberships
has_many :tag_groups, through: :tag_group_memberships
COUNT_ARG = "topic_tags.id"
# Apply more activerecord filters to the tags_by_count_query, and then
# fetch the result with .count(Tag::COUNT_ARG).
#
# e.g., Tag.tags_by_count_query.where("topics.category_id = ?", category.id).count(Tag::COUNT_ARG)
def self.tags_by_count_query(opts={})
q = TopicTag.joins(:tag, :topic).group("topic_tags.tag_id, tags.name").order('count_all DESC')
q = Tag.joins("LEFT JOIN topic_tags ON tags.id = topic_tags.tag_id")
.joins("LEFT JOIN topics ON topics.id = topic_tags.topic_id")
.group("tags.id, tags.name")
.order('count_topic_tags_id DESC')
q = q.limit(opts[:limit]) if opts[:limit]
q
end
@@ -37,7 +46,7 @@ class Tag < ActiveRecord::Base
category: category
)
tags.count.map {|name, _| name}
tags.count(COUNT_ARG).map {|name, _| name}
end
def self.include_tags?