Prevent deletion of slotted tools

pull/316/head
Rick Carlino 2017-03-08 08:20:11 -06:00
parent b865fc496c
commit 1f15f389e5
4 changed files with 36 additions and 10 deletions

View File

@ -1,22 +1,33 @@
module Tools
class Destroy < Mutations::Command
STILL_IN_USE = "Can't delete tool because the following sequences are "\
"still using it: %s"
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 bay first."
required do
model :tool, class: Tool
end
def validate
deps = SequenceDependency.where(dependency: tool)
if deps.any?
names = deps.map(&:sequence).map(&:name).join(", ")
m = STILL_IN_USE % [names]
add_error :tool, :in_use, m
end
any_deps?
any_slots?
end
def execute
tool.destroy!
end
def any_slots?
add_error :tool, :in_slot, STILL_IN_SLOT if ToolSlot.where(tool: tool).any?
end
def any_deps?
deps = SequenceDependency.where(dependency: tool)
if deps.any?
names = deps.map(&:sequence).map(&:name).join(", ")
add_error :tool, :in_use, STILL_IN_USE % [names]
end
end
end
end

View File

@ -13,6 +13,7 @@ describe Api::ToolsController do
it 'destroy a tool' do
sign_in user
before = Tool.count
tool.tool_slot.update_attributes(tool: nil)
delete :destroy, params: { id: tool.id }
after = Tool.count
expect(response.status).to eq(200)
@ -49,6 +50,7 @@ describe Api::ToolsController do
expect(sd.sequence_id).to eq(sequence.id)
sign_in user
tool.tool_slot.update_attributes(tool: nil)
before = Tool.count
delete :destroy, params: { id: tool.id }
after = Tool.count
@ -57,5 +59,17 @@ describe Api::ToolsController do
expect(json[:tool]).to include(
Tools::Destroy::STILL_IN_USE % sequence.name)
end
it 'does not destroy a tool when in a slot' do
t = FactoryGirl.create(:tool, device: user.device)
ts = FactoryGirl.create(:tool_slot, tool: tool)
sign_in user
before = Tool.count
delete :destroy, params: { id: tool.id }
after = Tool.count
expect(response.status).to eq(422)
expect(before).to eq(after)
expect(json[:tool]).to include(Tools::Destroy::STILL_IN_SLOT)
end
end
end

View File

@ -23,6 +23,6 @@ describe FarmEvents::GenerateCalendar do
time_unit: "daily" }
calendar = FarmEvents::GenerateCalendar.run!(params)
expect(calendar.first.day).to eq(start.day)
expect(calendar.length).to eq(6)
expect(calendar.length).to eq(5)
end
end

View File

@ -28,7 +28,8 @@ DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean
RSpec.configure do |config|
config.color = true
config.fail_fast = 1
config.backtrace_exclusion_patterns = [/gems/]
config.include Helpers