summaryrefslogtreecommitdiff
path: root/app/models/label.rb
blob: c8f6a7cd48c4ca42a4c3189486aa11c12578ebeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# == 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
#

class Label < ActiveRecord::Base
  include Sortable

  DEFAULT_COLOR = '#428BCA'

  belongs_to :project
  has_many :label_links, dependent: :destroy
  has_many :issues, through: :label_links, source: :target, source_type: 'Issue'

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

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

  scope :order_by_name, -> { reorder("labels.title ASC") }

  alias_attribute :name, :title

  def open_issues_count
    issues.opened.count
  end
end