Commit 7342a456 authored by Zeger-Jan van de Weg's avatar Zeger-Jan van de Weg

Dry destroy action on issuables

parent 67043ec5
module IssuableAction
extend ActiveSupport::Concern
def destroy
issuable = @merge_request || @issue
unless current_user.can?(:"remove_#{issuable.to_ability_name}", issuable)
return access_denied!
end
issuable.destroy
route = polymorphic_path([@project.namespace.becomes(Namespace), @project, issuable.class])
issuable_name = issuable.class.name.underscore.gsub('_', ' ')
respond_to do |format|
format.html { redirect_to route, notice: "This #{issuable_name} was deleted." }
format.json { head :ok }
end
end
end
class Projects::IssuesController < Projects::ApplicationController class Projects::IssuesController < Projects::ApplicationController
include ToggleSubscriptionAction include ToggleSubscriptionAction
include IssuableAction
before_action :module_enabled before_action :module_enabled
before_action :issue, only: [:edit, :update, :show] before_action :issue, only: [:edit, :update, :show, :destroy]
# Allow read any issue # Allow read any issue
before_action :authorize_read_issue!, only: [:show] before_action :authorize_read_issue!, only: [:show]
...@@ -108,17 +109,6 @@ class Projects::IssuesController < Projects::ApplicationController ...@@ -108,17 +109,6 @@ class Projects::IssuesController < Projects::ApplicationController
end end
end end
def destroy
return access_denied! unless current_user.admin?
issue.destroy
respond_to do |format|
format.html { redirect_to namespace_project_issues_path(@project.namespace, @project), notice: "The issues was deleted." }
format.json { head :ok }
end
end
def bulk_update def bulk_update
result = Issues::BulkUpdateService.new(project, current_user, bulk_update_params).execute result = Issues::BulkUpdateService.new(project, current_user, bulk_update_params).execute
redirect_back_or_default(default: { action: 'index' }, options: { notice: "#{result[:count]} issues updated" }) redirect_back_or_default(default: { action: 'index' }, options: { notice: "#{result[:count]} issues updated" })
......
class Projects::MergeRequestsController < Projects::ApplicationController class Projects::MergeRequestsController < Projects::ApplicationController
include ToggleSubscriptionAction include ToggleSubscriptionAction
include DiffHelper include DiffHelper
include IssuableAction
before_action :module_enabled before_action :module_enabled
before_action :merge_request, only: [ before_action :merge_request, only: [
:edit, :update, :show, :diffs, :commits, :builds, :merge, :merge_check, :edit, :update, :show, :destroy, :diffs, :commits, :builds, :merge, :merge_check,
:ci_status, :toggle_subscription, :cancel_merge_when_build_succeeds, :remove_wip :ci_status, :toggle_subscription, :cancel_merge_when_build_succeeds, :remove_wip
] ]
before_action :closes_issues, only: [:edit, :update, :show, :diffs, :commits, :builds] before_action :closes_issues, only: [:edit, :update, :show, :diffs, :commits, :builds]
...@@ -164,6 +165,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -164,6 +165,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
end end
end end
<<<<<<< 67043ec53b4c35d5a9862fe78bd3f47e412919cd
def remove_wip def remove_wip
MergeRequests::UpdateService.new(project, current_user, title: @merge_request.wipless_title).execute(@merge_request) MergeRequests::UpdateService.new(project, current_user, title: @merge_request.wipless_title).execute(@merge_request)
...@@ -171,6 +173,8 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -171,6 +173,8 @@ class Projects::MergeRequestsController < Projects::ApplicationController
notice: "The merge request can now be merged." notice: "The merge request can now be merged."
end end
=======
>>>>>>> Dry destroy action on issuables
def merge_check def merge_check
@merge_request.check_if_can_be_merged @merge_request.check_if_can_be_merged
......
...@@ -235,7 +235,9 @@ class Ability ...@@ -235,7 +235,9 @@ class Ability
:rename_project, :rename_project,
:remove_project, :remove_project,
:archive_project, :archive_project,
:remove_fork_project :remove_fork_project,
:remove_merge_request,
:remove_issue
] ]
end end
......
...@@ -7,11 +7,10 @@ module InternalId ...@@ -7,11 +7,10 @@ module InternalId
end end
def set_iid def set_iid
max_iid = if self.paranoid? records = project.send(self.class.name.tableize)
project.send(self.class.name.tableize).with_deleted.maximum(:iid) records = records.with_deleted if self.paranoid?
else max_iid = records.maximum(:iid)
project.send(self.class.name.tableize).maximum(:iid)
end
self.iid = max_iid.to_i + 1 self.iid = max_iid.to_i + 1
end end
......
...@@ -58,6 +58,8 @@ module Issuable ...@@ -58,6 +58,8 @@ module Issuable
attr_mentionable :description, cache: true attr_mentionable :description, cache: true
participant :author, :assignee, :notes_with_associations participant :author, :assignee, :notes_with_associations
strip_attributes :title strip_attributes :title
acts_as_paranoid
end end
module ClassMethods module ClassMethods
......
...@@ -54,8 +54,6 @@ class Issue < ActiveRecord::Base ...@@ -54,8 +54,6 @@ class Issue < ActiveRecord::Base
state :closed state :closed
end end
acts_as_paranoid
def hook_attrs def hook_attrs
attributes attributes
end end
......
...@@ -142,8 +142,6 @@ class MergeRequest < ActiveRecord::Base ...@@ -142,8 +142,6 @@ class MergeRequest < ActiveRecord::Base
scope :join_project, -> { joins(:target_project) } scope :join_project, -> { joins(:target_project) }
scope :references_project, -> { references(:target_project) } scope :references_project, -> { references(:target_project) }
acts_as_paranoid
def self.reference_prefix def self.reference_prefix
'!' '!'
end end
......
...@@ -440,10 +440,6 @@ class User < ActiveRecord::Base ...@@ -440,10 +440,6 @@ class User < ActiveRecord::Base
Project.where("projects.id IN (#{projects_union.to_sql})") Project.where("projects.id IN (#{projects_union.to_sql})")
end end
def owner?(project)
owned_projects.include?(project)
end
def owned_projects def owned_projects
@owned_projects ||= @owned_projects ||=
Project.where('namespace_id IN (?) OR namespace_id = ?', Project.where('namespace_id IN (?) OR namespace_id = ?',
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
- if @merge_request.open? - if @merge_request.open?
= link_to 'Close', merge_request_path(@merge_request, merge_request: { state_event: :close }), method: :put, class: 'btn btn-nr btn-grouped btn-close', title: 'Close merge request' = link_to 'Close', merge_request_path(@merge_request, merge_request: { state_event: :close }), method: :put, class: 'btn btn-nr btn-grouped btn-close', title: 'Close merge request'
= link_to edit_namespace_project_merge_request_path(@project.namespace, @project, @merge_request), class: 'btn btn-nr btn-grouped issuable-edit', id: 'edit_merge_request' do = link_to edit_namespace_project_merge_request_path(@project.namespace, @project, @merge_request), class: 'btn btn-nr btn-grouped issuable-edit', id: 'edit_merge_request' do
=icon('pencil-square-o') #%i.fa.fa-pencil-square-o =icon('pencil-square-o')
Edit Edit
- if @merge_request.closed? - if @merge_request.closed?
= link_to 'Reopen', merge_request_path(@merge_request, merge_request: {state_event: :reopen }), method: :put, class: 'btn btn-nr btn-grouped btn-reopen reopen-mr-link', title: 'Reopen merge request' = link_to 'Reopen', merge_request_path(@merge_request, merge_request: {state_event: :reopen }), method: :put, class: 'btn btn-nr btn-grouped btn-reopen reopen-mr-link', title: 'Reopen merge request'
...@@ -106,10 +106,6 @@ ...@@ -106,10 +106,6 @@
= f.submit "Submit #{issuable.class.model_name.human.downcase}", class: 'btn btn-create' = f.submit "Submit #{issuable.class.model_name.human.downcase}", class: 'btn btn-create'
- else - else
= f.submit 'Save changes', class: 'btn btn-save' = f.submit 'Save changes', class: 'btn btn-save'
- if current_user.admin? || current_user.owner?(@project)
= link_to namespace_project_issue_path(@project.namespace, @project, issuable), method: :delete, class: 'btn' do
= icon('trash-o')
Delete
- if !issuable.persisted? && !issuable.project.empty_repo? && (guide_url = contribution_guide_path(issuable.project)) - if !issuable.persisted? && !issuable.project.empty_repo? && (guide_url = contribution_guide_path(issuable.project))
.inline.prepend-left-10 .inline.prepend-left-10
...@@ -118,7 +114,13 @@ ...@@ -118,7 +114,13 @@
for this project. for this project.
- if issuable.new_record? - if issuable.new_record?
- cancel_project = issuable.source_project = link_to namespace_project_issues_path(@project.namespace, @project), class: 'btn btn-cancel' do
Cancel
- else - else
- cancel_project = issuable.project - if current_user.can?(:"remove_#{issuable.to_ability_name}", @project)
= link_to 'Cancel', [cancel_project.namespace.becomes(Namespace), cancel_project, issuable], class: 'btn btn-cancel' .pull-right
= link_to polymorphic_path([@project.namespace.becomes(Namespace), @project, issuable]), method: :delete, class: 'btn' do
= icon('trash-o')
Delete
= link_to namespace_project_issue_path(@project.namespace, @project, issuable), class: 'btn btn-cancel' do
Cancel
...@@ -328,7 +328,7 @@ Example response: ...@@ -328,7 +328,7 @@ Example response:
## Delete existing issue ## Delete existing issue
Only for admins. Soft deletes the issue in question. Returns the issue which was deleted. Only for admins and project owners. Soft deletes the issue in question. Returns the issue which was deleted.
``` ```
DELETE /projects/:id/issues/:issue_id DELETE /projects/:id/issues/:issue_id
......
...@@ -382,7 +382,7 @@ If an error occurs, an error number and a message explaining the reason is retur ...@@ -382,7 +382,7 @@ If an error occurs, an error number and a message explaining the reason is retur
## Delete a MR ## Delete a MR
Soft deletes a merge request. For admins only. Soft deletes a merge request. For admins and owners only.
``` ```
DELETE /projects/:id/merge_requests/:merge_request_id DELETE /projects/:id/merge_requests/:merge_request_id
......
...@@ -199,11 +199,12 @@ module API ...@@ -199,11 +199,12 @@ module API
# Example Request: # Example Request:
# DELETE /projects/:id/issues/:issue_id # DELETE /projects/:id/issues/:issue_id
delete ":id/issues/:issue_id" do delete ":id/issues/:issue_id" do
authenticated_as_admin! issue = user_project.issues.find(params[:issue_id])
!JLJsdf sdfijsf current_user.can?(:remove_issue, issue)
issue = user_project.issues.find(params[:issue_id]) issue = user_project.issues.find(params[:issue_id])
issue.destroy issue.destroy
present issue, with: Entities::Issue present issue, with: Entities::Issue
end end
end end
......
...@@ -123,7 +123,7 @@ describe Projects::MergeRequestsController do ...@@ -123,7 +123,7 @@ describe Projects::MergeRequestsController do
end end
end end
describe 'GET #index' do describe '#index' do
def get_merge_requests def get_merge_requests
get :index, get :index,
namespace_id: project.namespace.to_param, namespace_id: project.namespace.to_param,
...@@ -157,8 +157,8 @@ describe Projects::MergeRequestsController do ...@@ -157,8 +157,8 @@ describe Projects::MergeRequestsController do
end end
end end
describe "DELETE #destroy" do describe "#destroy" do
it "lets mere mortals not acces this endpoint" do it "lets mere mortals not access this endpoint" do
delete :destroy, namespace_id: project.namespace.path, project_id: project.path, id: merge_request.iid delete :destroy, namespace_id: project.namespace.path, project_id: project.path, id: merge_request.iid
expect(response.status).to eq 404 expect(response.status).to eq 404
...@@ -170,7 +170,7 @@ describe Projects::MergeRequestsController do ...@@ -170,7 +170,7 @@ describe Projects::MergeRequestsController do
user.save user.save
end end
it "lets an admin delete an issue" do it "lets an admin or owner delete an issue" do
delete :destroy, namespace_id: project.namespace.path, project_id: project.path, id: merge_request.iid delete :destroy, namespace_id: project.namespace.path, project_id: project.path, id: merge_request.iid
expect(response.status).to be 302 expect(response.status).to be 302
......
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