2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
class Groups::LabelsController < Groups::ApplicationController
|
2017-08-17 22:00:37 +05:30
|
|
|
include ToggleSubscriptionAction
|
2021-01-03 14:25:43 +05:30
|
|
|
include ShowInheritedLabelsChecker
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
before_action :label, only: [:edit, :update, :destroy]
|
|
|
|
before_action :authorize_admin_labels!, only: [:new, :create, :edit, :update, :destroy]
|
|
|
|
before_action :save_previous_label_path, only: [:edit]
|
|
|
|
|
|
|
|
respond_to :html
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
feature_category :issue_tracking
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def index
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2021-01-03 14:25:43 +05:30
|
|
|
# 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
|
|
|
|
@labels = available_labels(params.merge(only_group_labels: true)).page(params[:page])
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
format.json do
|
2018-11-20 20:47:30 +05:30
|
|
|
render json: LabelSerializer.new.represent_appearance(available_labels)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@label = @group.labels.new
|
|
|
|
@previous_labels_path = previous_labels_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-08-17 22:00:37 +05:30
|
|
|
@label = Labels::CreateService.new(label_params).execute(group: group)
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
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-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
@previous_labels_path = previous_labels_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2017-08-17 22:00:37 +05:30
|
|
|
@label = Labels::UpdateService.new(label_params).execute(@label)
|
|
|
|
|
|
|
|
if @label.valid?
|
2016-11-03 12:29:30 +05:30
|
|
|
redirect_back_or_group_labels_path
|
|
|
|
else
|
|
|
|
render :edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@label.destroy
|
2021-01-03 14:25:43 +05:30
|
|
|
redirect_to group_labels_path(@group), status: :found, notice: "#{@label.name} deleted permanently"
|
2016-11-03 12:29:30 +05:30
|
|
|
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
|
2021-01-03 14:25:43 +05:30
|
|
|
@label ||= available_labels(params.merge(only_group_labels: true)).find(params[:id])
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
alias_method :subscribable_resource, :label
|
|
|
|
|
|
|
|
def subscribable_project
|
|
|
|
nil
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
def label_params
|
|
|
|
params.require(:label).permit(:title, :description, :color)
|
|
|
|
end
|
|
|
|
|
|
|
|
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-11-08 19:23:39 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def available_labels(options = params)
|
2018-11-08 19:23:39 +05:30
|
|
|
@available_labels ||=
|
|
|
|
LabelsFinder.new(
|
|
|
|
current_user,
|
|
|
|
group_id: @group.id,
|
2021-01-03 14:25:43 +05:30
|
|
|
only_group_labels: options[:only_group_labels],
|
|
|
|
include_ancestor_groups: show_inherited_labels?(params[:include_ancestor_groups]),
|
|
|
|
sort: sort,
|
|
|
|
subscribed: options[:subscribed],
|
|
|
|
include_descendant_groups: options[:include_descendant_groups],
|
|
|
|
search: options[:search]).execute
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
def sort
|
|
|
|
@sort ||= params[:sort] || 'name_asc'
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|