Use nested attributes for updating multiple variables
This commit is contained in:
parent
ba07784192
commit
5de85708ce
|
@ -1,6 +1,5 @@
|
||||||
class Projects::VariablesController < Projects::ApplicationController
|
class Projects::VariablesController < Projects::ApplicationController
|
||||||
before_action :variable, only: [:show, :update, :destroy]
|
before_action :variable, only: [:show, :update, :destroy]
|
||||||
before_action :variables, only: [:save_multiple]
|
|
||||||
before_action :authorize_admin_build!
|
before_action :authorize_admin_build!
|
||||||
|
|
||||||
layout 'project_settings'
|
layout 'project_settings'
|
||||||
|
@ -36,11 +35,9 @@ class Projects::VariablesController < Projects::ApplicationController
|
||||||
def save_multiple
|
def save_multiple
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.json do
|
format.json do
|
||||||
return head :bad_request unless @variables.all?(&:valid?)
|
return head :ok if @project.update(variables_params)
|
||||||
|
|
||||||
@variables.each(&:save)
|
head :bad_request
|
||||||
@variables_destroy.each(&:destroy)
|
|
||||||
head :ok
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -64,7 +61,7 @@ class Projects::VariablesController < Projects::ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def variables_params
|
def variables_params
|
||||||
params.permit(variables: [*variable_params_attributes])
|
params.permit(variables_attributes: [*variable_params_attributes])
|
||||||
end
|
end
|
||||||
|
|
||||||
def variable_params_attributes
|
def variable_params_attributes
|
||||||
|
@ -74,19 +71,4 @@ class Projects::VariablesController < Projects::ApplicationController
|
||||||
def variable
|
def variable
|
||||||
@variable ||= project.variables.find(params[:id]).present(current_user: current_user)
|
@variable ||= project.variables.find(params[:id]).present(current_user: current_user)
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -67,8 +67,8 @@ describe Projects::VariablesController do
|
||||||
subject do
|
subject do
|
||||||
post :save_multiple,
|
post :save_multiple,
|
||||||
namespace_id: project.namespace.to_param, project_id: project,
|
namespace_id: project.namespace.to_param, project_id: project,
|
||||||
variables: [{ key: variable.key, value: 'other_value' },
|
variables_attributes: [{ id: variable.id, key: variable.key, value: 'other_value' },
|
||||||
{ key: '..?', value: 'dummy_value' }],
|
{ key: '..?', value: 'dummy_value' }],
|
||||||
format: :json
|
format: :json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -91,8 +91,8 @@ describe Projects::VariablesController do
|
||||||
subject do
|
subject do
|
||||||
post :save_multiple,
|
post :save_multiple,
|
||||||
namespace_id: project.namespace.to_param, project_id: project,
|
namespace_id: project.namespace.to_param, project_id: project,
|
||||||
variables: [{ key: variable.key, value: 'other_value' },
|
variables_attributes: [{ id: variable.id, key: variable.key, value: 'other_value' },
|
||||||
{ key: 'new_key', value: 'dummy_value' }],
|
{ key: 'new_key', value: 'dummy_value' }],
|
||||||
format: :json
|
format: :json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -115,7 +115,8 @@ describe Projects::VariablesController do
|
||||||
subject do
|
subject do
|
||||||
post :save_multiple,
|
post :save_multiple,
|
||||||
namespace_id: project.namespace.to_param, project_id: project,
|
namespace_id: project.namespace.to_param, project_id: project,
|
||||||
variables: [{ key: variable.key, value: variable.value, _destroy: 'true' }],
|
variables_attributes: [{ id: variable.id, key: variable.key,
|
||||||
|
value: variable.value, _destroy: 'true' }],
|
||||||
format: :json
|
format: :json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue