Nondestructive #apply

pull/801/head
Rick Carlino 2018-04-20 09:41:10 -05:00
parent a22b4d761e
commit a71024cbf6
6 changed files with 55 additions and 3 deletions

View File

@ -20,6 +20,11 @@ module Api
mutate SavedGardens::SnapShot.run(params.as_json, device: current_device)
end
def apply
mutate SavedGardens::Apply
.run(garden: garden, device: current_device, destructive: false)
end
private
def gardens

View File

@ -1,3 +1,4 @@
class SavedGarden < ApplicationRecord
belongs_to :device
has_many :plant_templates
end

View File

@ -0,0 +1,27 @@
module SavedGardens
class Apply < Mutations::Command
required do
model :device, class: Device
model :garden, class: SavedGarden
boolean :destructive # Not yet implemented. RC 4/20/18
end
def execute
Plant
.create!(garden
.plant_templates
.map do |template|
{ device_id: device.id,
name: template.name,
openfarm_slug: template.openfarm_slug,
plant_stage: "planned",
pointer_id: 0,
radius: template.radius,
x: template.x,
y: template.y,
z: template.z }
end)
""
end
end
end

View File

@ -13,6 +13,7 @@ module SavedGardens
@garden = SavedGarden.create!(inputs)
create_templates_from_plants
end
""
end
def create_templates_from_plants

View File

@ -43,7 +43,7 @@ FarmBot::Application.routes.draw do
resources :saved_gardens, except: [ :show ] do
post :snapshot, on: :collection
# post :apply, on: :member
# patch :apply, on: :member
patch :apply, on: :member
end
get "/global_config" => "global_config#show", as: :global_config

View File

@ -60,12 +60,30 @@ describe Api::SavedGardensController do
SavedGarden.destroy_all
PlantTemplate.destroy_all
sign_in user
b4 = user.device.saved_gardens.count
gardens_b4 = user.device.saved_gardens.count
templates_b4 = user.device.plant_templates.count
plants = FactoryBot.create_list(:plant, 3, device: user.device)
post :snapshot
expect(response.status).to be(200)
expect(user.device.plant_templates.count).to eq(plants.length)
expect(user.device.saved_gardens.count).to be > b4
expect(user.device.saved_gardens.count).to be > gardens_b4
expect(user.device.plant_templates.count).to be > templates_b4
end
end
describe "#apply" do
it "converts plant templates to real plants" do
SavedGarden.destroy_all
Plant.destroy_all
PlantTemplate.destroy_all
sign_in user
# Create a garden
saved_garden = FactoryBot.create(:saved_garden, device: user.device)
FactoryBot.create_list(:plant_template, 3, device: user.device, saved_garden: saved_garden)
old_plant_count = user.device.plants.count
post :apply, params: {id: saved_garden.id }
expect(response.status).to be(200)
expect(user.device.plants.count).to be > old_plant_count
end
end
end