2013-05-06 05:26:36 -04:00
|
|
|
class DeployKeysProject < ActiveRecord::Base
|
|
|
|
belongs_to :project
|
2018-01-05 10:23:44 -05:00
|
|
|
belongs_to :deploy_key, inverse_of: :deploy_keys_projects
|
2013-05-06 05:26:36 -04:00
|
|
|
|
2018-01-05 10:23:44 -05: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 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
|