main
rb 26 lines 698 Bytes
Raw
1 # frozen_string_literal: true
2
3 # A repository issue, numbered per-repository. Comments attach polymorphically
4 # so the same machinery serves issues and pull requests.
5 class Issue < ApplicationRecord
6 STATES = %w[open closed].freeze
7
8 belongs_to :repository
9 belongs_to :user # author
10 has_many :comments, as: :commentable, dependent: :destroy
11
12 scope :open, -> { where(state: "open") }
13 scope :closed, -> { where(state: "closed") }
14
15 validates :number, presence: true, uniqueness: { scope: :repository_id }
16 validates :title, presence: true
17 validates :state, inclusion: { in: STATES }
18
19 def author
20 user
21 end
22
23 def html_url
24 "#{repository.html_url}/issues/#{number}"
25 end
26 end