From 7829d53c854e2ba6eb574d6f994204fb0b4bb869 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Fri, 1 Jun 2018 20:44:11 +0100 Subject: Rework the services method in the MiniEnvironmentController Bring the transaction inside of the request.put? conditional, as it shouldn't have been outside to begin with. Also, store the data snapshot if one is selected. --- app/controllers/mini_environments_controller.rb | 40 +++++++++++++++---------- 1 file changed, 25 insertions(+), 15 deletions(-) (limited to 'app') diff --git a/app/controllers/mini_environments_controller.rb b/app/controllers/mini_environments_controller.rb index 0cee75b..05cf558 100644 --- a/app/controllers/mini_environments_controller.rb +++ b/app/controllers/mini_environments_controller.rb @@ -101,28 +101,38 @@ class MiniEnvironmentsController < ApplicationController def services @mini_environment = MiniEnvironment.find(params[:id]) - @mini_environment.transaction do - @mini_environment.govuk_guix_revision.all_available_services - .each do |service| + if request.put? + @mini_environment.transaction do + @mini_environment.govuk_guix_revision.all_available_services + .each do |service| + + name = service['name'] + + next if params["#{name}-selected"] != 'on' - name = service['name'] + revision = params["#{name}-revision"] - next if params["#{name}-selected"] != 'on' + @mini_environment.services.where( + name: name + ).delete_all - revision = params["#{name}-revision"] + @mini_environment.services.create( + name: name, + revision: revision + ) + end - @mini_environment.services.where( - name: name - ).delete_all + data_snapshot_id = params.fetch(:data_snapshot, nil) - @mini_environment.services.create( - name: name, - revision: revision - ) + if data_snapshot_id + # TODO: Validate that this is on the list of approved snapshots + + @mini_environment.update!( + data_snapshot_id: data_snapshot_id + ) + end end - end - if request.put? @mini_environment.backend.build(@mini_environment) redirect_to @mini_environment end -- cgit v1.2.3