Yay batch updates work! 🎉

pull/941/head
Rick Carlino 2018-07-31 13:52:50 -05:00
parent efe5477430
commit edb1a65a7f
4 changed files with 29 additions and 7 deletions

View File

@ -1,8 +1,7 @@
module Resources
DEVICE_REGEX = /device_\d*/
ACTIONS = [
DESTROY = "destroy"
]
ACTIONS = [ DESTROY = "destroy", SAVE = "save" ]
RESOURCES = { # Because I don't trust Kernel.const_get
"FarmEvent" => FarmEvent,
"FarmwareInstallations" => FarmwareInstallation,

View File

@ -1,6 +1,9 @@
module Resources
class Job < Mutations::Command
NOT_FOUND = "Resource not found"
NOT_FOUND = "Resource not found"
NO_CREATE_YET = "You did not put a numeric `id` in the `body`. " +
"This would be handled as the creation of a new " +
"resource, but we don't support it yet."
required do
duck :body, methods: [:[], :[]=]
duck :resource, duck: [:where, :find_by]
@ -18,6 +21,7 @@ module Resources
def execute
case action
when DESTROY then do_deletion
when SAVE then do_save
else; never
end
end
@ -28,6 +32,22 @@ module Resources
@plural_resource ||= resource.name.pluralize
end
def do_save
model_name = resource.model_name
device_params = inputs.slice(:device)
klass = Kernel.const_get(model_name.name.pluralize)
id = body[:id]
if id.is_a?(Integer)
model = resource.where(device_params).find(id)
model_params = {model_name.singular => model}
binding.pry
# device_params is ALWAYS last because security.
klass::Update.run!(body, model_params, device_params) # Security!
else
add_error :body, :body, NO_CREATE_YET
end
end
def do_deletion
model_name = resource.model_name
mutation = Kernel.const_get(model_name.name.pluralize)::Destroy

View File

@ -37,12 +37,11 @@ module Resources
end
def self.step2(params)
puts params if Rails.env.production?
Job.run!(params)
uuid = (params[:uuid] || "NONE")
dev = params[:device]
dev.auto_sync_transaction do
oh_wow = Job.run!(params)
uuid = (params[:uuid] || "NONE")
Transport.current.amqp_send(ok(uuid), dev.id, MQTT_CHAN)
end
rescue Mutations::ValidationException => q
@ -61,6 +60,8 @@ module Resources
def self.process(delivery_info, body)
params = step1(delivery_info, body)
params && step2(params)
rescue => q
binding.pry
end
end # Service
end # Resources

View File

@ -1,3 +1,5 @@
require_relative "../../lib/hstore_filter"
module Points
class Update < Mutations::Command
required do