diff options
author | Phil Hughes <me@iamphill.com> | 2016-10-19 22:33:34 +0100 |
---|---|---|
committer | Phil Hughes <me@iamphill.com> | 2016-10-19 22:33:34 +0100 |
commit | fcf0a4a12d1ca17438a99c2b7bf334b82fa0e26e (patch) | |
tree | 8e109ee1d989f741fd7c1b0fd26cfdbf9a545b31 /app/controllers/groups/labels_controller.rb | |
parent | cd5e83b6d6da3bddbc44334a1bcdbac287b35fb4 (diff) | |
parent | c08435e3c25f0a7a705ed8a49b16dde176b41a40 (diff) | |
download | gitlab-ce-fcf0a4a12d1ca17438a99c2b7bf334b82fa0e26e.tar.gz |
Merge branch 'master' into issue-board-sidebar
Diffstat (limited to 'app/controllers/groups/labels_controller.rb')
-rw-r--r-- | app/controllers/groups/labels_controller.rb | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/app/controllers/groups/labels_controller.rb b/app/controllers/groups/labels_controller.rb new file mode 100644 index 00000000000..29528b2cfaa --- /dev/null +++ b/app/controllers/groups/labels_controller.rb @@ -0,0 +1,92 @@ +class Groups::LabelsController < Groups::ApplicationController + before_action :label, only: [:edit, :update, :destroy] + before_action :authorize_admin_labels!, only: [:new, :create, :edit, :update, :destroy] + before_action :save_previous_label_path, only: [:edit] + + respond_to :html + + def index + respond_to do |format| + format.html do + @labels = @group.labels.page(params[:page]) + end + + format.json do + available_labels = LabelsFinder.new(current_user, group_id: @group.id).execute + render json: available_labels.as_json(only: [:id, :title, :color]) + end + end + end + + def new + @label = @group.labels.new + @previous_labels_path = previous_labels_path + end + + def create + @label = @group.labels.create(label_params) + + if @label.valid? + redirect_to group_labels_path(@group) + else + render :new + end + end + + def edit + @previous_labels_path = previous_labels_path + end + + def update + if @label.update_attributes(label_params) + redirect_back_or_group_labels_path + else + render :edit + end + end + + def destroy + @label.destroy + + respond_to do |format| + format.html do + redirect_to group_labels_path(@group), notice: 'Label was removed' + 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 + + def label_params + params.require(:label).permit(:title, :description, :color) + end + + 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 +end |