c0faf91ff2
Now there is a single source of information for which attribute a model uses to be referenced, and its special character.
67 lines
1.6 KiB
Ruby
67 lines
1.6 KiB
Ruby
# == 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
|
|
#
|
|
|
|
class Label < ActiveRecord::Base
|
|
include Referable
|
|
|
|
DEFAULT_COLOR = '#428BCA'
|
|
|
|
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'
|
|
|
|
validates :color,
|
|
format: { with: /\A#[0-9A-Fa-f]{6}\Z/ },
|
|
allow_blank: false
|
|
validates :project, presence: true
|
|
|
|
# Don't allow '?', '&', and ',' for label titles
|
|
validates :title,
|
|
presence: true,
|
|
format: { with: /\A[^&\?,]+\z/ },
|
|
uniqueness: { scope: :project_id }
|
|
|
|
default_scope { order(title: :asc) }
|
|
|
|
alias_attribute :name, :title
|
|
|
|
def self.reference_prefix
|
|
'~'
|
|
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
|
|
%(#{self.class.reference_prefix}"#{name}")
|
|
else
|
|
"#{self.class.reference_prefix}#{id}"
|
|
end
|
|
end
|
|
|
|
def open_issues_count
|
|
issues.opened.count
|
|
end
|
|
end
|