2016-12-21 14:01:06 -07:00
|
|
|
module Tools
|
|
|
|
class Destroy < Mutations::Command
|
2020-02-19 10:15:18 -07:00
|
|
|
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. " \
|
2017-03-08 14:10:31 -07:00
|
|
|
"Please remove it from the tool slot first."
|
2017-03-08 07:20:11 -07:00
|
|
|
|
2016-12-21 14:01:06 -07:00
|
|
|
required do
|
|
|
|
model :tool, class: Tool
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate
|
2017-03-08 07:20:11 -07:00
|
|
|
any_deps?
|
|
|
|
any_slots?
|
2016-12-21 14:01:06 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2020-02-19 10:15:18 -07:00
|
|
|
maybe_unmount_tool
|
2016-12-21 14:01:06 -07:00
|
|
|
tool.destroy!
|
|
|
|
end
|
2017-03-08 07:20:11 -07:00
|
|
|
|
2020-02-19 10:15:18 -07:00
|
|
|
private
|
2018-04-21 09:34:21 -06:00
|
|
|
|
|
|
|
def slot
|
|
|
|
@slot ||= tool.tool_slot
|
|
|
|
end
|
|
|
|
|
2017-03-08 07:20:11 -07:00
|
|
|
def any_slots?
|
2018-04-21 09:34:21 -06:00
|
|
|
add_error :tool, :in_slot, STILL_IN_SLOT if slot.present?
|
2017-03-08 07:20:11 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def any_deps?
|
2018-02-11 14:40:21 -07:00
|
|
|
add_error :tool, :in_use, STILL_IN_USE % [names] if names.present?
|
2017-03-08 07:20:11 -07:00
|
|
|
end
|
2018-04-04 14:54:28 -06:00
|
|
|
|
|
|
|
def names
|
2020-02-19 10:15:18 -07:00
|
|
|
@names ||=
|
2018-04-04 14:54:28 -06:00
|
|
|
InUseTool.where(tool_id: tool.id).pluck(:sequence_name).join(", ")
|
|
|
|
end
|
2020-02-19 10:15:18 -07:00
|
|
|
|
|
|
|
def maybe_unmount_tool
|
|
|
|
if tool.device.mounted_tool_id == tool.id
|
|
|
|
tool.device.update!(mounted_tool_id: nil)
|
|
|
|
end
|
|
|
|
end
|
2016-12-21 14:01:06 -07:00
|
|
|
end
|
|
|
|
end
|