Class: Label
Constant Summary
collapse
- DEFAULT_COLOR =
::Gitlab::Color.of('#6699cc')
Gitlab::SQL::Pattern::MIN_CHARS_FOR_PARTIAL_MATCHING, Gitlab::SQL::Pattern::REGEX_QUOTED_WORD
CacheMarkdownField::INVALIDATED_BY
ApplicationRecord::MAX_PLUCK
Class Method Summary
collapse
Instance Method Summary
collapse
#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
#attribute_invalidated?, #banzai_render_context, #cached_html_for, #cached_html_up_to_date?, #can_cache_field?, #invalidated_markdown_cache?, #latest_cached_markdown_version, #local_version, #mentionable_attributes_changed?, #parent_user, #refresh_markdown_cache, #refresh_markdown_cache!, #rendered_field_content, #skip_project_check?, #store_mentions!, #updated_cached_html_for
cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, 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
#serializable_hash
Class Method Details
.color_for(value) ⇒ Object
Generate a hex color based on hex-encoded value
164
165
166
|
# File 'app/models/label.rb', line 164
def self.color_for(value)
"##{Digest::MD5.hexdigest(value)[0..5]}"
end
|
.ids_on_board(board_id) ⇒ Object
135
136
137
|
# File 'app/models/label.rb', line 135
def self.ids_on_board(board_id)
on_board(board_id).pluck(:label_id)
end
|
.left_join_priorities ⇒ Object
79
80
81
82
83
84
85
86
87
88
|
# File 'app/models/label.rb', line 79
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
|
.link_reference_pattern ⇒ Object
131
132
133
|
# File 'app/models/label.rb', line 131
def self.link_reference_pattern
nil
end
|
.min_chars_for_partial_matching ⇒ Object
Override Gitlab::SQL::Pattern.min_chars_for_partial_matching as label queries are never global, and so will not use a trigram index. That means we can have just one character in the LIKE.
153
154
155
|
# File 'app/models/label.rb', line 153
def self.min_chars_for_partial_matching
1
end
|
.on_project_board?(project_id, label_id) ⇒ Boolean
157
158
159
160
161
|
# File 'app/models/label.rb', line 157
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
90
91
92
93
94
95
96
|
# File 'app/models/label.rb', line 90
def self.optionally_subscribed_by(user_id)
if user_id
subscribed_by(user_id)
else
all
end
end
|
.prioritized(project) ⇒ Object
62
63
64
65
66
|
# File 'app/models/label.rb', line 62
def self.prioritized(project)
joins(:priorities)
.where(label_priorities: { project_id: project })
.reorder('label_priorities.priority ASC, labels.title ASC')
end
|
.reference_pattern ⇒ Object
Pattern used to extract label references from text
This pattern supports cross-project references.
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'app/models/label.rb', line 109
def self.reference_pattern
@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
[A-Za-z0-9_\-\?\.&]+
(?<!\.|\?)
|
# String-based multi-word label surrounded in quotes
".+?"
)
)
}x
end
|
.reference_prefix ⇒ Object
100
101
102
|
# File 'app/models/label.rb', line 100
def self.reference_prefix
'~'
end
|
.search(query, **options) ⇒ Object
Searches for labels with a matching title or description.
This method uses ILIKE on PostgreSQL.
query - The search query as a String.
Returns an ActiveRecord::Relation.
146
147
148
|
# File 'app/models/label.rb', line 146
def self.search(query, **options)
fuzzy_search(query, [:title, :description])
end
|
.unprioritized(project) ⇒ Object
68
69
70
71
72
73
74
75
76
77
|
# File 'app/models/label.rb', line 68
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
260
261
262
263
264
265
266
|
# File 'app/models/label.rb', line 260
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
172
173
174
|
# File 'app/models/label.rb', line 172
def closed_issues_count(user = nil)
issues_count(user, state: 'closed')
end
|
#color ⇒ Object
211
212
213
|
# File 'app/models/label.rb', line 211
def color
super || DEFAULT_COLOR
end
|
#description=(value) ⇒ Object
227
228
229
230
231
232
233
|
# File 'app/models/label.rb', line 227
def description=(value)
if value.blank?
super
else
write_attribute(:description, sanitize_value(value))
end
end
|
#hook_attrs ⇒ Object
268
269
270
|
# File 'app/models/label.rb', line 268
def hook_attrs
attributes
end
|
#open_issues_count(user = nil) ⇒ Object
168
169
170
|
# File 'app/models/label.rb', line 168
def open_issues_count(user = nil)
issues_count(user, state: 'opened')
end
|
#open_merge_requests_count(user = nil) ⇒ Object
176
177
178
179
180
181
182
183
184
185
|
# File 'app/models/label.rb', line 176
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
272
273
274
|
# File 'app/models/label.rb', line 272
def present(attributes = {})
super(**attributes.merge(presenter_class: ::LabelPresenter))
end
|
#prioritize!(project, value) ⇒ Object
187
188
189
190
191
|
# File 'app/models/label.rb', line 187
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
197
198
199
200
201
202
203
204
205
|
# File 'app/models/label.rb', line 197
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
207
208
209
|
# File 'app/models/label.rb', line 207
def priority?
priorities.present?
end
|
#text_color ⇒ Object
215
216
217
|
# File 'app/models/label.rb', line 215
def text_color
color.contrast
end
|
#title=(value) ⇒ Object
219
220
221
222
223
224
225
|
# File 'app/models/label.rb', line 219
def title=(value)
if value.blank?
super
else
write_attribute(:title, sanitize_value(value))
end
end
|
#to_reference(from = nil, target_project: 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 Label.first.to_reference(format: :name) Label.first.to_reference(project, target_project: same_namespace_project) Label.first.to_reference(project, target_project: another_namespace_project)
Returns a String
249
250
251
252
253
254
255
256
257
258
|
# File 'app/models/label.rb', line 249
def to_reference(from = nil, target_project: 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_project, full: full)}#{reference}"
else
reference
end
end
|
#unprioritize!(project) ⇒ Object
193
194
195
|
# File 'app/models/label.rb', line 193
def unprioritize!(project)
priorities.where(project: project).delete_all
end
|