2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class LabelLink < ApplicationRecord
|
2020-02-12 16:08:48 -05:00
|
|
|
include BulkInsertSafe
|
2016-07-22 06:10:45 -04:00
|
|
|
include Importable
|
|
|
|
|
2018-09-06 13:02:51 -04:00
|
|
|
belongs_to :target, polymorphic: true, inverse_of: :label_links # rubocop:disable Cop/PolymorphicAssociations
|
2014-07-29 10:10:15 -04:00
|
|
|
belongs_to :label
|
|
|
|
|
2016-07-22 06:10:45 -04:00
|
|
|
validates :target, presence: true, unless: :importing?
|
|
|
|
validates :label, presence: true, unless: :importing?
|
2021-05-12 08:10:24 -04:00
|
|
|
|
|
|
|
scope :for_target, -> (target_id, target_type) { where(target_id: target_id, target_type: target_type) }
|
2021-11-16 07:10:23 -05:00
|
|
|
|
|
|
|
# Example: Issues has at least one label within a project
|
|
|
|
# > Issue.where(project_id: 100) # or any scope on issues
|
|
|
|
# > .where(LabelLink.by_target_for_exists_query('Issue', Issue.arel_table[:id]).arel.exists)
|
|
|
|
scope :by_target_for_exists_query, -> (target_type, arel_join_column, label_ids = nil) do
|
|
|
|
relation = LabelLink
|
|
|
|
.where(target_type: target_type)
|
|
|
|
.where(arel_table['target_id'].eq(arel_join_column))
|
|
|
|
|
|
|
|
relation = relation.where(label_id: label_ids) if label_ids
|
|
|
|
relation
|
|
|
|
end
|
2014-07-29 10:10:15 -04:00
|
|
|
end
|