gitlab-org--gitlab-foss/app/models/label.rb

143 lines
3.6 KiB
Ruby
Raw Normal View History

class Label < ActiveRecord::Base
include Referable
include Subscribable
# Represents a "No Label" state used for filtering Issues and Merge
# Requests that have no label assigned.
LabelStruct = Struct.new(:title, :name)
None = LabelStruct.new('No Label', 'No Label')
Any = LabelStruct.new('Any Label', '')
DEFAULT_COLOR = '#428BCA'
2015-04-17 11:38:31 -04:00
default_value_for :color, DEFAULT_COLOR
belongs_to :project
has_many :label_links, dependent: :destroy
has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
2016-03-01 21:04:13 -05:00
has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest'
2015-12-01 18:53:44 -05:00
validates :color, color: true, allow_blank: false
2015-09-03 08:50:23 -04:00
validates :project, presence: true, unless: Proc.new { |service| service.template? }
2016-06-16 19:09:13 -04:00
# Don't allow ',' for label titles
2014-08-12 04:53:50 -04:00
validates :title,
presence: true,
2016-06-16 19:09:13 -04:00
format: { with: /\A[^,]+\z/ },
2014-08-13 08:12:05 -04:00
uniqueness: { scope: :project_id }
2016-06-02 16:21:00 -04:00
before_save :nullify_priority
default_scope { order(title: :asc) }
2015-09-03 08:50:23 -04:00
scope :templates, -> { where(template: true) }
2016-06-02 15:57:31 -04:00
def self.prioritized
where.not(priority: nil).reorder(:priority, :title)
2016-06-02 15:57:31 -04:00
end
2016-06-02 15:57:31 -04:00
def self.unprioritized
where(priority: nil)
end
2014-08-12 08:16:25 -04:00
alias_attribute :name, :title
def self.reference_prefix
'~'
end
##
# Pattern used to extract label references from text
#
# This pattern supports cross-project references.
#
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>
[A-Za-z0-9_\-\?\.&]+ | # String-based single-word label title, or
".+?" # String-based multi-word label surrounded in quotes
)
)
}x
end
def self.link_reference_pattern
nil
end
##
# 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
#
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
2016-03-21 19:21:23 -04:00
def open_issues_count(user = nil)
issues.visible_to_user(user).opened.count
end
2015-09-03 08:50:23 -04:00
2016-03-21 19:21:23 -04:00
def closed_issues_count(user = nil)
issues.visible_to_user(user).closed.count
end
2016-03-01 21:04:34 -05:00
def open_merge_requests_count
merge_requests.opened.count
end
2015-09-03 08:50:23 -04:00
def template?
template
end
def text_color
LabelsHelper::text_color_for_bg(self.color)
end
def title=(value)
2016-06-16 19:09:13 -04:00
write_attribute(:title, sanitize_title(value)) if value.present?
2016-05-04 17:21:57 -04:00
end
private
def label_format_reference(format = :id)
raise StandardError, 'Unknown format' unless [:id, :name].include?(format)
if format == :name && !name.include?('"')
%("#{name}")
else
id
end
end
2016-06-02 16:21:00 -04:00
def nullify_priority
self.priority = nil if priority.blank?
end
2016-06-16 19:09:13 -04:00
def sanitize_title(value)
CGI.unescapeHTML(Sanitize.clean(value.to_s))
2016-06-16 19:09:13 -04:00
end
end