BigW Consortium Gitlab

labels_controller.rb 2.89 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
  before_action :authorize_admin_labels!, only: [
8
    :new, :create, :edit, :update, :generate, :destroy, :remove_priority, :set_priorities
9
  ]
randx committed
10 11 12 13

  respond_to :js, :html

  def index
14 15
    @labels = @project.labels.unprioritized.page(params[:page])
    @prioritized_labels = @project.labels.prioritized
16 17 18 19

    respond_to do |format|
      format.html
      format.json do
Phil Hughes committed
20
        render json: @project.labels
21 22
      end
    end
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 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 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 committed
54
      redirect_to namespace_project_issues_path(@project.namespace, @project)
55
    elsif params[:redirect] == 'merge_requests'
Vinnie Okada committed
56 57
      redirect_to namespace_project_merge_requests_path(@project.namespace,
                                                        @project)
58
    else
Vinnie Okada committed
59
      redirect_to namespace_project_labels_path(@project.namespace, @project)
60
    end
61 62
  end

Dmitriy Zaporozhets committed
63 64 65
  def destroy
    @label.destroy

66
    respond_to do |format|
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 committed
73 74
  end

75
  def remove_priority
76
    respond_to do |format|
77
      if label.update_attribute(:priority, nil)
78 79 80 81 82 83 84 85
        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

86
  def set_priorities
87 88 89 90 91 92 93
    Label.transaction do
      params[:label_ids].each_with_index do |label_id, index|
        label = @project.labels.find_by_id(label_id)
        label.update_attribute(:priority, index) if label
      end
    end

94
    respond_to do |format|
95
      format.json { render json: { message: 'success' } }
96 97 98
    end
  end

randx committed
99 100 101
  protected

  def module_enabled
102 103 104
    unless @project.issues_enabled || @project.merge_requests_enabled
      return render_404
    end
randx committed
105
  end
106 107

  def label_params
108
    params.require(:label).permit(:title, :description, :color)
109 110 111
  end

  def label
112
    @label ||= @project.labels.find(params[:id])
113
  end
114
  alias_method :subscribable_resource, :label
115 116 117 118

  def authorize_admin_labels!
    return render_404 unless can?(current_user, :admin_label, @project)
  end
randx committed
119
end