labels_controller.rb 2.63 KB
Newer Older
1
class Groups::LabelsController < Groups::ApplicationController
2 3
  include ToggleSubscriptionAction

4
  before_action :label, only: [:edit, :update, :destroy]
5
  before_action :authorize_admin_labels!, only: [:new, :create, :edit, :update, :destroy]
6
  before_action :save_previous_label_path, only: [:edit]
7 8 9 10

  respond_to :html

  def index
11 12 13 14 15 16
    respond_to do |format|
      format.html do
        @labels = @group.labels.page(params[:page])
      end

      format.json do
17 18 19 20
        available_labels = LabelsFinder.new(
          current_user,
          group_id: @group.id,
          only_group_labels: params[:only_group_labels],
21 22
          include_ancestor_groups: params[:include_ancestor_groups],
          include_descendant_groups: params[:include_descendant_groups]
23 24
        ).execute

25
        render json: LabelSerializer.new.represent_appearance(available_labels)
26 27
      end
    end
28 29 30 31
  end

  def new
    @label = @group.labels.new
32
    @previous_labels_path = previous_labels_path
33 34 35
  end

  def create
36
    @label = Labels::CreateService.new(label_params).execute(group: group)
37

Felipe Artur's avatar
Felipe Artur committed
38 39 40 41 42 43 44 45 46 47 48 49
    respond_to do |format|
      format.html do
        if @label.valid?
          redirect_to group_labels_path(@group)
        else
          render :new
        end
      end

      format.json do
        render json: LabelSerializer.new.represent_appearance(@label)
      end
50 51 52 53
    end
  end

  def edit
54
    @previous_labels_path = previous_labels_path
55 56 57
  end

  def update
58 59 60
    @label = Labels::UpdateService.new(label_params).execute(@label)

    if @label.valid?
61
      redirect_back_or_group_labels_path
62 63 64 65 66 67 68 69 70 71
    else
      render :edit
    end
  end

  def destroy
    @label.destroy

    respond_to do |format|
      format.html do
72
        redirect_to group_labels_path(@group), status: 302, notice: "#{@label.name} deleted permanently"
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
      end
      format.js
    end
  end

  protected

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

  def authorize_read_labels!
    return render_404 unless can?(current_user, :read_label, @group)
  end

  def label
    @label ||= @group.labels.find(params[:id])
  end
91 92 93 94 95
  alias_method :subscribable_resource, :label

  def subscribable_project
    nil
  end
96 97 98 99

  def label_params
    params.require(:label).permit(:title, :description, :color)
  end
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

  def redirect_back_or_group_labels_path(options = {})
    redirect_to previous_labels_path, options
  end

  def previous_labels_path
    session.fetch(:previous_labels_path, fallback_path)
  end

  def fallback_path
    group_labels_path(@group)
  end

  def save_previous_label_path
    session[:previous_labels_path] = URI(request.referer || '').path
  end
116
end