Dismount tool prior to deletion
parent
ddb480921e
commit
73222de627
|
@ -15,6 +15,7 @@ module Tools
|
|||
end
|
||||
|
||||
def execute
|
||||
maybe_unmount_tool
|
||||
tool.destroy!
|
||||
end
|
||||
|
||||
|
@ -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
|
||||
|
|
|
@ -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) }
|
||||
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,7 +22,7 @@ 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",
|
||||
|
@ -30,12 +31,11 @@ describe Api::ToolsController do
|
|||
{
|
||||
kind: "move_absolute",
|
||||
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
|
||||
}
|
||||
}
|
||||
speed: 100,
|
||||
},
|
||||
},
|
||||
])
|
||||
sequence = Sequence.last
|
||||
sd_list = EdgeNode
|
||||
|
@ -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 }
|
||||
|
|
Loading…
Reference in New Issue