Begin adding missing ::Destroy classes
parent
d21400a833
commit
60b85e4d38
|
@ -4,8 +4,8 @@ module Resources
|
|||
DESTROY = "destroy"
|
||||
]
|
||||
RESOURCES = { # Because I don't trust Kernel.const_get
|
||||
"DeviceConfig" => DeviceConfig,
|
||||
"DiagnosticDump" => DiagnosticDump,
|
||||
# "DeviceConfig" => DeviceConfig,
|
||||
# "DiagnosticDump" => DiagnosticDump,
|
||||
"FarmEvent" => FarmEvent,
|
||||
"FarmwareInstallations" => FarmwareInstallations,
|
||||
"Image" => Image,
|
||||
|
|
|
@ -19,6 +19,8 @@ module Resources
|
|||
when DESTROY then do_deletion
|
||||
else; never
|
||||
end
|
||||
rescue => x
|
||||
binding.pry
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -39,7 +41,7 @@ module Resources
|
|||
|
||||
# Escape hatch for things that should "never happen".
|
||||
def never
|
||||
raise "PANIC"
|
||||
raise "PANIC: Tried to do batch op on #{resource}"
|
||||
end
|
||||
end # Job
|
||||
end # Resources
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
# I heard you like mutations. So we made a mutation class that creates mutations
|
||||
# so you can mutate while you mutate.
|
||||
# This class will create a "base case" `::Delete` mutation. Very useful when you
|
||||
# don't have special logic.
|
||||
class CreateDestroyer < Mutations::Command
|
||||
BAD_OWNERSHIP = "You do not own that %s"
|
||||
|
||||
required do
|
||||
duck :resource
|
||||
end
|
||||
|
||||
def execute
|
||||
symbolized_name = resource.model_name.singular
|
||||
klass = resource
|
||||
|
||||
Class.new(Mutations::Command) do
|
||||
@@resource_name = symbolized_name
|
||||
|
||||
required do
|
||||
model :device, class: Device
|
||||
model @@resource_name, class: klass
|
||||
end
|
||||
|
||||
def validate
|
||||
not_yours unless self.send(@@resource_name).device == device
|
||||
end
|
||||
|
||||
def execute
|
||||
self.send(@@resource_name).destroy! && ""
|
||||
end
|
||||
|
||||
def not_yours
|
||||
add_error @@resource_name,
|
||||
@@resource_name,
|
||||
BAD_OWNERSHIP % @@resource_name
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,4 @@
|
|||
module WebcamFeeds
|
||||
Destroy = CreateDestroyer.run!(resource: WebcamFeed,
|
||||
singular_name: "webcam_feed")
|
||||
end
|
|
@ -0,0 +1,43 @@
|
|||
require "spec_helper"
|
||||
|
||||
describe Resources::Job do
|
||||
it "executes deletion for various resources" do
|
||||
puts "TODO: Test cases for points, sequences."
|
||||
device = FactoryBot.create(:device)
|
||||
base = \
|
||||
{ body: {}, device: device, action: "destroy", uuid: SecureRandom.uuid }
|
||||
|
||||
test_cases = [
|
||||
# DeviceConfig,
|
||||
# DiagnosticDump,
|
||||
# FarmEvent,
|
||||
# FarmwareInstallation,
|
||||
# Image,
|
||||
# Log,
|
||||
# PlantTemplate,
|
||||
# Point,
|
||||
# Regimen,
|
||||
# SavedGarden,
|
||||
# SensorReading,
|
||||
WebcamFeed,
|
||||
Peripheral,
|
||||
PinBinding,
|
||||
Sensor,
|
||||
Tool,
|
||||
]
|
||||
.each{ |k| k.destroy_all }
|
||||
.map { |k| FactoryBot.create(k.model_name.singular.to_sym, device: device) }
|
||||
.concat([FakeSequence.create( device: device)])
|
||||
.map { |r| base.merge({resource: r.class, resource_id: r.id }) }
|
||||
.map do |params|
|
||||
res = params[:resource]
|
||||
count = res.count
|
||||
expect(count).to eq(1)
|
||||
Resources::Job.run!(params)
|
||||
expect(res.count).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
it "deals with points later"
|
||||
it "doesn't let you delete other people's resources"
|
||||
end
|
Loading…
Reference in New Issue