From fe96a1f268558526fd122a348866fbf513ac17e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Fri, 12 Jan 2018 22:39:42 +0100 Subject: Stub multiple variable controller method --- app/controllers/projects/variables_controller.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index 6a825137564..b7b88830837 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -32,6 +32,10 @@ class Projects::VariablesController < Projects::ApplicationController end end + def save_multiple + head :ok + end + def destroy if variable.destroy redirect_to project_settings_ci_cd_path(project), -- cgit v1.2.1 From 121d84d774e18b27a8a4624f173e97cfad0d7f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Sat, 13 Jan 2018 01:46:43 +0100 Subject: Implement multiple variable handling action --- app/controllers/projects/variables_controller.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index b7b88830837..f9d548a14f8 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -33,7 +33,20 @@ class Projects::VariablesController < Projects::ApplicationController end def save_multiple - head :ok + respond_to do |format| + format.json do + variables = [] + variables_params[:variables].each do |variable_hash| + variable = project.variables.where(key: variable_hash[:key]).first_or_initialize(variable_hash) + variable.assign_attributes(variable_hash) unless variable.new_record? + return head :bad_request unless variable.valid? + + variables << variable + end + variables.each { |variable| variable.save } + end + head :ok + end end def destroy @@ -54,6 +67,10 @@ class Projects::VariablesController < Projects::ApplicationController params.require(:variable).permit(*variable_params_attributes) end + def variables_params + params.permit(variables: [*variable_params_attributes]) + end + def variable_params_attributes %i[id key value protected _destroy] end -- cgit v1.2.1 From b91539d68fa21979001e122c912fad1c31dfd5d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Sat, 13 Jan 2018 02:10:04 +0100 Subject: Refactor VariablesController#save_multiple --- app/controllers/projects/variables_controller.rb | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index f9d548a14f8..9aacb53078a 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -35,17 +35,12 @@ class Projects::VariablesController < Projects::ApplicationController def save_multiple respond_to do |format| format.json do - variables = [] - variables_params[:variables].each do |variable_hash| - variable = project.variables.where(key: variable_hash[:key]).first_or_initialize(variable_hash) - variable.assign_attributes(variable_hash) unless variable.new_record? - return head :bad_request unless variable.valid? + variables = variables_from_params(variables_params) + return head :bad_request unless variables.all?(&:valid?) - variables << variable - end - variables.each { |variable| variable.save } + variables.each(&:save) + head :ok end - head :ok end end @@ -71,6 +66,15 @@ class Projects::VariablesController < Projects::ApplicationController params.permit(variables: [*variable_params_attributes]) end + def variables_from_params(params) + params[:variables].map do |variable_hash| + variable = project.variables.where(key: variable_hash[:key]) + .first_or_initialize(variable_hash) + variable.assign_attributes(variable_hash) unless variable.new_record? + variable + end + end + def variable_params_attributes %i[id key value protected _destroy] end -- cgit v1.2.1 From c64181ce4c57bc7ffbbcc51ee9bf0001bf83e1b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Sat, 13 Jan 2018 02:52:49 +0100 Subject: Move variable loading into before_action --- app/controllers/projects/variables_controller.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index 9aacb53078a..cd68018e033 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -1,5 +1,6 @@ class Projects::VariablesController < Projects::ApplicationController before_action :variable, only: [:show, :update, :destroy] + before_action :variables, only: [:save_multiple] before_action :authorize_admin_build! layout 'project_settings' @@ -35,10 +36,9 @@ class Projects::VariablesController < Projects::ApplicationController def save_multiple respond_to do |format| format.json do - variables = variables_from_params(variables_params) - return head :bad_request unless variables.all?(&:valid?) + return head :bad_request unless @variables.all?(&:valid?) - variables.each(&:save) + @variables.each(&:save) head :ok end end @@ -66,15 +66,6 @@ class Projects::VariablesController < Projects::ApplicationController params.permit(variables: [*variable_params_attributes]) end - def variables_from_params(params) - params[:variables].map do |variable_hash| - variable = project.variables.where(key: variable_hash[:key]) - .first_or_initialize(variable_hash) - variable.assign_attributes(variable_hash) unless variable.new_record? - variable - end - end - def variable_params_attributes %i[id key value protected _destroy] end @@ -82,4 +73,13 @@ class Projects::VariablesController < Projects::ApplicationController def variable @variable ||= project.variables.find(params[:id]).present(current_user: current_user) end + + def variables + @variables = variables_params[:variables].map do |variable_hash| + variable = project.variables.where(key: variable_hash[:key]) + .first_or_initialize(variable_hash).present(current_user: current_user) + variable.assign_attributes(variable_hash) unless variable.new_record? + variable + end + end end -- cgit v1.2.1 From ba077841922089c0eb2bbb48947de8828f891776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Sat, 13 Jan 2018 03:36:04 +0100 Subject: Add destroy functionality to save_multiple --- app/controllers/projects/variables_controller.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index cd68018e033..dd514d500d0 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -39,6 +39,7 @@ class Projects::VariablesController < Projects::ApplicationController return head :bad_request unless @variables.all?(&:valid?) @variables.each(&:save) + @variables_destroy.each(&:destroy) head :ok end end @@ -75,10 +76,16 @@ class Projects::VariablesController < Projects::ApplicationController end def variables - @variables = variables_params[:variables].map do |variable_hash| + destroy, edit = variables_params[:variables].partition { |hash| hash[:_destroy] == 'true' } + @variables = initialize_or_update_variables_from_hash(edit) + @variables_destroy = initialize_or_update_variables_from_hash(destroy) + end + + def initialize_or_update_variables_from_hash(hash) + hash.map do |variable_hash| variable = project.variables.where(key: variable_hash[:key]) .first_or_initialize(variable_hash).present(current_user: current_user) - variable.assign_attributes(variable_hash) unless variable.new_record? + variable.assign_attributes(variable_hash.except(:_destroy)) unless variable.new_record? variable end end -- cgit v1.2.1 From 5de85708ce17c1965581b8bf7563751dda77510d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Tue, 23 Jan 2018 01:17:40 +0100 Subject: Use nested attributes for updating multiple variables --- app/controllers/projects/variables_controller.rb | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index dd514d500d0..f916c545fab 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -1,6 +1,5 @@ class Projects::VariablesController < Projects::ApplicationController before_action :variable, only: [:show, :update, :destroy] - before_action :variables, only: [:save_multiple] before_action :authorize_admin_build! layout 'project_settings' @@ -36,11 +35,9 @@ class Projects::VariablesController < Projects::ApplicationController def save_multiple respond_to do |format| format.json do - return head :bad_request unless @variables.all?(&:valid?) + return head :ok if @project.update(variables_params) - @variables.each(&:save) - @variables_destroy.each(&:destroy) - head :ok + head :bad_request end end end @@ -64,7 +61,7 @@ class Projects::VariablesController < Projects::ApplicationController end def variables_params - params.permit(variables: [*variable_params_attributes]) + params.permit(variables_attributes: [*variable_params_attributes]) end def variable_params_attributes @@ -74,19 +71,4 @@ class Projects::VariablesController < Projects::ApplicationController def variable @variable ||= project.variables.find(params[:id]).present(current_user: current_user) end - - def variables - destroy, edit = variables_params[:variables].partition { |hash| hash[:_destroy] == 'true' } - @variables = initialize_or_update_variables_from_hash(edit) - @variables_destroy = initialize_or_update_variables_from_hash(destroy) - end - - def initialize_or_update_variables_from_hash(hash) - hash.map do |variable_hash| - variable = project.variables.where(key: variable_hash[:key]) - .first_or_initialize(variable_hash).present(current_user: current_user) - variable.assign_attributes(variable_hash.except(:_destroy)) unless variable.new_record? - variable - end - end end -- cgit v1.2.1 From edbe911b04465f0e6c72e102d083d0e85848a552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Tue, 23 Jan 2018 02:09:36 +0100 Subject: Remove redundant routes in VariablesController --- app/controllers/projects/variables_controller.rb | 51 ------------------------ 1 file changed, 51 deletions(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index f916c545fab..8bee1b97d14 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -1,37 +1,6 @@ class Projects::VariablesController < Projects::ApplicationController - before_action :variable, only: [:show, :update, :destroy] before_action :authorize_admin_build! - layout 'project_settings' - - def index - redirect_to project_settings_ci_cd_path(@project) - end - - def show - end - - def update - if variable.update(variable_params) - redirect_to project_variables_path(project), - notice: 'Variable was successfully updated.' - else - render "show" - end - end - - def create - @variable = project.variables.create(variable_params) - .present(current_user: current_user) - - if @variable.persisted? - redirect_to project_settings_ci_cd_path(project), - notice: 'Variable was successfully created.' - else - render "show" - end - end - def save_multiple respond_to do |format| format.json do @@ -42,24 +11,8 @@ class Projects::VariablesController < Projects::ApplicationController end end - def destroy - if variable.destroy - redirect_to project_settings_ci_cd_path(project), - status: 302, - notice: 'Variable was successfully removed.' - else - redirect_to project_settings_ci_cd_path(project), - status: 302, - notice: 'Failed to remove the variable.' - end - end - private - def variable_params - params.require(:variable).permit(*variable_params_attributes) - end - def variables_params params.permit(variables_attributes: [*variable_params_attributes]) end @@ -67,8 +20,4 @@ class Projects::VariablesController < Projects::ApplicationController def variable_params_attributes %i[id key value protected _destroy] end - - def variable - @variable ||= project.variables.find(params[:id]).present(current_user: current_user) - end end -- cgit v1.2.1 From bf2a040cf9fb8e0eb3576732f3cda6fe6326e65d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Thu, 25 Jan 2018 03:50:38 +0100 Subject: Pass validation errors in JSON endpoint --- app/controllers/projects/variables_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index 8bee1b97d14..9c0dad393c3 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -6,7 +6,7 @@ class Projects::VariablesController < Projects::ApplicationController format.json do return head :ok if @project.update(variables_params) - head :bad_request + render status: :bad_request, json: @project.errors.to_hash end end end -- cgit v1.2.1 From 6b82a9ef51f59d37975bd5de48142d1a0a8504de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Thu, 25 Jan 2018 12:59:52 +0100 Subject: Format validation errors as human readable messages --- app/controllers/projects/variables_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index 9c0dad393c3..99dea65927d 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -6,7 +6,7 @@ class Projects::VariablesController < Projects::ApplicationController format.json do return head :ok if @project.update(variables_params) - render status: :bad_request, json: @project.errors.to_hash + render status: :bad_request, json: @project.errors.full_messages end end end -- cgit v1.2.1 From 0bfcdd66bf932c080398ff264323b5c0df17d05c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Mon, 29 Jan 2018 18:39:06 +0100 Subject: Use `resource` in Project Variables routing scheme --- app/controllers/projects/variables_controller.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index 99dea65927d..2f03603bd1d 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -1,7 +1,18 @@ class Projects::VariablesController < Projects::ApplicationController before_action :authorize_admin_build! - def save_multiple + def show + respond_to do |format| + format.json do + variables = @project.variables + .map { |variable| variable.present(current_user: current_user) } + + render status: :ok, json: { variables: variables } + end + end + end + + def update respond_to do |format| format.json do return head :ok if @project.update(variables_params) -- cgit v1.2.1 From b48d8c8ad0bb2874db6b4c9accb3bebd19e9f2c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Tue, 30 Jan 2018 00:44:53 +0100 Subject: Return all variables after UPDATE --- app/controllers/projects/variables_controller.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index 2f03603bd1d..58fa600eb34 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -15,7 +15,12 @@ class Projects::VariablesController < Projects::ApplicationController def update respond_to do |format| format.json do - return head :ok if @project.update(variables_params) + if @project.update(variables_params) + variables = @project.variables + .map { |variable| variable.present(current_user: current_user) } + + return render status: :ok, json: { variables: variables } + end render status: :bad_request, json: @project.errors.full_messages end -- cgit v1.2.1 From 9be519c199b01d4b4b1c69ec4d74a1e99345eb47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Tue, 30 Jan 2018 02:01:53 +0100 Subject: Add VariableSerializer for Ci::Variable --- app/controllers/projects/variables_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index 58fa600eb34..b5635ca1b3b 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -7,7 +7,7 @@ class Projects::VariablesController < Projects::ApplicationController variables = @project.variables .map { |variable| variable.present(current_user: current_user) } - render status: :ok, json: { variables: variables } + render status: :ok, json: { variables: VariableSerializer.new.represent(variables) } end end end @@ -19,7 +19,7 @@ class Projects::VariablesController < Projects::ApplicationController variables = @project.variables .map { |variable| variable.present(current_user: current_user) } - return render status: :ok, json: { variables: variables } + return render status: :ok, json: { variables: VariableSerializer.new.represent(variables) } end render status: :bad_request, json: @project.errors.full_messages -- cgit v1.2.1 From e2c8a2231bcc00e5993411e4ad6c0c0db7e1a297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Tue, 30 Jan 2018 02:14:39 +0100 Subject: Remove usage of VariablePresenter in controller --- app/controllers/projects/variables_controller.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index b5635ca1b3b..18225218a1b 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -4,10 +4,7 @@ class Projects::VariablesController < Projects::ApplicationController def show respond_to do |format| format.json do - variables = @project.variables - .map { |variable| variable.present(current_user: current_user) } - - render status: :ok, json: { variables: VariableSerializer.new.represent(variables) } + render status: :ok, json: { variables: VariableSerializer.new.represent(@project.variables) } end end end @@ -16,10 +13,7 @@ class Projects::VariablesController < Projects::ApplicationController respond_to do |format| format.json do if @project.update(variables_params) - variables = @project.variables - .map { |variable| variable.present(current_user: current_user) } - - return render status: :ok, json: { variables: VariableSerializer.new.represent(variables) } + return render status: :ok, json: { variables: VariableSerializer.new.represent(@project.variables) } end render status: :bad_request, json: @project.errors.full_messages -- cgit v1.2.1 From c95c3ffc9e4f1992929899bfc21440b621cc8daf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Thu, 1 Feb 2018 21:38:41 +0100 Subject: Switch emphasis from controller format to update --- app/controllers/projects/variables_controller.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index 18225218a1b..329e1cdfef0 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -10,13 +10,13 @@ class Projects::VariablesController < Projects::ApplicationController end def update - respond_to do |format| - format.json do - if @project.update(variables_params) - return render status: :ok, json: { variables: VariableSerializer.new.represent(@project.variables) } - end - - render status: :bad_request, json: @project.errors.full_messages + if @project.update(variables_params) + respond_to do |format| + format.json { return render status: :ok, json: { variables: VariableSerializer.new.represent(@project.variables) } } + end + else + respond_to do |format| + format.json { render status: :bad_request, json: @project.errors.full_messages } end end end -- cgit v1.2.1 From efcdc269e03836e78dcc8d460621c948ac02bc24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Tue, 6 Feb 2018 17:56:54 +0100 Subject: Fix static_analysis failure --- app/controllers/projects/variables_controller.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'app/controllers/projects/variables_controller.rb') diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb index 329e1cdfef0..7eb509e2e64 100644 --- a/app/controllers/projects/variables_controller.rb +++ b/app/controllers/projects/variables_controller.rb @@ -12,17 +12,25 @@ class Projects::VariablesController < Projects::ApplicationController def update if @project.update(variables_params) respond_to do |format| - format.json { return render status: :ok, json: { variables: VariableSerializer.new.represent(@project.variables) } } + format.json { return render_variables } end else respond_to do |format| - format.json { render status: :bad_request, json: @project.errors.full_messages } + format.json { render_error } end end end private + def render_variables + render status: :ok, json: { variables: VariableSerializer.new.represent(@project.variables) } + end + + def render_error + render status: :bad_request, json: @project.errors.full_messages + end + def variables_params params.permit(variables_attributes: [*variable_params_attributes]) end -- cgit v1.2.1