diff options
author | Matija Čupić <matteeyah@gmail.com> | 2018-01-23 03:17:40 +0300 |
---|---|---|
committer | Matija Čupić <matteeyah@gmail.com> | 2018-02-05 20:57:43 +0300 |
commit | 5de85708ce17c1965581b8bf7563751dda77510d (patch) | |
tree | e8873b614aae25144e48eacf468ed35305d5fe29 /app/controllers/projects | |
parent | ba077841922089c0eb2bbb48947de8828f891776 (diff) |
Use nested attributes for updating multiple variables
Diffstat (limited to 'app/controllers/projects')
-rw-r--r-- | app/controllers/projects/variables_controller.rb | 24 |
1 files changed, 3 insertions, 21 deletions
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 |