Merge pull request #4387 from gdpelican/feature/tags-intersection

FEATURE: Tags intersection page
This commit is contained in:
Neil Lalonde
2016-08-15 16:24:29 -04:00
committed by GitHub
9 changed files with 92 additions and 11 deletions

View File

@@ -20,6 +20,7 @@ class TopicQuery
visible
category
tags
match_all_tags
no_tags
order
ascending
@@ -460,11 +461,27 @@ class TopicQuery
if @options[:tags] && @options[:tags].size > 0
result = result.joins(:tags)
# ANY of the given tags:
if @options[:tags][0].is_a?(Integer)
result = result.where("tags.id in (?)", @options[:tags])
if @options[:match_all_tags]
# ALL of the given tags:
tags_count = @options[:tags].length
@options[:tags] = Tag.where(name: @options[:tags]).pluck(:id) unless @options[:tags][0].is_a?(Integer)
if tags_count == @options[:tags].length
@options[:tags].each_with_index do |tag, index|
sql_alias = ['t', index].join
result = result.joins("INNER JOIN topic_tags #{sql_alias} ON #{sql_alias}.topic_id = topics.id AND #{sql_alias}.tag_id = #{tag}")
end
else
result = result.none # don't return any results unless all tags exist in the database
end
else
result = result.where("tags.name in (?)", @options[:tags])
# ANY of the given tags:
if @options[:tags][0].is_a?(Integer)
result = result.where("tags.id in (?)", @options[:tags])
else
result = result.where("tags.name in (?)", @options[:tags])
end
end
elsif @options[:no_tags]
# the following will do: ("topics"."id" NOT IN (SELECT DISTINCT "topic_tags"."topic_id" FROM "topic_tags"))