label.rb 695 Bytes
Newer Older
1
class Label < ActiveRecord::Base
2
3
  DEFAULT_COLOR = '#82C5FF'

4
5
  belongs_to :project
  has_many :label_links, dependent: :destroy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
6
  has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
7

8
9
10
  validates :color,
            format: { with: /\A\#[0-9A-Fa-f]{6}+\Z/ },
            allow_blank: false
11
  validates :project, presence: true
12

13
  # Don't allow '?', '&', and ',' for label titles
14
15
16
  validates :title,
            presence: true,
            format: { with: /\A[^&\?,&]*\z/ },
17
            uniqueness: { scope: :project_id }
18

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
19
20
  scope :order_by_name, -> { reorder("labels.title ASC") }

21
22
23
  def name
    title
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
24
25
26
27

  def open_issues_count
    issues.opened.count
  end
28
end