2015-04-26 12:48:37 +05:30
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: labels
|
|
|
|
#
|
2016-04-02 18:10:28 +05:30
|
|
|
# id :integer not null, primary key
|
|
|
|
# title :string(255)
|
|
|
|
# color :string(255)
|
|
|
|
# project_id :integer
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# template :boolean default(FALSE)
|
|
|
|
# description :string(255)
|
2015-04-26 12:48:37 +05:30
|
|
|
#
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
class Label < ActiveRecord::Base
|
2015-09-11 14:41:01 +05:30
|
|
|
include Referable
|
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
|
|
|
|
has_many :label_links, dependent: :destroy
|
|
|
|
has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
|
|
|
|
|
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
|
|
|
|
|
|
|
# Don't allow '?', '&', and ',' for label titles
|
|
|
|
validates :title,
|
|
|
|
presence: true,
|
2015-09-11 14:41:01 +05:30
|
|
|
format: { with: /\A[^&\?,]+\z/ },
|
2014-09-02 18:07:02 +05:30
|
|
|
uniqueness: { scope: :project_id }
|
|
|
|
|
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) }
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# Pattern used to extract label references from text
|
|
|
|
def self.reference_pattern
|
|
|
|
%r{
|
|
|
|
#{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
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
if format == :name && !name.include?('"')
|
|
|
|
%(#{self.class.reference_prefix}"#{name}")
|
|
|
|
else
|
|
|
|
"#{self.class.reference_prefix}#{id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def open_issues_count
|
|
|
|
issues.opened.count
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
def closed_issues_count
|
|
|
|
issues.closed.count
|
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
def template?
|
|
|
|
template
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|