gitlab-org--gitlab-foss/app/controllers/groups/labels_controller.rb

117 lines
2.6 KiB
Ruby
Raw Normal View History

2016-09-19 15:49:08 -04:00
class Groups::LabelsController < Groups::ApplicationController
include ToggleSubscriptionAction
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]
before_action :save_previous_label_path, only: [:edit]
2016-09-19 15:49:08 -04:00
respond_to :html
def index
respond_to do |format|
format.html do
@labels = @group.labels.page(params[:page])
end
format.json do
available_labels = LabelsFinder.new(
current_user,
group_id: @group.id,
only_group_labels: params[:only_group_labels],
include_ancestor_groups: params[:include_ancestor_groups],
include_descendant_groups: params[:include_descendant_groups]
).execute
2017-05-05 18:47:32 -04:00
render json: LabelSerializer.new.represent_appearance(available_labels)
end
end
2016-09-19 15:49:08 -04:00
end
def new
@label = @group.labels.new
@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
@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?
redirect_back_or_group_labels_path
2016-09-19 15:49:08 -04:00
else
render :edit
end
end
def destroy
@label.destroy
respond_to do |format|
format.html do
redirect_to group_labels_path(@group), status: 302, notice: "#{@label.name} deleted permanently"
2016-09-19 15:49:08 -04:00
end
format.js
end
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
@label ||= @group.labels.find(params[:id])
end
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
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
2016-09-19 15:49:08 -04:00
end