labels_controller.rb 2.03 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]).per(PER_PAGE)
randx's avatar
randx committed
15 16
  end

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

  def edit
  end

  def update
    if @label.update_attributes(label_params)
Vinnie Okada's avatar
Vinnie Okada committed
36
      redirect_to namespace_project_labels_path(@project.namespace, @project)
37 38 39 40 41
    else
      render 'edit'
    end
  end

42
  def generate
43
    Gitlab::IssuesLabels.generate(@project)
44

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
55 56 57
  def destroy
    @label.destroy

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

randx's avatar
randx committed
67 68 69
  protected

  def module_enabled
70 71 72
    unless @project.issues_enabled || @project.merge_requests_enabled
      return render_404
    end
randx's avatar
randx committed
73
  end
74 75

  def label_params
Tap's avatar
Tap committed
76
    params.require(:label).permit(:title, :description, :color)
77 78 79
  end

  def label
80
    @label ||= @project.labels.find(params[:id])
81
  end
82
  alias_method :subscribable_resource, :label
83 84 85 86

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