2013-05-06 05:26:36 -04:00
|
|
|
class DeployKey < Key
|
2018-01-05 10:23:44 -05:00
|
|
|
include IgnorableColumn
|
|
|
|
|
|
|
|
has_many :deploy_keys_projects, inverse_of: :deploy_key, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
2018-04-09 07:19:18 -04:00
|
|
|
has_many :projects, -> { auto_include(false) }, through: :deploy_keys_projects
|
2013-05-06 08:10:55 -04:00
|
|
|
|
|
|
|
scope :in_projects, ->(projects) { joins(:deploy_keys_projects).where('deploy_keys_projects.project_id in (?)', projects) }
|
2015-03-27 09:43:48 -04:00
|
|
|
scope :are_public, -> { where(public: true) }
|
|
|
|
|
2018-01-05 10:23:44 -05:00
|
|
|
ignore_column :can_push
|
|
|
|
|
|
|
|
accepts_nested_attributes_for :deploy_keys_projects
|
|
|
|
|
2015-03-27 09:43:48 -04:00
|
|
|
def private?
|
|
|
|
!public?
|
|
|
|
end
|
2015-04-03 06:22:44 -04:00
|
|
|
|
|
|
|
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
|
2016-11-11 08:44:33 -05:00
|
|
|
|
2018-03-28 12:54:15 -04:00
|
|
|
def user
|
|
|
|
super || User.ghost
|
|
|
|
end
|
|
|
|
|
2016-11-16 07:31:08 -05:00
|
|
|
def has_access_to?(project)
|
2018-01-05 10:23:44 -05:00
|
|
|
deploy_keys_project_for(project).present?
|
2016-11-16 07:31:08 -05:00
|
|
|
end
|
|
|
|
|
2016-11-11 08:44:33 -05:00
|
|
|
def can_push_to?(project)
|
2018-01-05 10:23:44 -05:00
|
|
|
!!deploy_keys_project_for(project)&.can_push?
|
|
|
|
end
|
|
|
|
|
|
|
|
def deploy_keys_project_for(project)
|
|
|
|
deploy_keys_projects.find_by(project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def projects_with_write_access
|
|
|
|
Project.preload(:route).where(id: deploy_keys_projects.with_write_access.select(:project_id))
|
2016-11-11 08:44:33 -05:00
|
|
|
end
|
2013-05-06 05:26:36 -04:00
|
|
|
end
|