debian-mirror-gitlab/app/presenters/projects/settings/deploy_keys_presenter.rb

72 lines
2.1 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Projects
module Settings
class DeployKeysPresenter < Gitlab::View::Presenter::Simple
presents :project
delegate :size, to: :enabled_keys, prefix: true
delegate :size, to: :available_project_keys, prefix: true
delegate :size, to: :available_public_keys, prefix: true
def new_key
2018-03-17 18:26:18 +05:30
@key ||= DeployKey.new.tap { |dk| dk.deploy_keys_projects.build }
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def enabled_keys
2017-09-10 17:25:29 +05:30
@enabled_keys ||= project.deploy_keys.includes(:projects)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def any_keys_enabled?
enabled_keys.any?
end
def available_keys
@available_keys ||= current_user.accessible_deploy_keys - enabled_keys
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def available_project_keys
2017-09-10 17:25:29 +05:30
@available_project_keys ||= current_user.project_deploy_keys.includes(:projects) - enabled_keys
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def key_available?(deploy_key)
available_keys.include?(deploy_key)
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def available_public_keys
return @available_public_keys if defined?(@available_public_keys)
2017-09-10 17:25:29 +05:30
@available_public_keys ||= DeployKey.are_public.includes(:projects) - enabled_keys
2017-08-17 22:00:37 +05:30
# Public keys that are already used by another accessible project are already
# in @available_project_keys.
@available_public_keys -= available_project_keys
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def as_json
2018-12-05 23:21:45 +05:30
serializer = DeployKeySerializer.new # rubocop: disable CodeReuse/Serializer
2017-08-17 22:00:37 +05:30
opts = { user: current_user }
{
enabled_keys: serializer.represent(enabled_keys, opts),
available_project_keys: serializer.represent(available_project_keys, opts),
public_keys: serializer.represent(available_public_keys, opts)
}
end
def to_partial_path
'projects/deploy_keys/index'
end
def form_partial_path
'projects/deploy_keys/form'
end
end
end
end