BigW Consortium Gitlab

label.rb 2.37 KB
Newer Older
Dmitriy Zaporozhets committed
1 2 3 4 5 6 7 8 9 10
# == 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
Dmitriy Zaporozhets committed
11
#  template   :boolean          default(FALSE)
Dmitriy Zaporozhets committed
12 13
#

14
class Label < ActiveRecord::Base
15
  include Referable
16 17
  # Represents a "No Label" state used for filtering Issues and Merge
  # Requests that have no label assigned.
18 19
  LabelStruct = Struct.new(:title, :name)
  None = LabelStruct.new('No Label', 'No Label')
20
  Any = LabelStruct.new('Any Label', '')
21

22
  DEFAULT_COLOR = '#428BCA'
23

Douwe Maan committed
24 25
  default_value_for :color, DEFAULT_COLOR

26 27
  belongs_to :project
  has_many :label_links, dependent: :destroy
Dmitriy Zaporozhets committed
28
  has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
29

30
  validates :color, color: true, allow_blank: false
Valery Sizov committed
31
  validates :project, presence: true, unless: Proc.new { |service| service.template? }
32

33
  # Don't allow '?', '&', and ',' for label titles
34 35
  validates :title,
            presence: true,
36
            format: { with: /\A[^&\?,]+\z/ },
37
            uniqueness: { scope: :project_id }
38

39
  default_scope { order(title: :asc) }
40

Valery Sizov committed
41 42
  scope :templates, ->  { where(template: true) }

43
  alias_attribute :name, :title
Dmitriy Zaporozhets committed
44

45 46 47 48
  def self.reference_prefix
    '~'
  end

49 50 51 52 53
  # Pattern used to extract label references from text
  def self.reference_pattern
    %r{
      #{reference_prefix}
      (?:
54
        (?<label_id>\d+) | # Integer-based label ID, or
55
        (?<label_name>
56 57
          [A-Za-z0-9_-]+ | # String-based single-word label title, or
          "[^&\?,]+"       # String-based multi-word label surrounded in quotes
58 59 60 61 62
        )
      )
    }x
  end

63 64 65 66 67 68 69 70 71 72 73 74 75 76
  # 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)
77
    if format == :name && !name.include?('"')
78 79 80 81 82 83
      %(#{self.class.reference_prefix}"#{name}")
    else
      "#{self.class.reference_prefix}#{id}"
    end
  end

Dmitriy Zaporozhets committed
84 85 86
  def open_issues_count
    issues.opened.count
  end
Valery Sizov committed
87 88 89 90

  def template?
    template
  end
91
end