Class: Label

Inherits:
ApplicationRecord show all
Includes:
BaseLabel, EachBatch, FromUnion, OptionallySearch, Presentable, Referable, Sortable, Subscribable
Defined in:
app/models/label.rb

Direct Known Subclasses

GroupLabel, ProjectLabel

Constant Summary collapse

DESCRIPTION_LENGTH_MAX =
512.kilobytes

Constants included from BaseLabel

BaseLabel::DEFAULT_COLOR

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Subscribable

#lazy_subscription, #set_subscription, #subscribe, #subscribed?, #subscribed_without_subscriptions?, #subscribers, #toggle_subscription, #unsubscribe

Methods included from Referable

#referable_inspect, #reference_link_text, #to_reference_base

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, nullable_column?, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.ids_on_board(board_id) ⇒ Object



169
170
171
# File 'app/models/label.rb', line 169

def self.ids_on_board(board_id)
  on_board(board_id).pluck(:label_id)
end

.left_join_prioritiesObject



113
114
115
116
117
118
119
120
121
122
# File 'app/models/label.rb', line 113

def self.left_join_priorities
  labels = Label.arel_table
  priorities = LabelPriority.arel_table

  label_priorities = labels.join(priorities, Arel::Nodes::OuterJoin)
                            .on(labels[:id].eq(priorities[:label_id]))
                            .join_sources

  joins(label_priorities)
end


165
166
167
# File 'app/models/label.rb', line 165

def self.link_reference_pattern
  nil
end

.on_project_board?(project_id, label_id) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
176
177
# File 'app/models/label.rb', line 173

def self.on_project_board?(project_id, label_id)
  return false if label_id.blank?

  on_project_boards(project_id).where(id: label_id).exists?
end

.optionally_subscribed_by(user_id) ⇒ Object



124
125
126
127
128
129
130
# File 'app/models/label.rb', line 124

def self.optionally_subscribed_by(user_id)
  if user_id
    subscribed_by(user_id)
  else
    all
  end
end

.pluck_titlesObject



92
93
94
# File 'app/models/label.rb', line 92

def self.pluck_titles
  pluck(:title)
end

.prioritized(project) ⇒ Object



96
97
98
99
100
# File 'app/models/label.rb', line 96

def self.prioritized(project)
  joins(:priorities)
    .where(label_priorities: { project_id: project })
    .reorder('label_priorities.priority ASC, labels.title ASC')
end

.reference_patternObject

Pattern used to extract label references from text

This pattern supports cross-project references.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/models/label.rb', line 143

def self.reference_pattern
  # NOTE: The id pattern only matches when all characters on the expression
  # are digits, so it will match ~2 but not ~2fa because that's probably a
  # label name and we want it to be matched as such.
  @reference_pattern ||= %r{
    (#{Project.reference_pattern})?
    #{Regexp.escape(reference_prefix)}
    (?:
        (?<label_id>\d+(?!\S\w)\b)
      | # Integer-based label ID, or
        (?<label_name>
            # String-based single-word label title, or
            #{Gitlab::Regex.sep_by_1(/:{1,2}/, /[A-Za-z0-9_\-\?\.&]+/)}
            (?<!\.|\?)
          |
            # String-based multi-word label surrounded in quotes
            ".+?"
        )
    )
  }x
end

.reference_prefixObject



134
135
136
# File 'app/models/label.rb', line 134

def self.reference_prefix
  '~'
end

.unprioritized(project) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'app/models/label.rb', line 102

def self.unprioritized(project)
  labels = Label.arel_table
  priorities = LabelPriority.arel_table

  label_priorities = labels.join(priorities, Arel::Nodes::OuterJoin)
                            .on(labels[:id].eq(priorities[:label_id]).and(priorities[:project_id].eq(project.id)))
                            .join_sources

  joins(label_priorities).where(priorities[:priority].eq(nil))
end

Instance Method Details

#as_json(options = {}) ⇒ Object



247
248
249
250
251
252
253
# File 'app/models/label.rb', line 247

def as_json(options = {})
  super(options).tap do |json|
    json[:type] = self.try(:type)
    json[:priority] = priority(options[:project]) if options.key?(:project)
    json[:textColor] = text_color
  end
end

#closed_issues_count(user = nil) ⇒ Object



183
184
185
# File 'app/models/label.rb', line 183

def closed_issues_count(user = nil)
  issues_count(user, state: 'closed')
end

#hook_attrsObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'app/models/label.rb', line 255

def hook_attrs
  {
    id: id,
    title: title,
    color: color,
    project_id: project_id,
    created_at: created_at,
    updated_at: updated_at,
    template: template,
    description: description,
    type: type,
    group_id: group_id
  }
end

#open_issues_count(user = nil) ⇒ Object



179
180
181
# File 'app/models/label.rb', line 179

def open_issues_count(user = nil)
  issues_count(user, state: 'opened')
end

#open_merge_requests_count(user = nil) ⇒ Object



187
188
189
190
191
192
193
194
195
196
# File 'app/models/label.rb', line 187

def open_merge_requests_count(user = nil)
  params = {
    subject_foreign_key => subject.id,
    label_name: title,
    scope: 'all',
    state: 'opened'
  }

  MergeRequestsFinder.new(user, params.with_indifferent_access).execute.count
end

#present(attributes = {}) ⇒ Object



270
271
272
# File 'app/models/label.rb', line 270

def present(attributes = {})
  super(**attributes.merge(presenter_class: ::LabelPresenter))
end

#prioritize!(project, value) ⇒ Object



198
199
200
201
202
# File 'app/models/label.rb', line 198

def prioritize!(project, value)
  label_priority = priorities.find_or_initialize_by(project_id: project.id)
  label_priority.priority = value
  label_priority.save!
end

#priority(project) ⇒ Object



208
209
210
211
212
213
214
215
216
# File 'app/models/label.rb', line 208

def priority(project)
  priority = if priorities.loaded?
               priorities.first { |p| p.project == project }
             else
               priorities.find_by(project: project)
             end

  priority.try(:priority)
end

#priority?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'app/models/label.rb', line 218

def priority?
  priorities.present?
end

#to_reference(from = nil, target_container: nil, format: :id, full: false) ⇒ Object

Returns the String necessary to reference this Label in Markdown

format - Symbol format to use (default: :id, optional: :name)

Examples:

Label.first.to_reference                                     # => "~1"
Label.first.to_reference(format: :name)                      # => "~\"bug\""
Label.first.to_reference(project, target_container: same_namespace_project)    # => "gitlab-foss~1"
Label.first.to_reference(project, target_container: another_namespace_project) # => "gitlab-org/gitlab-foss~1"

Returns a String



236
237
238
239
240
241
242
243
244
245
# File 'app/models/label.rb', line 236

def to_reference(from = nil, target_container: nil, format: :id, full: false)
  format_reference = label_format_reference(format)
  reference = "#{self.class.reference_prefix}#{format_reference}"

  if from
    "#{from.to_reference_base(target_container, full: full)}#{reference}"
  else
    reference
  end
end

#unprioritize!(project) ⇒ Object



204
205
206
# File 'app/models/label.rb', line 204

def unprioritize!(project)
  priorities.where(project: project).delete_all
end