Dismount tool prior to deletion

tool_deleteion
Rick Carlino 2020-02-19 11:15:18 -06:00
parent ddb480921e
commit 73222de627
2 changed files with 46 additions and 29 deletions

View File

@ -1,8 +1,8 @@
module Tools module Tools
class Destroy < Mutations::Command class Destroy < Mutations::Command
STILL_IN_USE = "Can't delete tool because the following sequences are "\ STILL_IN_USE = "Can't delete tool because the following sequences are " \
"still using it: %s" "still using it: %s"
STILL_IN_SLOT = "Can't delete tool because it is still in a tool slot. "\ STILL_IN_SLOT = "Can't delete tool because it is still in a tool slot. " \
"Please remove it from the tool slot first." "Please remove it from the tool slot first."
required do required do
@ -15,10 +15,11 @@ module Tools
end end
def execute def execute
maybe_unmount_tool
tool.destroy! tool.destroy!
end end
private private
def slot def slot
@slot ||= tool.tool_slot @slot ||= tool.tool_slot
@ -33,8 +34,14 @@ private
end end
def names def names
@names ||= \ @names ||=
InUseTool.where(tool_id: tool.id).pluck(:sequence_name).join(", ") InUseTool.where(tool_id: tool.id).pluck(:sequence_name).join(", ")
end end
def maybe_unmount_tool
if tool.device.mounted_tool_id == tool.id
tool.device.update!(mounted_tool_id: nil)
end
end
end end
end end

View File

@ -1,17 +1,18 @@
require 'spec_helper' require "spec_helper"
describe Api::ToolsController do describe Api::ToolsController do
include Devise::Test::ControllerHelpers include Devise::Test::ControllerHelpers
describe '#destroy' do describe "#destroy" do
let(:user) { FactoryBot.create(:user) } let(:user) { FactoryBot.create(:user) }
let(:tool_slot) { FactoryBot.create(:tool_slot) } let(:tool_slot) { FactoryBot.create(:tool_slot) }
let!(:tool) { let!(:tool) {
Point.destroy_all Point.destroy_all
FactoryBot.create(:tool, FactoryBot.create(:tool,
tool_slot: tool_slot, tool_slot: tool_slot,
device: user.device) } device: user.device)
}
it 'destroy a tool' do it "destroy a tool" do
sign_in user sign_in user
before = Tool.count before = Tool.count
tool.tool_slot.update(tool: nil) tool.tool_slot.update(tool: nil)
@ -21,7 +22,7 @@ describe Api::ToolsController do
expect(before).to be > after expect(before).to be > after
end 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 PinBinding.destroy_all
Sequence.destroy_all Sequence.destroy_all
Sequences::Create.run!(name: "Dep. tracking", Sequences::Create.run!(name: "Dep. tracking",
@ -30,12 +31,11 @@ describe Api::ToolsController do
{ {
kind: "move_absolute", kind: "move_absolute",
args: { args: {
location: { kind: "tool", args: { tool_id: tool.id } location: { kind: "tool", args: { tool_id: tool.id } },
offset: { kind: "coordinate", args: { x: 1, y: 2, z: 3 } },
speed: 100,
},
}, },
offset: { kind: "coordinate", args: {x: 1, y: 2, z: 3} },
speed: 100
}
}
]) ])
sequence = Sequence.last sequence = Sequence.last
sd_list = EdgeNode sd_list = EdgeNode
@ -52,11 +52,21 @@ describe Api::ToolsController do
expect(response.status).to eq(422) expect(response.status).to eq(422)
expect(before).to eq(after) expect(before).to eq(after)
expect(json[:tool]) expect(json[:tool]).to include(Tools::Destroy::STILL_IN_USE % sequence.name)
.to include(Tools::Destroy::STILL_IN_USE % sequence.name)
end 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 sign_in user
before = Tool.count before = Tool.count
delete :destroy, params: { id: tool.id } delete :destroy, params: { id: tool.id }