From 73222de6277316e63a732fcf7dfde372944cf73b Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Wed, 19 Feb 2020 11:15:18 -0600 Subject: [PATCH] Dismount tool prior to deletion --- app/mutations/tools/destroy.rb | 17 +++++-- spec/controllers/api/tools/destroy_spec.rb | 58 +++++++++++++--------- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/app/mutations/tools/destroy.rb b/app/mutations/tools/destroy.rb index e504606b7..f60fb3c29 100644 --- a/app/mutations/tools/destroy.rb +++ b/app/mutations/tools/destroy.rb @@ -1,8 +1,8 @@ module Tools class Destroy < Mutations::Command - STILL_IN_USE = "Can't delete tool because the following sequences are "\ - "still using it: %s" - STILL_IN_SLOT = "Can't delete tool because it is still in a tool slot. "\ + STILL_IN_USE = "Can't delete tool because the following sequences are " \ + "still using it: %s" + STILL_IN_SLOT = "Can't delete tool because it is still in a tool slot. " \ "Please remove it from the tool slot first." required do @@ -15,10 +15,11 @@ module Tools end def execute + maybe_unmount_tool tool.destroy! end -private + private def slot @slot ||= tool.tool_slot @@ -33,8 +34,14 @@ private end def names - @names ||= \ + @names ||= InUseTool.where(tool_id: tool.id).pluck(:sequence_name).join(", ") end + + def maybe_unmount_tool + if tool.device.mounted_tool_id == tool.id + tool.device.update!(mounted_tool_id: nil) + end + end end end diff --git a/spec/controllers/api/tools/destroy_spec.rb b/spec/controllers/api/tools/destroy_spec.rb index d860940ba..446542489 100644 --- a/spec/controllers/api/tools/destroy_spec.rb +++ b/spec/controllers/api/tools/destroy_spec.rb @@ -1,17 +1,18 @@ -require 'spec_helper' +require "spec_helper" describe Api::ToolsController do include Devise::Test::ControllerHelpers - describe '#destroy' do + describe "#destroy" do let(:user) { FactoryBot.create(:user) } let(:tool_slot) { FactoryBot.create(:tool_slot) } let!(:tool) { - Point.destroy_all - FactoryBot.create(:tool, - tool_slot: tool_slot, - device: user.device) } + Point.destroy_all + FactoryBot.create(:tool, + tool_slot: tool_slot, + device: user.device) + } - it 'destroy a tool' do + it "destroy a tool" do sign_in user before = Tool.count tool.tool_slot.update(tool: nil) @@ -21,24 +22,23 @@ describe Api::ToolsController do expect(before).to be > after end - it 'does not destroy a tool when in use by a sequence' do + it "does not destroy a tool when in use by a sequence" do PinBinding.destroy_all Sequence.destroy_all - Sequences::Create.run!(name: "Dep. tracking", + Sequences::Create.run!(name: "Dep. tracking", device: user.device, - body: [ - { - kind: "move_absolute", - args: { - location: { kind: "tool", args: { tool_id: tool.id } - }, - offset: { kind: "coordinate", args: {x: 1, y: 2, z: 3} }, - speed: 100 - } - } - ]) + body: [ + { + kind: "move_absolute", + args: { + location: { kind: "tool", args: { tool_id: tool.id } }, + offset: { kind: "coordinate", args: { x: 1, y: 2, z: 3 } }, + speed: 100, + }, + }, + ]) sequence = Sequence.last - sd_list = EdgeNode + sd_list = EdgeNode .where(kind: "tool_id", sequence: sequence) .map { |x| Tool.find(x.value) } sign_in user @@ -52,11 +52,21 @@ describe Api::ToolsController do expect(response.status).to eq(422) expect(before).to eq(after) - expect(json[:tool]) - .to include(Tools::Destroy::STILL_IN_USE % sequence.name) + expect(json[:tool]).to include(Tools::Destroy::STILL_IN_USE % sequence.name) end - it 'does not destroy a tool when in a slot' do + it "dismounts a tool when it is deleted" do + sign_in user + before = Tool.count + tool.tool_slot.update(tool: nil) + tool.device.update!(mounted_tool_id: tool.id) + delete :destroy, params: { id: tool.id } + after = Tool.count + expect(response.status).to eq(200) + expect(before).to be > after + end + + it "does not destroy a tool when in a slot" do sign_in user before = Tool.count delete :destroy, params: { id: tool.id }