2014-08-25 09:25:02 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: labels
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# title :string(255)
|
|
|
|
# color :string(255)
|
|
|
|
# project_id :integer
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
#
|
|
|
|
|
2014-07-29 14:10:15 +00:00
|
|
|
class Label < ActiveRecord::Base
|
2015-05-03 03:11:21 +00:00
|
|
|
include Referable
|
2015-10-06 16:57:13 +00:00
|
|
|
# Represents a "No Label" state used for filtering Issues and Merge
|
|
|
|
# Requests that have no label assigned.
|
2015-10-09 04:24:55 +00:00
|
|
|
LabelStruct = Struct.new(:title, :name)
|
|
|
|
None = LabelStruct.new('No Label', 'No Label')
|
|
|
|
Any = LabelStruct.new('Any', '')
|
2015-05-03 03:11:21 +00:00
|
|
|
|
2014-08-15 09:02:05 +00:00
|
|
|
DEFAULT_COLOR = '#428BCA'
|
2014-08-14 08:17:52 +00:00
|
|
|
|
2015-04-17 15:38:31 +00:00
|
|
|
default_value_for :color, DEFAULT_COLOR
|
|
|
|
|
2014-07-29 14:10:15 +00:00
|
|
|
belongs_to :project
|
|
|
|
has_many :label_links, dependent: :destroy
|
2014-07-30 12:15:39 +00:00
|
|
|
has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
|
2014-07-29 14:10:15 +00:00
|
|
|
|
2014-08-11 21:59:30 +00:00
|
|
|
validates :color,
|
2014-08-17 20:22:01 +00:00
|
|
|
format: { with: /\A#[0-9A-Fa-f]{6}\Z/ },
|
2014-08-11 21:59:30 +00:00
|
|
|
allow_blank: false
|
2015-09-03 12:50:23 +00:00
|
|
|
validates :project, presence: true, unless: Proc.new { |service| service.template? }
|
2014-07-30 10:26:54 +00:00
|
|
|
|
2014-08-11 21:59:30 +00:00
|
|
|
# Don't allow '?', '&', and ',' for label titles
|
2014-08-12 08:53:50 +00:00
|
|
|
validates :title,
|
|
|
|
presence: true,
|
2015-04-10 20:14:00 +00:00
|
|
|
format: { with: /\A[^&\?,]+\z/ },
|
2014-08-13 12:12:05 +00:00
|
|
|
uniqueness: { scope: :project_id }
|
2014-07-29 16:19:26 +00:00
|
|
|
|
2015-02-06 04:29:41 +00:00
|
|
|
default_scope { order(title: :asc) }
|
2014-07-30 16:13:35 +00:00
|
|
|
|
2015-09-03 12:50:23 +00:00
|
|
|
scope :templates, -> { where(template: true) }
|
|
|
|
|
2014-08-12 12:16:25 +00:00
|
|
|
alias_attribute :name, :title
|
2014-07-30 12:15:39 +00:00
|
|
|
|
2015-05-03 03:11:21 +00:00
|
|
|
def self.reference_prefix
|
|
|
|
'~'
|
|
|
|
end
|
|
|
|
|
2015-05-14 20:59:39 +00:00
|
|
|
# Pattern used to extract label references from text
|
|
|
|
def self.reference_pattern
|
|
|
|
%r{
|
|
|
|
#{reference_prefix}
|
|
|
|
(?:
|
2015-05-15 20:07:25 +00:00
|
|
|
(?<label_id>\d+) | # Integer-based label ID, or
|
2015-05-14 20:59:39 +00:00
|
|
|
(?<label_name>
|
2015-05-15 20:07:25 +00:00
|
|
|
[A-Za-z0-9_-]+ | # String-based single-word label title, or
|
|
|
|
"[^&\?,]+" # String-based multi-word label surrounded in quotes
|
2015-05-14 20:59:39 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}x
|
|
|
|
end
|
|
|
|
|
2015-05-03 03:11:21 +00:00
|
|
|
# Returns the String necessary to reference this Label in Markdown
|
|
|
|
#
|
|
|
|
# format - Symbol format to use (default: :id, optional: :name)
|
|
|
|
#
|
|
|
|
# Note that its argument differs from other objects implementing Referable. If
|
|
|
|
# a non-Symbol argument is given (such as a Project), it will default to :id.
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# Label.first.to_reference # => "~1"
|
|
|
|
# Label.first.to_reference(:name) # => "~\"bug\""
|
|
|
|
#
|
|
|
|
# Returns a String
|
|
|
|
def to_reference(format = :id)
|
2015-05-15 20:07:25 +00:00
|
|
|
if format == :name && !name.include?('"')
|
2015-05-03 03:11:21 +00:00
|
|
|
%(#{self.class.reference_prefix}"#{name}")
|
|
|
|
else
|
|
|
|
"#{self.class.reference_prefix}#{id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-30 12:15:39 +00:00
|
|
|
def open_issues_count
|
|
|
|
issues.opened.count
|
|
|
|
end
|
2015-09-03 12:50:23 +00:00
|
|
|
|
|
|
|
def template?
|
|
|
|
template
|
|
|
|
end
|
2014-07-29 14:10:15 +00:00
|
|
|
end
|