diff options
author | Matija Čupić <matteeyah@gmail.com> | 2018-01-13 05:36:04 +0300 |
---|---|---|
committer | Matija Čupić <matteeyah@gmail.com> | 2018-02-05 20:57:43 +0300 |
commit | ba077841922089c0eb2bbb48947de8828f891776 (patch) | |
tree | 557eef632e82cc106a272c64376f065990ad326a /app/controllers/projects | |
parent | c64181ce4c57bc7ffbbcc51ee9bf0001bf83e1b1 (diff) |
Add destroy functionality to save_multiple
Diffstat (limited to 'app/controllers/projects')
-rw-r--r-- | app/controllers/projects/variables_controller.rb | 11 |
1 files changed, 9 insertions, 2 deletions
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 |