Introduced the deploy keys presenter

This commit is contained in:
Grzegorz Bizon 2017-01-31 15:12:02 +01:00 committed by Jose Ivan Vargas
parent 2ee8644115
commit 5967c17e80
7 changed files with 110 additions and 32 deletions

View file

@ -7,7 +7,9 @@ module Projects
before_action :load_protected_branches, only: [:show]
def show
define_deploy_keys_variables
@deploy_keys = DeployKeysPresenter
.new(@project, current_user: @current_user)
define_protected_branches_controller
end
@ -15,25 +17,8 @@ module Projects
@protected_branches = @project.protected_branches.order(:name).page(params[:page])
end
def set_index_vars
@enabled_keys ||= @project.deploy_keys
@available_keys ||= current_user.accessible_deploy_keys - @enabled_keys
@available_project_keys ||= current_user.project_deploy_keys - @enabled_keys
@available_public_keys ||= DeployKey.are_public - @enabled_keys
# Public keys that are already used by another accessible project are already
# in @available_project_keys.
@available_public_keys -= @available_project_keys
end
private
def define_deploy_keys_variables
@key = DeployKey.new
set_index_vars
end
def define_protected_branches_controller
@protected_branch = @project.protected_branches.new
load_gon_index(@project)

View file

@ -0,0 +1,65 @@
module Projects
module Settings
class DeployKeysPresenter < Gitlab::View::Presenter::Simple
presents :project
def new_key
@key ||= DeployKey.new
end
def enabled_keys
@enabled_keys ||= project.deploy_keys
end
def any_keys_enabled?
enabled_keys.any?
end
def enabled_keys_size
enabled_keys.size
end
def available_keys
@available_keys ||= current_user.accessible_deploy_keys - enabled_keys
end
def available_project_keys
@available_project_keys ||= current_user.project_deploy_keys - enabled_keys
end
def any_available_project_keys_enabled?
available_project_keys.any?
end
def available_project_keys_size
available_project_keys.size
end
def available_public_keys
return @available_public_keys if defined?(@available_public_keys)
@available_public_keys ||= DeployKey.are_public - enabled_keys
# Public keys that are already used by another accessible project are already
# in @available_project_keys.
@available_public_keys -= available_project_keys
end
def any_available_public_keys_enabled?
available_public_keys.any?
end
def available_public_keys_size
available_public_keys.size
end
def to_partial_path
'projects/deploy_keys/index'
end
def form_partial_path
'projects/deploy_keys/form'
end
end
end
end

View file

@ -18,7 +18,7 @@
%span.key-created-at
created #{time_ago_with_tooltip(deploy_key.created_at)}
.visible-xs-block.visible-sm-block
- if @available_keys.include?(deploy_key)
- if @deploy_keys.available_keys.include?(deploy_key)
= link_to enable_namespace_project_deploy_key_path(@project.namespace, @project, deploy_key), class: "btn btn-sm prepend-left-10", method: :put do
Enable
- else

View file

@ -1,5 +1,5 @@
= form_for [@project.namespace.becomes(Namespace), @project, @key], url: namespace_project_deploy_keys_path, html: { class: "js-requires-input" } do |f|
= form_errors(@key)
= form_for [@project.namespace.becomes(Namespace), @project, @deploy_keys.new_key], url: namespace_project_deploy_keys_path, html: { class: "js-requires-input" } do |f|
= form_errors(@deploy_keys.new_key)
.form-group
= f.label :title, class: "label-light"
= f.text_field :title, class: 'form-control', autofocus: true, required: true

View file

@ -7,31 +7,31 @@
.col-lg-9
%h5.prepend-top-0
Create a new deploy key for this project
= render "projects/deploy_keys/form"
= render @deploy_keys.form_partial_path
.col-lg-9.col-lg-offset-3
%hr
.col-lg-9.col-lg-offset-3.append-bottom-default.deploy-keys
%h5.prepend-top-0
Enabled deploy keys for this project (#{@enabled_keys.size})
- if @enabled_keys.any?
Enabled deploy keys for this project (#{@deploy_keys.enabled_keys_size})
- if @deploy_keys.any_keys_enabled?
%ul.well-list
- @enabled_keys.each do |enabled_key|
- @deploy_keys.enabled_keys.each do |enabled_key|
= render partial: 'projects/deploy_keys/deploy_key', locals: {deploy_key: enabled_key}
- else
.settings-message.text-center
No deploy keys found. Create one with the form above or add existing one below.
%h5.prepend-top-default
Deploy keys from projects you have access to (#{@available_project_keys.size})
- if @available_project_keys.any?
Deploy keys from projects you have access to (#{@deploy_keys.available_project_keys_size})
- if @deploy_keys.any_available_project_keys_enabled?
%ul.well-list
- @available_project_keys.each do |available_key|
- @deploy_keys.available_project_keys.each do |available_key|
= render partial: 'projects/deploy_keys/deploy_key', locals: {deploy_key: available_key}
- else
.settings-message.text-center
No deploy keys from your projects could be found. Create one with the form above or add existing one below.
- if @available_public_keys.any?
- if @deploy_keys.any_available_public_keys_enabled?
%h5.prepend-top-default
Public deploy keys available to any project (#{@available_public_keys.size})
Public deploy keys available to any project (#{@deploy_keys.available_public_keys_size})
%ul.well-list
- @available_public_keys.each do |available_key|
- @deploy_keys.available_public_keys.each do |available_key|
= render partial: 'projects/deploy_keys/deploy_key', locals: {deploy_key: available_key}

View file

@ -1,4 +1,4 @@
- page_title "Repository"
= render "projects/deploy_keys/index"
= render @deploy_keys
= render "projects/protected_branches/index"

View file

@ -0,0 +1,28 @@
require 'spec_helper'
describe Projects::Settings::DeployKeysPresenter do
let(:project) { create(:empty_project) }
let(:user) { create(:user) }
subject(:presenter) do
described_class.new(project, current_user: user)
end
it 'inherits from Gitlab::View::Presenter::Simple' do
expect(described_class.superclass).to eq(Gitlab::View::Presenter::Simple)
end
describe '#enabled_keys' do
let(:deploy_key) do
create(:deploy_keys_project, project: project).deploy_key
end
it 'returns project keys' do
expect(presenter.enabled_keys).to eq [deploy_key]
end
it 'does not contain enabled_keys inside available_keys' do
expect(presenter.available_keys).not_to include deploy_key
end
end
end