Skip to content

Commit

Permalink
Merge pull request #6164 from krauselukas/feature/staging/delete_stag…
Browse files Browse the repository at this point in the history
…ing_project

Delete Staging Workflows
  • Loading branch information
bgeuken committed Nov 13, 2018
2 parents dc4e57c + fdd6ae2 commit 0f26b2e
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/api/app/assets/javascripts/webui2/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@
//= require webui2/jquery-ui.min.js
//= require webui2/cm2/use-codemirror.js
//= require webui2/package-view_file.js
//= require webui2/staging_workflow.js
9 changes: 9 additions & 0 deletions src/api/app/assets/javascripts/webui2/staging_workflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function setSpinnersForDeletion() { // jshint ignore:line
$("#staging-workflow-delete").on('ajax:beforeSend', function(){
$(this).find('.delete-spinner').removeClass('d-none');
});

$("#staging-workflow-delete").on('ajax:complete', function(){
$(this).find('.delete-spinner').addClass('d-none');
});
}
19 changes: 18 additions & 1 deletion src/api/app/controllers/webui/staging_workflows_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def new

def create
staging_workflow = @project.build_staging

authorize staging_workflow

if staging_workflow.save
Expand Down Expand Up @@ -47,6 +46,24 @@ def edit
@project = @staging_workflow.project
end

def destroy
@staging_workflow = StagingWorkflow.find_by(id: params[:id])
authorize @staging_workflow
@project = @staging_workflow.project

# attached staging projects get nullified by default but we want to
# allow to destroy manually by setting a checkbox
@staging_workflow.staging_projects.where(id: params[:staging_project_ids]).destroy_all

if @staging_workflow.destroy
flash[:success] = "Staging Workflow for #{@project.name} was successfully deleted."
render js: "window.location='#{project_show_path(@project)}'"
else
flash[:error] = "Staging Workflow for #{@project.name} couldn't be deleted: #{@staging_workflow.errors.full_messages.to_sentence}."
render js: "window.location='#{staging_workflow_path(@staging_workflow)}'"
end
end

private

def set_bootstrap_views
Expand Down
2 changes: 1 addition & 1 deletion src/api/app/policies/staging_workflow_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ def edit?
end

def destroy?
update?
ProjectPolicy.new(@user, @record.project).destroy?
end
end
28 changes: 28 additions & 0 deletions src/api/app/views/webui2/webui/staging_workflows/_delete.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
%button.btn.btn-danger{ data: { toggle: 'modal', target: '#delete-staging-workflow' } }
Delete Staging Workflow

.modal.fade{ id: 'delete-staging-workflow', tabindex: -1, role: 'dialog' }
.modal-dialog.modal-dialog-centered{ role: 'document' }
.modal-content
.modal-header
%h5.modal-title
Do you want to delete the staging workflow for #{project.name}?
.modal-body
%p Please confirm deletion of the staging workflow for #{project.name}
= form_tag staging_workflow_path, method: :delete, remote: true, id: 'staging-workflow-delete' do
- if @staging_workflow.staging_projects.any?
%p Check the staging projects you want to be deleted:
- @staging_workflow.staging_projects.each do |staging_project|
.form-check
= check_box_tag 'staging_project_ids[]', staging_project.id, false, class: 'form-check-input'
%label.form-check-label
= staging_project.name
%br
.modal-footer
%i.fas.fa-spinner.fa-spin.delete-spinner.d-none
%button.btn.btn-sm.btn-outline-secondary.px-4{ data: { dismiss: 'modal' } }
Cancel
= submit_tag 'Delete', class: 'btn btn-sm btn-danger px-4'

= content_for :ready_function do
setSpinnersForDeletion();
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
= link_to(edit_staging_workflow_path(@staging_workflow), class: 'nav-link') do
%i.fas.fa-edit.text-info
Edit Staging Workflow
- if policy(@staging_workflow).destroy?
= render(partial: 'webui2/webui/staging_workflows/delete', locals: { project: @project })
%hr

%table.table.table-striped.table-bordered.table-sm.dt-responsive.w-100#staging-projects-datatable
Expand Down
2 changes: 1 addition & 1 deletion src/api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def self.public_or_about_path?(request)
get 'published/:project(/:repository(/:arch(/:binary)))' => 'published#index', constraints: cons
get 'published/' => 'source#index', via: :get

resources :staging_workflows, except: [:index, :update, :destroy], controller: 'webui/staging_workflows', constraints: cons do
resources :staging_workflows, except: [:index, :update], controller: 'webui/staging_workflows', constraints: cons do
resources :staging_projects, only: [:create, :destroy], controller: 'webui/staging_workflows/staging_projects', param: :project_name, constraints: cons
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,50 @@
it { expect(response).to render_template(:edit) }
end
end

describe 'DELETE #destroy' do
context 'a staging workflow and staging projects' do
before do
project.create_staging
params = { id: project.staging, staging_project_ids: project.staging.staging_projects.ids, format: :js }
delete :destroy, params: params
end

subject { project.staging }

it { expect(StagingWorkflow.count).to eq(0) }
it { expect(subject.staging_projects.count).to eq(0) }
it { expect(flash[:success]).not_to be_nil }
it { expect(response.body).to eq("window.location='#{project_show_path(project)}'") }
it { expect(project.subprojects.count).to eq(0) }
end

context 'a staging workflow and one staging project' do
before do
project.create_staging
params = { id: project.staging, staging_project_ids: project.staging.staging_projects.ids.first, format: :js }
delete :destroy, params: params
end

subject { project.staging }

it { expect(StagingWorkflow.count).to eq(0) }
it { expect(subject.staging_projects.count).to eq(0) }
it { expect(project.subprojects.count).to eq(1) }
it { expect(flash[:success]).not_to be_nil }
it { expect(response.body).to eq("window.location='#{project_show_path(project)}'") }
end

context 'a staging workflow unsuccessful' do
before do
project.create_staging
allow_any_instance_of(StagingWorkflow).to receive(:destroy).and_return(false)
params = { id: project.staging, staging_project_ids: project.staging.staging_projects.ids, format: :js }
delete :destroy, params: params
end

it { expect(flash[:error]).not_to be_nil }
it { expect(response.body).to eq("window.location='#{staging_workflow_path(project.staging)}'") }
end
end
end

0 comments on commit 0f26b2e

Please sign in to comment.