deploy_keys_project.rb 826 Bytes
Newer Older
1 2
# frozen_string_literal: true

3 4
class DeployKeysProject < ActiveRecord::Base
  belongs_to :project
5
  belongs_to :deploy_key, inverse_of: :deploy_keys_projects
6

7 8 9 10 11 12 13
  scope :without_project_deleted,  -> { joins(:project).where(projects: { pending_delete: false }) }
  scope :in_project, ->(project) { where(project: project) }
  scope :with_write_access, -> { where(can_push: true) }

  accepts_nested_attributes_for :deploy_key

  validates :deploy_key, presence: true
14 15
  validates :deploy_key_id, uniqueness: { scope: [:project_id], message: "already exists in project" }
  validates :project_id, presence: true
16 17 18 19 20 21

  after_destroy :destroy_orphaned_deploy_key

  private

  def destroy_orphaned_deploy_key
Douwe Maan's avatar
Douwe Maan committed
22
    return unless self.deploy_key.destroyed_when_orphaned? && self.deploy_key.orphaned?
23

24
    self.deploy_key.destroy
25
  end
26
end