From 134fe5af83167f95205a080f7932452de7d77496 Mon Sep 17 00:00:00 2001 From: Timothy Andrew Date: Thu, 7 Jul 2016 13:06:28 +0530 Subject: Use the `{Push,Merge}AccessLevel` models in the UI. 1. Improve error handling while creating protected branches. 2. Modify coffeescript code so that the "Developers can *" checkboxes send a '1' or '0' even when using AJAX. This lets us keep the backend code simpler. 3. Use services for both creating and updating protected branches. Destruction is taken care of with `dependent: :destroy` --- .../projects/protected_branches_controller.rb | 24 +++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'app/controllers/projects/protected_branches_controller.rb') diff --git a/app/controllers/projects/protected_branches_controller.rb b/app/controllers/projects/protected_branches_controller.rb index 10dca47fded..fdbe0044d3c 100644 --- a/app/controllers/projects/protected_branches_controller.rb +++ b/app/controllers/projects/protected_branches_controller.rb @@ -3,19 +3,23 @@ class Projects::ProtectedBranchesController < Projects::ApplicationController before_action :require_non_empty_project before_action :authorize_admin_project! before_action :load_protected_branch, only: [:show, :update, :destroy] + before_action :load_protected_branches, only: [:index, :create] layout "project_settings" def index - @protected_branches = @project.protected_branches.order(:name).page(params[:page]) @protected_branch = @project.protected_branches.new gon.push({ open_branches: @project.open_branches.map { |br| { text: br.name, id: br.name, title: br.name } } }) end def create - @project.protected_branches.create(protected_branch_params) - redirect_to namespace_project_protected_branches_path(@project.namespace, - @project) + service = ProtectedBranches::CreateService.new(@project, current_user, protected_branch_params) + if service.execute + redirect_to namespace_project_protected_branches_path(@project.namespace, @project) + else + @protected_branch = service.protected_branch + render :index + end end def show @@ -23,13 +27,15 @@ class Projects::ProtectedBranchesController < Projects::ApplicationController end def update - if @protected_branch && @protected_branch.update_attributes(protected_branch_params) + service = ProtectedBranches::UpdateService.new(@project, current_user, params[:id], protected_branch_params) + + if service.execute respond_to do |format| - format.json { render json: @protected_branch, status: :ok } + format.json { render json: service.protected_branch, status: :ok } end else respond_to do |format| - format.json { render json: @protected_branch.errors, status: :unprocessable_entity } + format.json { render json: service.protected_branch.errors, status: :unprocessable_entity } end end end @@ -52,4 +58,8 @@ class Projects::ProtectedBranchesController < Projects::ApplicationController def protected_branch_params params.require(:protected_branch).permit(:name, :developers_can_push, :developers_can_merge) end + + def load_protected_branches + @protected_branches = @project.protected_branches.order(:name).page(params[:page]) + end end -- cgit v1.2.3