Commit ed097df6 authored by Douwe Maan's avatar Douwe Maan

Clean up code.

parent a243e44c
class Admin::DeployKeysController < Admin::ApplicationController class Admin::DeployKeysController < Admin::ApplicationController
before_filter :deploy_keys, only: [:index]
before_filter :deploy_key, only: [:show, :destroy] before_filter :deploy_key, only: [:show, :destroy]
def index def index
@deploy_keys = DeployKey.are_public
end end
def show def show
...@@ -10,12 +11,11 @@ class Admin::DeployKeysController < Admin::ApplicationController ...@@ -10,12 +11,11 @@ class Admin::DeployKeysController < Admin::ApplicationController
end end
def new def new
@deploy_key = DeployKey.new(public: true) @deploy_key = deploy_keys.new
end end
def create def create
@deploy_key = DeployKey.new(deploy_key_params) @deploy_key = deploy_keys.new(deploy_key_params)
@deploy_key.public = true
if @deploy_key.save if @deploy_key.save
redirect_to admin_deploy_keys_path redirect_to admin_deploy_keys_path
...@@ -36,7 +36,11 @@ class Admin::DeployKeysController < Admin::ApplicationController ...@@ -36,7 +36,11 @@ class Admin::DeployKeysController < Admin::ApplicationController
protected protected
def deploy_key def deploy_key
@deploy_key ||= DeployKey.find(params[:id]) @deploy_key ||= deploy_keys.find(params[:id])
end
def deploy_keys
@deploy_keys ||= DeployKey.are_public
end end
def deploy_key_params def deploy_key_params
......
...@@ -8,9 +8,14 @@ class Projects::DeployKeysController < Projects::ApplicationController ...@@ -8,9 +8,14 @@ class Projects::DeployKeysController < Projects::ApplicationController
def index def index
@enabled_keys = @project.deploy_keys @enabled_keys = @project.deploy_keys
@available_keys = available_keys - @enabled_keys
@available_keys = accessible_keys - @enabled_keys
@available_project_keys = current_user.project_deploy_keys - @enabled_keys @available_project_keys = current_user.project_deploy_keys - @enabled_keys
@available_public_keys = DeployKey.are_public - @available_project_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 end
def show def show
...@@ -35,7 +40,7 @@ class Projects::DeployKeysController < Projects::ApplicationController ...@@ -35,7 +40,7 @@ class Projects::DeployKeysController < Projects::ApplicationController
end end
def enable def enable
@key = current_user.accessible_deploy_keys.find(params[:id]) @key = accessible_keys.find(params[:id])
@project.deploy_keys << @key @project.deploy_keys << @key
redirect_to namespace_project_deploy_keys_path(@project.namespace, redirect_to namespace_project_deploy_keys_path(@project.namespace,
...@@ -50,8 +55,8 @@ class Projects::DeployKeysController < Projects::ApplicationController ...@@ -50,8 +55,8 @@ class Projects::DeployKeysController < Projects::ApplicationController
protected protected
def available_keys def accessible_keys
@available_keys ||= current_user.accessible_deploy_keys @accessible_keys ||= current_user.accessible_deploy_keys
end end
def deploy_key_params def deploy_key_params
......
...@@ -127,6 +127,14 @@ module ProjectsHelper ...@@ -127,6 +127,14 @@ module ProjectsHelper
html + count_html html + count_html
end end
def project_for_deploy_key(deploy_key)
if deploy_key.projects.include?(@project)
@project
else
deploy_key.projects.find { |project| can?(current_user, :read_project, project) }
end
end
private private
def get_project_nav_tabs(project, current_user) def get_project_nav_tabs(project, current_user)
......
...@@ -19,9 +19,20 @@ class DeployKey < Key ...@@ -19,9 +19,20 @@ class DeployKey < Key
scope :in_projects, ->(projects) { joins(:deploy_keys_projects).where('deploy_keys_projects.project_id in (?)', projects) } scope :in_projects, ->(projects) { joins(:deploy_keys_projects).where('deploy_keys_projects.project_id in (?)', projects) }
scope :are_public, -> { where(public: true) } scope :are_public, -> { where(public: true) }
scope :are_private, -> { where(public: false) }
def private? def private?
!public? !public?
end 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
end end
...@@ -22,9 +22,7 @@ class DeployKeysProject < ActiveRecord::Base ...@@ -22,9 +22,7 @@ class DeployKeysProject < ActiveRecord::Base
private private
def destroy_orphaned_deploy_key def destroy_orphaned_deploy_key
# Public deploy keys are never automatically deleted return unless self.deploy_key.destroyed_when_orphaned? && self.deploy_key.orphaned?
return if self.deploy_key.public?
return if self.deploy_key.deploy_keys_projects.length > 0
self.deploy_key.destroy self.deploy_key.destroy
end end
......
...@@ -5,21 +5,15 @@ ...@@ -5,21 +5,15 @@
%i.fa.fa-plus %i.fa.fa-plus
Enable Enable
- else - else
- if deploy_key.projects.count > 1 || deploy_key.public? - if deploy_key.destroyed_when_orphaned? && deploy_key.almost_orphaned?
= link_to 'Remove', disable_namespace_project_deploy_key_path(@project.namespace, @project, deploy_key), data: { confirm: 'You are going to remove deploy key. Are you sure?'}, method: :put, class: "btn btn-remove delete-key btn-sm pull-right"
- else
= link_to disable_namespace_project_deploy_key_path(@project.namespace, @project, deploy_key), class: 'btn btn-sm', method: :put do = link_to disable_namespace_project_deploy_key_path(@project.namespace, @project, deploy_key), class: 'btn btn-sm', method: :put do
%i.fa.fa-power-off %i.fa.fa-power-off
Disable Disable
- else
= link_to 'Remove', disable_namespace_project_deploy_key_path(@project.namespace, @project, deploy_key), data: { confirm: 'You are going to remove deploy key. Are you sure?'}, method: :put, class: "btn btn-remove delete-key btn-sm pull-right"
- if deploy_key.projects.include?(@project)
- key_project = @project
- else
- key_project = deploy_key.projects.find { |project| can?(current_user, :read_project, project) }
- if key_project - if project = project_for_deploy_key(deploy_key)
= link_to namespace_project_deploy_key_path(key_project.namespace, key_project, deploy_key) do = link_to namespace_project_deploy_key_path(project.namespace, project, deploy_key) do
%i.fa.fa-key %i.fa.fa-key
%strong= deploy_key.title %strong= deploy_key.title
- else - else
......
...@@ -22,7 +22,8 @@ ...@@ -22,7 +22,8 @@
.light-well .light-well
.nothing-here-block Create a #{link_to 'new deploy key', new_namespace_project_deploy_key_path(@project.namespace, @project)} or add an existing one .nothing-here-block Create a #{link_to 'new deploy key', new_namespace_project_deploy_key_path(@project.namespace, @project)} or add an existing one
.col-md-6.available-keys .col-md-6.available-keys
- unless @available_project_keys.blank? && @available_public_keys.any? - # If there are available public deploy keys but no available project deploy keys, only public deploy keys are shown.
- if @available_project_keys.any? || @available_public_keys.blank?
%h5 %h5
%strong Deploy keys %strong Deploy keys
from projects you have access to from projects you have access to
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment