labels_controller.rb 2.14 KB
Newer Older
1
class Projects::LabelsController < Projects::ApplicationController
2 3
  include ToggleSubscriptionAction

4
  before_action :module_enabled
5
  before_action :label, only: [:edit, :update, :destroy]
6
  before_action :authorize_read_label!
7 8 9
  before_action :authorize_admin_labels!, only: [
    :new, :create, :edit, :update, :generate, :destroy
  ]
randx's avatar
randx committed
10 11 12 13

  respond_to :js, :html

  def index
14
    @labels = @project.labels.page(params[:page])
15 16 17 18

    respond_to do |format|
      format.html
      format.json do
Phil Hughes's avatar
Phil Hughes committed
19
        render json: @project.labels
20 21
      end
    end
randx's avatar
randx committed
22 23
  end

24 25 26 27 28 29 30 31
  def new
    @label = @project.labels.new
  end

  def create
    @label = @project.labels.create(label_params)

    if @label.valid?
Vinnie Okada's avatar
Vinnie Okada committed
32
      redirect_to namespace_project_labels_path(@project.namespace, @project)
33 34 35 36 37 38 39 40 41 42
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @label.update_attributes(label_params)
Vinnie Okada's avatar
Vinnie Okada committed
43
      redirect_to namespace_project_labels_path(@project.namespace, @project)
44 45 46 47 48
    else
      render 'edit'
    end
  end

49
  def generate
50
    Gitlab::IssuesLabels.generate(@project)
51

52
    if params[:redirect] == 'issues'
Vinnie Okada's avatar
Vinnie Okada committed
53
      redirect_to namespace_project_issues_path(@project.namespace, @project)
54
    elsif params[:redirect] == 'merge_requests'
Vinnie Okada's avatar
Vinnie Okada committed
55 56
      redirect_to namespace_project_merge_requests_path(@project.namespace,
                                                        @project)
57
    else
Vinnie Okada's avatar
Vinnie Okada committed
58
      redirect_to namespace_project_labels_path(@project.namespace, @project)
59
    end
60 61
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
62 63 64
  def destroy
    @label.destroy

65
    respond_to do |format|
Vinnie Okada's avatar
Vinnie Okada committed
66 67 68 69
      format.html do
        redirect_to(namespace_project_labels_path(@project.namespace, @project),
                    notice: 'Label was removed')
      end
70
      format.js
71
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
72 73
  end

randx's avatar
randx committed
74 75 76
  protected

  def module_enabled
77 78 79
    unless @project.issues_enabled || @project.merge_requests_enabled
      return render_404
    end
randx's avatar
randx committed
80
  end
81 82

  def label_params
Tap's avatar
Tap committed
83
    params.require(:label).permit(:title, :description, :color)
84 85 86
  end

  def label
87
    @label ||= @project.labels.find(params[:id])
88
  end
89
  alias_method :subscribable_resource, :label
90 91 92 93

  def authorize_admin_labels!
    return render_404 unless can?(current_user, :admin_label, @project)
  end
randx's avatar
randx committed
94
end