label.rb 666 Bytes
Newer Older
1
2
3
class Label < ActiveRecord::Base
  belongs_to :project
  has_many :label_links, dependent: :destroy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
4
  has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
5

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

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

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

19
20
21
  def name
    title
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
22
23
24
25

  def open_issues_count
    issues.opened.count
  end
26
end