BigW Consortium Gitlab

deploy_key.rb 879 Bytes
Newer Older
Dmitriy Zaporozhets committed
1 2 3 4
# == Schema Information
#
# Table name: keys
#
Dmitriy Zaporozhets committed
5 6
#  id          :integer          not null, primary key
#  user_id     :integer
Dmitriy Zaporozhets committed
7 8
#  created_at  :datetime
#  updated_at  :datetime
Dmitriy Zaporozhets committed
9 10 11 12
#  key         :text
#  title       :string(255)
#  type        :string(255)
#  fingerprint :string(255)
Stan Hu committed
13
#  public      :boolean          default(FALSE), not null
Dmitriy Zaporozhets committed
14 15
#

16 17 18
class DeployKey < Key
  has_many :deploy_keys_projects, dependent: :destroy
  has_many :projects, through: :deploy_keys_projects
19 20

  scope :in_projects, ->(projects) { joins(:deploy_keys_projects).where('deploy_keys_projects.project_id in (?)', projects) }
21 22 23 24 25
  scope :are_public,  -> { where(public: true) }

  def private?
    !public?
  end
Douwe Maan committed
26 27 28 29 30 31 32 33 34 35 36 37

  def orphaned?
    self.deploy_keys_projects.length == 0
  end

  def almost_orphaned?
    self.deploy_keys_projects.length == 1
  end

  def destroyed_when_orphaned?
    self.private?
  end
38
end