debian-mirror-gitlab/app/models/deploy_key.rb

52 lines
1.2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class DeployKey < Key
2018-03-17 18:26:18 +05:30
include IgnorableColumn
has_many :deploy_keys_projects, inverse_of: :deploy_key, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
2014-09-02 18:07:02 +05:30
has_many :projects, through: :deploy_keys_projects
scope :in_projects, ->(projects) { joins(:deploy_keys_projects).where('deploy_keys_projects.project_id in (?)', projects) }
2015-04-26 12:48:37 +05:30
scope :are_public, -> { where(public: true) }
2018-03-17 18:26:18 +05:30
ignore_column :can_push
accepts_nested_attributes_for :deploy_keys_projects
2015-04-26 12:48:37 +05:30
def private?
!public?
end
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
2017-08-17 22:00:37 +05:30
2018-05-09 12:01:36 +05:30
def user
super || User.ghost
end
2017-08-17 22:00:37 +05:30
def has_access_to?(project)
2018-03-17 18:26:18 +05:30
deploy_keys_project_for(project).present?
2017-08-17 22:00:37 +05:30
end
def can_push_to?(project)
2018-03-17 18:26:18 +05:30
!!deploy_keys_project_for(project)&.can_push?
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
def deploy_keys_project_for(project)
deploy_keys_projects.find_by(project: project)
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
def projects_with_write_access
Project.preload(:route).where(id: deploy_keys_projects.with_write_access.select(:project_id))
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
end