Skip to content
labels_controller.rb 3.23 KiB
Newer Older
class Projects::LabelsController < Projects::ApplicationController
  include ToggleSubscriptionAction

  before_action :module_enabled
  before_action :label, only: [:edit, :update, :destroy]
  before_action :authorize_read_label!
  before_action :authorize_admin_labels!, only: [
    :new, :create, :edit, :update, :generate, :destroy, :remove_priority, :set_priorities

  respond_to :js, :html

  def index
Alfredo Sumaran's avatar
Alfredo Sumaran committed
    @labels = @project.labels.unprioritized.page(params[:page])
    @prioritized_labels = @project.labels.prioritized

    respond_to do |format|
      format.html
      format.json do
        render json: LabelsFinder.new(current_user, project_id: @project.id).execute
  def new
    @label = @project.labels.new
  end

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

    if @label.valid?
      respond_to do |format|
        format.html { redirect_to namespace_project_labels_path(@project.namespace, @project) }
        format.json { render json: @label }
      end
      respond_to do |format|
        format.html { render 'new' }
        format.json { render json: { message: @label.errors.messages }, status: 400 }
      end
    end
  end

  def edit
  end

  def update
    if @label.update_attributes(label_params)
Vinnie Okada's avatar
Vinnie Okada committed
      redirect_to namespace_project_labels_path(@project.namespace, @project)
    if params[:redirect] == 'issues'
Vinnie Okada's avatar
Vinnie Okada committed
      redirect_to namespace_project_issues_path(@project.namespace, @project)
    elsif params[:redirect] == 'merge_requests'
Vinnie Okada's avatar
Vinnie Okada committed
      redirect_to namespace_project_merge_requests_path(@project.namespace,
                                                        @project)
Vinnie Okada's avatar
Vinnie Okada committed
      redirect_to namespace_project_labels_path(@project.namespace, @project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
  def destroy
    @label.destroy

    respond_to do |format|
Vinnie Okada's avatar
Vinnie Okada committed
      format.html do
        redirect_to(namespace_project_labels_path(@project.namespace, @project),
                    notice: 'Label was removed')
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
  end

  def remove_priority
    respond_to do |format|
      if label.update_attribute(:priority, nil)
        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

Alfredo Sumaran's avatar
Alfredo Sumaran committed
  def set_priorities
    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

    respond_to do |format|
      format.json { render json: { message: 'success' } }
  protected

  def module_enabled
    unless @project.feature_available?(:issues, current_user) || @project.feature_available?(:merge_requests, current_user)
      return render_404
    end
Alfredo Sumaran's avatar
Alfredo Sumaran committed
    params.require(:label).permit(:title, :description, :color)
    @label ||= @project.labels.find(params[:id])
  alias_method :subscribable_resource, :label

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