2014-08-25 05:25:02 -04: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 10:10:15 -04:00
|
|
|
class Label < ActiveRecord::Base
|
2015-05-02 23:11:21 -04:00
|
|
|
include Referable
|
2015-10-06 12:57:13 -04:00
|
|
|
# Represents a "No Label" state used for filtering Issues and Merge
|
|
|
|
# Requests that have no label assigned.
|
2015-10-09 00:24:55 -04:00
|
|
|
LabelStruct = Struct.new(:title, :name)
|
|
|
|
None = LabelStruct.new('No Label', 'No Label')
|
|
|
|
Any = LabelStruct.new('Any', '')
|
2015-05-02 23:11:21 -04:00
|
|
|
|
2014-08-15 05:02:05 -04:00
|
|
|
DEFAULT_COLOR = '#428BCA'
|
2014-08-14 04:17:52 -04:00
|
|
|
|
2015-04-17 11:38:31 -04:00
|
|
|
default_value_for :color, DEFAULT_COLOR
|
|
|
|
|
2014-07-29 10:10:15 -04:00
|
|
|
belongs_to :project
|
|
|
|
has_many :label_links, dependent: :destroy
|
2014-07-30 08:15:39 -04:00
|
|
|
has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
|
2014-07-29 10:10:15 -04:00
|
|
|
|
2014-08-11 17:59:30 -04:00
|
|
|
validates :color,
|
2014-08-17 16:22:01 -04:00
|
|
|
format: { with: /\A#[0-9A-Fa-f]{6}\Z/ },
|
2014-08-11 17:59:30 -04:00
|
|
|
allow_blank: false
|
2015-09-03 08:50:23 -04:00
|
|
|
validates :project, presence: true, unless: Proc.new { |service| service.template? }
|
2014-07-30 06:26:54 -04:00
|
|
|
|
2014-08-11 17:59:30 -04:00
|
|
|
# Don't allow '?', '&', and ',' for label titles
|
2014-08-12 04:53:50 -04:00
|
|
|
validates :title,
|
|
|
|
presence: true,
|
2015-04-10 16:14:00 -04:00
|
|
|
format: { with: /\A[^&\?,]+\z/ },
|
2014-08-13 08:12:05 -04:00
|
|
|
uniqueness: { scope: :project_id }
|
2014-07-29 12:19:26 -04:00
|
|
|
|
2015-02-05 23:29:41 -05:00
|
|
|
default_scope { order(title: :asc) }
|
2014-07-30 12:13:35 -04:00
|
|
|
|
2015-09-03 08:50:23 -04:00
|
|
|
scope :templates, -> { where(template: true) }
|
|
|
|
|
2014-08-12 08:16:25 -04:00
|
|
|
alias_attribute :name, :title
|
2014-07-30 08:15:39 -04:00
|
|
|
|
2015-05-02 23:11:21 -04:00
|
|
|
def self.reference_prefix
|
|
|
|
'~'
|
|
|
|
end
|
|
|
|
|
2015-05-14 16:59:39 -04:00
|
|
|
# Pattern used to extract label references from text
|
|
|
|
def self.reference_pattern
|
|
|
|
%r{
|
|
|
|
#{reference_prefix}
|
|
|
|
(?:
|
2015-05-15 16:07:25 -04:00
|
|
|
(?<label_id>\d+) | # Integer-based label ID, or
|
2015-05-14 16:59:39 -04:00
|
|
|
(?<label_name>
|
2015-05-15 16:07:25 -04:00
|
|
|
[A-Za-z0-9_-]+ | # String-based single-word label title, or
|
|
|
|
"[^&\?,]+" # String-based multi-word label surrounded in quotes
|
2015-05-14 16:59:39 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}x
|
|
|
|
end
|
|
|
|
|
2015-05-02 23:11:21 -04: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 16:07:25 -04:00
|
|
|
if format == :name && !name.include?('"')
|
2015-05-02 23:11:21 -04:00
|
|
|
%(#{self.class.reference_prefix}"#{name}")
|
|
|
|
else
|
|
|
|
"#{self.class.reference_prefix}#{id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-30 08:15:39 -04:00
|
|
|
def open_issues_count
|
|
|
|
issues.opened.count
|
|
|
|
end
|
2015-09-03 08:50:23 -04:00
|
|
|
|
|
|
|
def template?
|
|
|
|
template
|
|
|
|
end
|
2014-07-29 10:10:15 -04:00
|
|
|
end
|