labels_controller.rb 2.68 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 15
    @labels = @project.labels.prioritized(false).page(params[:page])
    @prioritized = @project.labels.prioritized
16 17 18 19

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

25 26 27 28 29 30 31 32
  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
33
      redirect_to namespace_project_labels_path(@project.namespace, @project)
34 35 36 37 38 39 40 41 42 43
    else
      render 'new'
    end
  end

  def edit
  end

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

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

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

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

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

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  def toggle_priority
    priority = label.priority

    respond_to do |format|
      if label.update_attributes(priority: !priority)
        format.json { render json: label }
      else
        message = label.errors.full_messages.uniq.join('. ')
        format.json { render json: { message: message }, status: :unprocessable_entity }
      end
    end
  end

  def set_sorting
    respond_to do |format|
      format.json { render json: {message: 'success'}}
    end
  end

randx's avatar
randx committed
94 95 96
  protected

  def module_enabled
97 98 99
    unless @project.issues_enabled || @project.merge_requests_enabled
      return render_404
    end
randx's avatar
randx committed
100
  end
101 102

  def label_params
Thijs Wouters's avatar
Thijs Wouters committed
103
    params.require(:label).permit(:title, :description, :color, :priority)
104 105 106
  end

  def label
107
    @label ||= @project.labels.find(params[:id])
108
  end
109
  alias_method :subscribable_resource, :label
110 111 112 113

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