diff options
author | Matija Čupić <matteeyah@gmail.com> | 2018-01-13 04:10:04 +0300 |
---|---|---|
committer | Matija Čupić <matteeyah@gmail.com> | 2018-02-05 20:57:42 +0300 |
commit | b91539d68fa21979001e122c912fad1c31dfd5d5 (patch) | |
tree | e415a944ce3d677c61fde4427993e7f57764455b /app/controllers | |
parent | 121d84d774e18b27a8a4624f173e97cfad0d7f7c (diff) |
Refactor VariablesController#save_multiple
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/projects/variables_controller.rb | 22 |
1 files changed, 13 insertions, 9 deletions
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 |