label.rb 951 Bytes
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1
2
3
4
5
6
7
8
9
10
11
12
# == 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
#

13
class Label < ActiveRecord::Base
14
  DEFAULT_COLOR = '#428BCA'
15

Douwe Maan's avatar
Douwe Maan committed
16
17
  default_value_for :color, DEFAULT_COLOR

18
19
  belongs_to :project
  has_many :label_links, dependent: :destroy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
20
  has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
21

22
  validates :color,
23
            format: { with: /\A#[0-9A-Fa-f]{6}\Z/ },
24
            allow_blank: false
25
  validates :project, presence: true
26

27
  # Don't allow '?', '&', and ',' for label titles
28
29
  validates :title,
            presence: true,
30
            format: { with: /\A[^&\?,]+\z/ },
31
            uniqueness: { scope: :project_id }
32

33
  default_scope { order(title: :asc) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
34

Robert Schilling's avatar
Robert Schilling committed
35
  alias_attribute :name, :title
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
36
37
38
39

  def open_issues_count
    issues.opened.count
  end
40
end