labels_controller.rb 1.89 KB
Newer Older
1
class Projects::LabelsController < Projects::ApplicationController
2 3 4 5
  before_action :module_enabled
  before_action :label, only: [:edit, :update, :destroy]
  before_action :authorize_labels!
  before_action :authorize_admin_labels!, except: [:index]
randx's avatar
randx committed
6 7 8 9

  respond_to :js, :html

  def index
10
    @labels = @project.labels.page(params[:page]).per(PER_PAGE)
randx's avatar
randx committed
11 12
  end

13 14 15 16 17 18 19 20
  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
21
      redirect_to namespace_project_labels_path(@project.namespace, @project)
22 23 24 25 26 27 28 29 30 31
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @label.update_attributes(label_params)
Vinnie Okada's avatar
Vinnie Okada committed
32
      redirect_to namespace_project_labels_path(@project.namespace, @project)
33 34 35 36 37
    else
      render 'edit'
    end
  end

38
  def generate
39
    Gitlab::IssuesLabels.generate(@project)
40

41
    if params[:redirect] == 'issues'
Vinnie Okada's avatar
Vinnie Okada committed
42
      redirect_to namespace_project_issues_path(@project.namespace, @project)
43
    elsif params[:redirect] == 'merge_requests'
Vinnie Okada's avatar
Vinnie Okada committed
44 45
      redirect_to namespace_project_merge_requests_path(@project.namespace,
                                                        @project)
46
    else
Vinnie Okada's avatar
Vinnie Okada committed
47
      redirect_to namespace_project_labels_path(@project.namespace, @project)
48
    end
49 50
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
51 52 53
  def destroy
    @label.destroy

54
    respond_to do |format|
Vinnie Okada's avatar
Vinnie Okada committed
55 56 57 58
      format.html do
        redirect_to(namespace_project_labels_path(@project.namespace, @project),
                    notice: 'Label was removed')
      end
59
      format.js
60
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
61 62
  end

randx's avatar
randx committed
63 64 65
  protected

  def module_enabled
66 67 68
    unless @project.issues_enabled || @project.merge_requests_enabled
      return render_404
    end
randx's avatar
randx committed
69
  end
70 71 72 73 74 75 76 77 78 79 80 81

  def label_params
    params.require(:label).permit(:title, :color)
  end

  def label
    @label = @project.labels.find(params[:id])
  end

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