debian-mirror-gitlab/app/models/label.rb

145 lines
3.6 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class Label < ActiveRecord::Base
2015-09-11 14:41:01 +05:30
include Referable
2016-06-02 11:05:42 +05:30
include Subscribable
2015-10-24 18:46:33 +05:30
# 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')
2015-12-23 02:04:40 +05:30
Any = LabelStruct.new('Any Label', '')
2015-09-11 14:41:01 +05:30
2014-09-02 18:07:02 +05:30
DEFAULT_COLOR = '#428BCA'
2015-04-26 12:48:37 +05:30
default_value_for :color, DEFAULT_COLOR
2014-09-02 18:07:02 +05:30
belongs_to :project
2016-09-13 17:45:13 +05:30
has_many :lists, dependent: :destroy
2014-09-02 18:07:02 +05:30
has_many :label_links, dependent: :destroy
has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
2016-06-02 11:05:42 +05:30
has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest'
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
validates :color, color: true, allow_blank: false
2015-09-25 12:07:36 +05:30
validates :project, presence: true, unless: Proc.new { |service| service.template? }
2014-09-02 18:07:02 +05:30
2016-08-24 12:49:21 +05:30
# Don't allow ',' for label titles
2014-09-02 18:07:02 +05:30
validates :title,
presence: true,
2016-08-24 12:49:21 +05:30
format: { with: /\A[^,]+\z/ },
2014-09-02 18:07:02 +05:30
uniqueness: { scope: :project_id }
before_save :nullify_priority
2015-04-26 12:48:37 +05:30
default_scope { order(title: :asc) }
2014-09-02 18:07:02 +05:30
2015-09-25 12:07:36 +05:30
scope :templates, -> { where(template: true) }
def self.prioritized
where.not(priority: nil).reorder(:priority, :title)
end
def self.unprioritized
where(priority: nil)
end
2014-09-02 18:07:02 +05:30
alias_attribute :name, :title
2015-09-11 14:41:01 +05:30
def self.reference_prefix
'~'
end
2016-06-02 11:05:42 +05:30
##
2015-09-11 14:41:01 +05:30
# Pattern used to extract label references from text
2016-06-02 11:05:42 +05:30
#
# This pattern supports cross-project references.
#
2015-09-11 14:41:01 +05:30
def self.reference_pattern
2016-08-24 12:49:21 +05:30
# 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.
2016-06-02 11:05:42 +05:30
@reference_pattern ||= %r{
(#{Project.reference_pattern})?
#{Regexp.escape(reference_prefix)}
2015-09-11 14:41:01 +05:30
(?:
2016-08-24 12:49:21 +05:30
(?<label_id>\d+(?!\S\w)\b) | # Integer-based label ID, or
2015-09-11 14:41:01 +05:30
(?<label_name>
2016-08-24 12:49:21 +05:30
[A-Za-z0-9_\-\?\.&]+ | # String-based single-word label title, or
".+?" # String-based multi-word label surrounded in quotes
2015-09-11 14:41:01 +05:30
)
)
}x
end
2016-06-02 11:05:42 +05:30
def self.link_reference_pattern
nil
end
##
2015-09-11 14:41:01 +05:30
# Returns the String necessary to reference this Label in Markdown
#
# format - Symbol format to use (default: :id, optional: :name)
#
# Examples:
#
2016-06-02 11:05:42 +05:30
# Label.first.to_reference # => "~1"
# Label.first.to_reference(format: :name) # => "~\"bug\""
# Label.first.to_reference(project) # => "gitlab-org/gitlab-ce~1"
2015-09-11 14:41:01 +05:30
#
# Returns a String
2016-06-02 11:05:42 +05:30
#
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
2015-09-11 14:41:01 +05:30
else
2016-06-02 11:05:42 +05:30
reference
2015-09-11 14:41:01 +05:30
end
end
2016-06-02 11:05:42 +05:30
def open_issues_count(user = nil)
issues.visible_to_user(user).opened.count
end
def closed_issues_count(user = nil)
issues.visible_to_user(user).closed.count
2014-09-02 18:07:02 +05:30
end
2015-09-25 12:07:36 +05:30
2016-06-02 11:05:42 +05:30
def open_merge_requests_count
merge_requests.opened.count
2016-04-02 18:10:28 +05:30
end
2015-09-25 12:07:36 +05:30
def template?
template
end
2016-06-02 11:05:42 +05:30
def text_color
LabelsHelper::text_color_for_bg(self.color)
end
def title=(value)
2016-08-24 12:49:21 +05:30
write_attribute(:title, sanitize_title(value)) if value.present?
2016-06-02 11:05:42 +05:30
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
def nullify_priority
self.priority = nil if priority.blank?
end
2016-08-24 12:49:21 +05:30
def sanitize_title(value)
CGI.unescapeHTML(Sanitize.clean(value.to_s))
end
2014-09-02 18:07:02 +05:30
end