Commit b91539d6 authored by Matija Čupić's avatar Matija Čupić

Refactor VariablesController#save_multiple

parent 121d84d7
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment