2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class LabelLink < ApplicationRecord
|
2020-03-13 15:44:24 +05:30
|
|
|
include BulkInsertSafe
|
2016-08-24 12:49:21 +05:30
|
|
|
include Importable
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
belongs_to :target, polymorphic: true, inverse_of: :label_links # rubocop:disable Cop/PolymorphicAssociations
|
2014-09-02 18:07:02 +05:30
|
|
|
belongs_to :label
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
validates :target, presence: true, unless: :importing?
|
|
|
|
validates :label, presence: true, unless: :importing?
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
scope :for_target, -> (target_id, target_type) { where(target_id: target_id, target_type: target_type) }
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
# 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-09-02 18:07:02 +05:30
|
|
|
end
|