labels_controller.rb 1.61 KB
Newer Older
1
class Projects::LabelsController < Projects::ApplicationController
randx's avatar
randx committed
2
  before_filter :module_enabled
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
3
  before_filter :label, only: [:edit, :update, :destroy]
Drew Blessing's avatar
Drew Blessing committed
4
  before_filter :authorize_labels!
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
5
  before_filter :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(20)
randx's avatar
randx committed
11 12
  end

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  def new
    @label = @project.labels.new
  end

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

    if @label.valid?
      redirect_to project_labels_path(@project)
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @label.update_attributes(label_params)
      redirect_to project_labels_path(@project)
    else
      render 'edit'
    end
  end

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

Drew Blessing's avatar
Drew Blessing committed
41 42 43 44
    if params[:redirect] == 'issues'
      redirect_to project_issues_path(@project)
    elsif params[:redirect] == 'merge_requests'
      redirect_to project_merge_requests_path(@project)
45 46
    else
      redirect_to project_labels_path(@project)
Drew Blessing's avatar
Drew Blessing committed
47
    end
48 49
  end

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

53 54
    respond_to do |format|
      format.html { redirect_to project_labels_path(@project), notice: 'Label was removed' }
55
      format.js
56
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
57 58
  end

randx's avatar
randx committed
59 60 61
  protected

  def module_enabled
Drew Blessing's avatar
Drew Blessing committed
62 63 64
    unless @project.issues_enabled || @project.merge_requests_enabled
      return render_404
    end
randx's avatar
randx committed
65
  end
66 67 68 69 70 71 72 73 74 75 76 77

  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
78
end