Farmbot-Web-App/app/mutations/tools/destroy.rb

48 lines
1.0 KiB
Ruby
Raw Normal View History

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. " \
"Please remove it from the tool slot first."
2017-03-08 07:20:11 -07:00
required do
model :tool, class: Tool
end
def validate
2017-03-08 07:20:11 -07:00
any_deps?
any_slots?
end
def execute
2020-02-19 10:15:18 -07:00
maybe_unmount_tool
tool.destroy!
end
2017-03-08 07:20:11 -07:00
2020-02-19 10:15:18 -07:00
private
def slot
@slot ||= tool.tool_slot
end
2017-03-08 07:20:11 -07:00
def any_slots?
add_error :tool, :in_slot, STILL_IN_SLOT if slot.present?
2017-03-08 07:20:11 -07:00
end
def any_deps?
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
end
end