Class: Label

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Referable, Subscribable
Defined in:
app/models/label.rb

Overview

Schema Information

Table name: labels

id          :integer          not null, primary key
title       :string
color       :string
project_id  :integer
created_at  :datetime
updated_at  :datetime
template    :boolean          default(FALSE)
description :string

Defined Under Namespace

Classes: LabelStruct

Constant Summary collapse

None =
LabelStruct.new('No Label', 'No Label')
Any =
LabelStruct.new('Any Label', '')
DEFAULT_COLOR =
'#428BCA'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Subscribable

#subscribed?, #subscribed_without_subscriptions?, #subscribers, #toggle_subscription, #unsubscribe

Methods included from Referable

#reference_link_text

Class Method Details


72
73
74
# File 'app/models/label.rb', line 72

def self.link_reference_pattern
  nil
end

.reference_patternObject

Pattern used to extract label references from text

This pattern supports cross-project references.


58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/label.rb', line 58

def self.reference_pattern
  @reference_pattern ||= %r{
    (#{Project.reference_pattern})?
    #{Regexp.escape(reference_prefix)}
    (?:
      (?<label_id>\d+) | # Integer-based label ID, or
      (?<label_name>
        [A-Za-z0-9_-]+ | # String-based single-word label title, or
        "[^&\?,]+"       # String-based multi-word label surrounded in quotes
      )
    )
  }x
end

.reference_prefixObject


49
50
51
# File 'app/models/label.rb', line 49

def self.reference_prefix
  '~'
end

Instance Method Details

#closed_issues_count(user = nil) ⇒ Object


104
105
106
# File 'app/models/label.rb', line 104

def closed_issues_count(user = nil)
  issues.visible_to_user(user).closed.count
end

#open_issues_count(user = nil) ⇒ Object


100
101
102
# File 'app/models/label.rb', line 100

def open_issues_count(user = nil)
  issues.visible_to_user(user).opened.count
end

#open_merge_requests_countObject


108
109
110
# File 'app/models/label.rb', line 108

def open_merge_requests_count
  merge_requests.opened.count
end

#template?Boolean


112
113
114
# File 'app/models/label.rb', line 112

def template?
  template
end

#text_colorObject


116
117
118
# File 'app/models/label.rb', line 116

def text_color
  LabelsHelper::text_color_for_bg(self.color)
end

#to_reference(from_project = nil, format: :id) ⇒ 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)       # => "gitlab-org/gitlab-ce~1"

Returns a String


89
90
91
92
93
94
95
96
97
98
# File 'app/models/label.rb', line 89

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

  if cross_project_reference?(from_project)
    project.to_reference + reference
  else
    reference
  end
end