1# frozen_string_literal: true 2 3module TaggableQueries 4 extend ActiveSupport::Concern 5 6 MAX_TAGS_IDS = 50 7 8 TooManyTagsError = Class.new(StandardError) 9 10 class_methods do 11 # context is a name `acts_as_taggable context` 12 def arel_tag_names_array(context = :tags) 13 ActsAsTaggableOn::Tagging 14 .joins(:tag) 15 .where("taggings.taggable_id=#{quoted_table_name}.id") # rubocop:disable GitlabSecurity/SqlInjection 16 .where(taggings: { context: context, taggable_type: polymorphic_name }) 17 .select('COALESCE(array_agg(tags.name ORDER BY name), ARRAY[]::text[])') 18 end 19 20 def matches_tag_ids(tag_ids, table: quoted_table_name, column: 'id') 21 matcher = ::ActsAsTaggableOn::Tagging 22 .where(taggable_type: CommitStatus.name) 23 .where(context: 'tags') 24 .where("taggable_id = #{connection.quote_table_name(table)}.#{connection.quote_column_name(column)}") # rubocop:disable GitlabSecurity/SqlInjection 25 .where.not(tag_id: tag_ids) 26 .select('1') 27 28 where("NOT EXISTS (?)", matcher) 29 end 30 31 def with_any_tags(table: quoted_table_name, column: 'id') 32 matcher = ::ActsAsTaggableOn::Tagging 33 .where(taggable_type: CommitStatus.name) 34 .where(context: 'tags') 35 .where("taggable_id = #{connection.quote_table_name(table)}.#{connection.quote_column_name(column)}") # rubocop:disable GitlabSecurity/SqlInjection 36 .select('1') 37 38 where("EXISTS (?)", matcher) 39 end 40 end 41 42 def tags_ids 43 tags.limit(MAX_TAGS_IDS).order('id ASC').pluck(:id).tap do |ids| 44 raise TooManyTagsError if ids.size >= MAX_TAGS_IDS 45 end 46 end 47end 48