2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class DeployKeysProject < ApplicationRecord
|
2019-08-01 12:18:17 -04:00
|
|
|
belongs_to :project, inverse_of: :deploy_keys_projects
|
2018-01-05 10:23:44 -05:00
|
|
|
belongs_to :deploy_key, inverse_of: :deploy_keys_projects
|
|
|
|
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) }
|
2020-07-15 08:09:26 -04:00
|
|
|
scope :with_deploy_keys, -> { includes(:deploy_key) }
|
2018-01-05 10:23:44 -05:00
|
|
|
|
|
|
|
accepts_nested_attributes_for :deploy_key
|
|
|
|
|
|
|
|
validates :deploy_key, presence: true
|
2013-05-06 05:26:36 -04:00
|
|
|
validates :deploy_key_id, uniqueness: { scope: [:project_id], message: "already exists in project" }
|
|
|
|
validates :project_id, presence: true
|
2015-03-17 09:55:43 -04:00
|
|
|
|
|
|
|
after_destroy :destroy_orphaned_deploy_key
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def destroy_orphaned_deploy_key
|
2015-04-03 06:22:44 -04:00
|
|
|
return unless self.deploy_key.destroyed_when_orphaned? && self.deploy_key.orphaned?
|
2017-08-15 13:44:37 -04:00
|
|
|
|
2015-03-27 09:43:48 -04:00
|
|
|
self.deploy_key.destroy
|
2015-03-17 09:55:43 -04:00
|
|
|
end
|
2013-05-06 05:26:36 -04:00
|
|
|
end
|