debian-mirror-gitlab/app/controllers/concerns/issuable_actions.rb

96 lines
2.4 KiB
Ruby
Raw Normal View History

2016-06-02 11:05:42 +05:30
module IssuableActions
extend ActiveSupport::Concern
included do
2016-11-03 12:29:30 +05:30
before_action :labels, only: [:show, :new, :edit]
2016-06-02 11:05:42 +05:30
before_action :authorize_destroy_issuable!, only: :destroy
2016-09-29 09:46:39 +05:30
before_action :authorize_admin_issuable!, only: :bulk_update
2016-06-02 11:05:42 +05:30
end
def destroy
issuable.destroy
2016-09-29 09:46:39 +05:30
destroy_method = "destroy_#{issuable.class.name.underscore}".to_sym
TodoService.new.public_send(destroy_method, issuable, current_user)
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
name = issuable.human_class_name
2016-06-02 11:05:42 +05:30
flash[:notice] = "The #{name} was successfully deleted."
2017-09-10 17:25:29 +05:30
index_path = polymorphic_path([@project.namespace.becomes(Namespace), @project, issuable.class])
respond_to do |format|
format.html { redirect_to index_path }
format.json do
render json: {
web_url: index_path
}
end
end
2016-06-02 11:05:42 +05:30
end
2016-09-29 09:46:39 +05:30
def bulk_update
result = Issuable::BulkUpdateService.new(project, current_user, bulk_update_params).execute(resource_name)
quantity = result[:count]
render json: { notice: "#{quantity} #{resource_name.pluralize(quantity)} updated" }
end
2016-06-02 11:05:42 +05:30
private
2017-08-17 22:00:37 +05:30
def render_conflict_response
respond_to do |format|
format.html do
@conflict = true
render :edit
end
format.json do
render json: {
errors: [
"Someone edited this #{issuable.human_class_name} at the same time you did. Please refresh your browser and make sure your changes will not unintentionally remove theirs."
]
}, status: 409
end
end
end
2016-11-03 12:29:30 +05:30
def labels
@labels ||= LabelsFinder.new(current_user, project_id: @project.id).execute
end
2016-06-02 11:05:42 +05:30
def authorize_destroy_issuable!
2016-09-29 09:46:39 +05:30
unless can?(current_user, :"destroy_#{issuable.to_ability_name}", issuable)
2016-06-02 11:05:42 +05:30
return access_denied!
end
end
2016-09-29 09:46:39 +05:30
def authorize_admin_issuable!
unless can?(current_user, :"admin_#{resource_name}", @project)
return access_denied!
end
end
def bulk_update_params
2017-08-17 22:00:37 +05:30
permitted_keys = [
2016-09-29 09:46:39 +05:30
:issuable_ids,
:assignee_id,
:milestone_id,
:state_event,
:subscription_event,
label_ids: [],
add_label_ids: [],
remove_label_ids: []
2017-08-17 22:00:37 +05:30
]
if resource_name == 'issue'
permitted_keys << { assignee_ids: [] }
else
permitted_keys.unshift(:assignee_id)
end
params.require(:update).permit(permitted_keys)
2016-09-29 09:46:39 +05:30
end
def resource_name
@resource_name ||= controller_name.singularize
end
2016-06-02 11:05:42 +05:30
end