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