Centralize (Points|ToolSlots|Plants)::Update

pull/325/merge
Rick Carlino 2017-05-10 16:14:13 -05:00
parent d1285a6bf1
commit 41fdb4df21
10 changed files with 121 additions and 96 deletions

View File

@ -14,22 +14,22 @@ module Api
render json: points
end
def show
render json: points.find(params[:id])
end
def search
mutate Points::Query.run(raw_json, device_params)
end
def create
mutate (case raw_json&.dig(:pointer_type)
when "GenericPointer" then Points::Create
when "ToolSlot" then ToolSlots::Create
when "Plant" then Plants::Create
else
raise BadPointerType
end).run(raw_json, device_params)
mutate pointer_klass::Create.run(raw_json, device_params)
end
def update
mutate Points::Update.run raw_json, { point: point }, device_params
mutate Points::Update.run(params.as_json,
{ point: point },
device_params)
end
def destroy
@ -39,6 +39,15 @@ module Api
private
def pointer_klass
case raw_json&.dig(:pointer_type)
when "GenericPointer" then Points
when "ToolSlot" then ToolSlots
when "Plant" then Plants
else; raise BadPointerType
end
end
def point
@point ||= points.find(params[:id])
end
@ -50,21 +59,5 @@ module Api
def device_params
@device_params ||= {device: current_device}
end
def tool_slots
@tool_slots ||= ToolSlot
.joins(:point)
.where("points.device_id = ?", current_device.id)
end
def tool_slot
@tool_slot ||= tool_slots.find_by!(id: params[:id])
end
def tool_slot_params
ts = (params[:id] ? tool_slot : nil)
@tool_slot_params ||= raw_json
.merge({ device: current_device, tool_slot: ts })
end
end
end

View File

@ -1,30 +1,30 @@
module Plants
class Update < Mutations::Command
required do
model :device, class: Device
model :plant, class: Plant
end
# required do
# model :device, class: Device
# model :plant, class: Plant
# end
optional do
float :x
float :y
string :name
string :openfarm_slug
float :radius
end
# optional do
# float :radius
# float :x
# float :y
# string :name
# string :openfarm_slug
# end
def execute
plant
.point
.update_attributes!(update_params) && plant.reload
# plant.update_attributes!(inputs.except(:device, :plant)) && plant
end
# def execute
# plant
# .point
# .update_attributes!(update_params) && plant.reload
# # plant.update_attributes!(inputs.except(:device, :plant)) && plant
# end
def update_params
plant.assign_attributes(inputs.slice(:openfarm_slug))
inputs
.slice(*Point::SHARED_FIELDS)
.merge(pointer: plant)
end
# def update_params
# plant.assign_attributes(inputs.slice(:openfarm_slug))
# inputs
# .slice(*Point::SHARED_FIELDS)
# .merge(pointer: plant)
# end
end
end

View File

@ -2,20 +2,32 @@ module Points
class Update < Mutations::Command
required do
model :device, class: Device
model :point, class: Point
model :point, class: Point
end
optional do
float :x
float :y
float :z
float :radius
hstore :meta
integer :tool_id, nils: true, empty_is_nil: true
float :x
float :y
float :z
float :radius
string :name
string :openfarm_slug
end
def validate
throw "BRB" if tool_id && !device.tools.pluck(:id).includes?(tool_id)
end
def execute
point.update_attributes!(inputs.except(:device, :point))
point
point.update_attributes!(update_params) && point
end
private
def update_params
point.assign_attributes(inputs.slice(:tool_id, :openfarm_slug))
inputs.slice(*Point::SHARED_FIELDS).merge(pointer: point)
end
end
end

View File

@ -1,31 +1,36 @@
module ToolSlots
class Update < ToolSlots::Base
required do
model :device, class: Device
model :tool_slot, class: ToolSlot
end
optional do
integer :tool_id, nils: true, empty_is_nil: true
string :name
integer :x
integer :y
integer :z
end
# required do
# model :device, class: Device
# model :point, class: Point
# end
def execute
tool_slot
.point
.update_attributes!(update_params) && tool_slot
end
# optional do
# integer :tool_id, nils: true, empty_is_nil: true
# float :x
# float :y
# float :z
# float :radius
# string :name
# string :openfarm_slug
# end
private
# def validate
# throw "BRB" if tool_id && !device.tools.pluck(:id).includes?(tool_id)
# end
def update_params
tool_slot.assign_attributes(inputs.slice(:tool_id))
inputs
.slice(*Point::SHARED_FIELDS)
.merge(pointer: tool_slot)
end
# def execute
# point.point.update_attributes!(update_params) && point
# end
# private
# def update_params
# point.assign_attributes(inputs.slice(:tool_id, :openfarm_slug))
# inputs
# .slice(*Point::SHARED_FIELDS)
# .merge(pointer: point)
# end
end
end

View File

@ -1,6 +1,6 @@
class PointSerializer < ActiveModel::Serializer
attributes :created_at, :updated_at, :device_id, :meta, :name, :pointer_type,
:radius, :x, :y, :z
attributes :id, :created_at, :updated_at, :device_id, :meta, :name,
:pointer_type, :radius, :x, :y, :z
attribute :openfarm_slug, if: :plant?
attribute :tool_id, if: :tool_slot?

View File

@ -3,14 +3,12 @@ FarmBot::Application.routes.draw do
namespace :api, defaults: {format: :json}, constraints: { format: 'json' } do
resources :tool_bays, only: [:index]
resources :images, only: [:create, :destroy, :show, :index]
resources :plants, only: [:create, :destroy, :index, :update]
resources :regimens, only: [:create, :destroy, :index, :update]
resources :peripherals, only: [:create, :destroy, :index, :update]
resources :corpuses, only: [:index, :show]
resources :logs, only: [:index, :create, :destroy]
resources :sequences, only: [:create, :update, :destroy, :index, :show]
resources :farm_events, only: [:create, :update, :destroy, :index]
resources :tool_slots, only: [:create, :show, :index, :destroy, :update]
resources :tools, only: [:create, :show, :index, :destroy, :update]
resources :points, only: [:create, :show, :index, :destroy, :update] do
post :search, on: :collection

View File

@ -1,9 +1,9 @@
require 'spec_helper'
describe Api::DevicesController do
include Devise::Test::ControllerHelpers
describe '#destroy' do
let(:user) { FactoryGirl.create(:user) }

View File

@ -41,9 +41,8 @@ describe Api::PointsController do
device: user.device,
pointer: ToolSlot.new)
get :index
binding.pry
expect(json.first[:id]).to eq(ts.id)
expect(json.first[:name]).to eq(ts.point.name)
expect(json.first[:name]).to eq(ts.name)
end
end
end

View File

@ -1,16 +1,29 @@
require "spec_helper"
describe Api::PointsController do
include Devise::Test::ControllerHelpers
describe("#show") do
let(:user) { FactoryGirl.create(:user) }
let(:device) { user.device }
it 'renders a tool slot' do
tool_slot = Point.create!(x: 0,
y: 0,
z: 0,
radius: 0,
device: user.device,
pointer: ToolSlot.new)
sign_in user
payload = { id: tool_slot.id }
get :show, params: payload
expect(response.status).to eq(200)
expect(json[:id]).to eq(tool_slot.id)
expect(json[:tool_id]).to eq(tool_slot.tool_id)
expect(json[:name]).to eq(tool_slot.point.name)
expect(json[:x]).to eq(tool_slot.point.x)
expect(json[:y]).to eq(tool_slot.point.y)
expect(json[:z]).to eq(tool_slot.point.z)
expect(json[:tool_id]).to eq(tool_slot.pointer.tool_id)
expect(json[:name]).to eq(tool_slot.name)
expect(json[:x]).to eq(tool_slot.x)
expect(json[:y]).to eq(tool_slot.y)
expect(json[:z]).to eq(tool_slot.z)
end
end
end

View File

@ -26,19 +26,24 @@ describe Api::PointsController do
end
it 'updates a plant' do
plant = Point.create(x: 0,
y: 0,
z: 0,
radius: 1,
pointer: Plant.new(openfarm_slug: "lettuce"),
device: user.device)
sign_in user
p = { id: plant.id,
x: 23,
y: 45,
name: "My Lettuce",
openfarm_slug: "limestone-lettuce"
}
patch :update, params: p
openfarm_slug: "limestone-lettuce" }
put :update, body: p.to_json, params: { format: :json, id: point.id }
expect(response.status).to eq(200)
plant.reload
expect(plant.point.x).to eq(p[:x])
expect(plant.point.y).to eq(p[:y])
expect(plant.point.name).to eq(p[:name])
expect(plant.x).to eq(p[:x])
expect(plant.y).to eq(p[:y])
expect(plant.name).to eq(p[:name])
expect(plant.openfarm_slug).to eq(p[:openfarm_slug])
p.keys.each do |key|
expect(json).to have_key(key)