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
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

View File

@ -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 }