labels_controller.rb 2.08 KB
Newer Older
1
class Projects::LabelsController < Projects::ApplicationController
2
  before_action :module_enabled
3
  before_action :label, only: [:edit, :update, :destroy, :toggle_subscription]
4
  before_action :authorize_read_label!
5
  before_action :authorize_admin_labels!, except: [:index, :toggle_subscription]
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

63
  def toggle_subscription
64 65
    return unless current_user

66 67 68 69
    @label.toggle_subscription(current_user)
    render nothing: true
  end

randx's avatar
randx committed
70 71 72
  protected

  def module_enabled
73 74 75
    unless @project.issues_enabled || @project.merge_requests_enabled
      return render_404
    end
randx's avatar
randx committed
76
  end
77 78

  def label_params
Tap's avatar
Tap committed
79
    params.require(:label).permit(:title, :description, :color)
80 81 82 83 84 85 86 87 88
  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
89
end