label.rb 700 Bytes
Newer Older
1
class Label < ActiveRecord::Base
2
  DEFAULT_COLOR = '#428BCA'
3

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") }

Robert Schilling's avatar
Robert Schilling committed
21
  alias_attribute :name, :title
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
22
23
24
25

  def open_issues_count
    issues.opened.count
  end
26
end