2018-09-23 15:44:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-19 15:49:08 -04:00
|
|
|
class Groups::LabelsController < Groups::ApplicationController
|
2016-11-14 17:42:44 -05:00
|
|
|
include ToggleSubscriptionAction
|
|
|
|
|
2016-10-19 09:31:08 -04:00
|
|
|
before_action :label, only: [:edit, :update, :destroy]
|
2016-09-19 15:49:08 -04:00
|
|
|
before_action :authorize_admin_labels!, only: [:new, :create, :edit, :update, :destroy]
|
2016-09-19 23:09:57 -04:00
|
|
|
before_action :save_previous_label_path, only: [:edit]
|
2016-09-19 15:49:08 -04:00
|
|
|
|
|
|
|
respond_to :html
|
|
|
|
|
2020-10-06 08:08:38 -04:00
|
|
|
feature_category :issue_tracking
|
|
|
|
|
2016-09-19 15:49:08 -04:00
|
|
|
def index
|
2018-04-25 06:56:54 -04:00
|
|
|
respond_to do |format|
|
2018-05-17 11:14:17 -04:00
|
|
|
format.html do
|
2020-09-25 05:09:40 -04:00
|
|
|
# at group level we do not want to list project labels,
|
|
|
|
# we only want `only_group_labels = false` when pulling labels for label filter dropdowns, fetched through json
|
2021-04-07 11:09:18 -04:00
|
|
|
@labels = available_labels(params.merge(only_group_labels: true)).page(params[:page]) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
Preloaders::LabelsPreloader.new(@labels, current_user).preload_all
|
2018-05-17 11:14:17 -04:00
|
|
|
end
|
2016-09-20 10:55:31 -04:00
|
|
|
format.json do
|
2018-09-07 08:26:32 -04:00
|
|
|
render json: LabelSerializer.new.represent_appearance(available_labels)
|
2016-09-20 10:55:31 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-19 15:49:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@label = @group.labels.new
|
2016-10-10 23:04:15 -04:00
|
|
|
@previous_labels_path = previous_labels_path
|
2016-09-19 15:49:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-03-29 07:45:15 -04:00
|
|
|
@label = Labels::CreateService.new(label_params).execute(group: group)
|
2016-09-19 15:49:08 -04:00
|
|
|
|
2018-02-19 14:06:16 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
if @label.valid?
|
|
|
|
redirect_to group_labels_path(@group)
|
|
|
|
else
|
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
|
|
|
render json: LabelSerializer.new.represent_appearance(@label)
|
|
|
|
end
|
2016-09-19 15:49:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2016-09-19 23:09:57 -04:00
|
|
|
@previous_labels_path = previous_labels_path
|
2016-09-19 15:49:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2017-03-29 07:45:15 -04:00
|
|
|
@label = Labels::UpdateService.new(label_params).execute(@label)
|
|
|
|
|
|
|
|
if @label.valid?
|
2016-09-19 23:09:57 -04:00
|
|
|
redirect_back_or_group_labels_path
|
2016-09-19 15:49:08 -04:00
|
|
|
else
|
|
|
|
render :edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@label.destroy
|
2020-09-23 08:09:58 -04:00
|
|
|
redirect_to group_labels_path(@group), status: :found, notice: "#{@label.name} deleted permanently"
|
2016-09-19 15:49:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def authorize_admin_labels!
|
|
|
|
return render_404 unless can?(current_user, :admin_label, @group)
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_read_labels!
|
|
|
|
return render_404 unless can?(current_user, :read_label, @group)
|
|
|
|
end
|
|
|
|
|
|
|
|
def label
|
2020-09-25 05:09:40 -04:00
|
|
|
@label ||= available_labels(params.merge(only_group_labels: true)).find(params[:id])
|
2016-09-19 15:49:08 -04:00
|
|
|
end
|
2016-11-14 17:42:44 -05:00
|
|
|
alias_method :subscribable_resource, :label
|
|
|
|
|
|
|
|
def subscribable_project
|
|
|
|
nil
|
|
|
|
end
|
2016-09-19 15:49:08 -04:00
|
|
|
|
|
|
|
def label_params
|
|
|
|
params.require(:label).permit(:title, :description, :color)
|
|
|
|
end
|
2016-09-19 23:09:57 -04:00
|
|
|
|
|
|
|
def redirect_back_or_group_labels_path(options = {})
|
|
|
|
redirect_to previous_labels_path, options
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous_labels_path
|
|
|
|
session.fetch(:previous_labels_path, fallback_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fallback_path
|
|
|
|
group_labels_path(@group)
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_previous_label_path
|
|
|
|
session[:previous_labels_path] = URI(request.referer || '').path
|
|
|
|
end
|
2018-04-25 06:56:54 -04:00
|
|
|
|
2020-09-25 05:09:40 -04:00
|
|
|
def available_labels(options = params)
|
2018-04-25 06:56:54 -04:00
|
|
|
@available_labels ||=
|
|
|
|
LabelsFinder.new(
|
|
|
|
current_user,
|
|
|
|
group_id: @group.id,
|
2020-09-25 05:09:40 -04:00
|
|
|
only_group_labels: options[:only_group_labels],
|
2020-12-23 01:10:22 -05:00
|
|
|
include_ancestor_groups: true,
|
2020-09-25 05:09:40 -04:00
|
|
|
sort: sort,
|
|
|
|
subscribed: options[:subscribed],
|
|
|
|
include_descendant_groups: options[:include_descendant_groups],
|
|
|
|
search: options[:search]).execute
|
2018-04-25 06:56:54 -04:00
|
|
|
end
|
2018-09-10 07:38:29 -04:00
|
|
|
|
|
|
|
def sort
|
|
|
|
@sort ||= params[:sort] || 'name_asc'
|
|
|
|
end
|
2016-09-19 15:49:08 -04:00
|
|
|
end
|