Merge pull request #1775 from FarmBot/point_meta_updates

Merge meta attrs, dont overwrite
pull/1776/head
Rick Carlino 2020-05-06 16:40:08 -05:00 committed by GitHub
commit 88e526cce3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 5 deletions

View File

@ -27,11 +27,19 @@ module Points
end
def execute
Point.transaction { point.update!(inputs.except(:point)) && point }
Point.transaction { point.update!(update_params) && point }
end
private
def merged_meta_fields
@merged_meta_fields ||= (point.meta || {}).merge(meta || {})
end
def update_params
@update_params ||= inputs.except(:point).merge(meta: merged_meta_fields)
end
def new_tool_id?
raw_inputs.key?("tool_id")
end

View File

@ -1,11 +1,26 @@
require 'spec_helper'
require "spec_helper"
require_relative "scenario"
describe Points::Update do
let(:device) { FactoryBot.create(:device) }
let(:point) do
FactoryBot.create(:generic_pointer, device: device, meta: { a: 1, b: 2 })
end
it "updates meta attributes (but doesn't clobber old ones)" do
new_meta = { c: 3, d: 4 }
Points::Update.run(device: point.device, point: point, meta: new_meta)
point.reload
expect(point.meta["a"]).to eq("1")
expect(point.meta["b"]).to eq("2")
expect(point.meta["c"]).to eq("3")
expect(point.meta["d"]).to eq("4")
end
it "prevents remove of tool from actively used tool slots" do
s = Points::Scenario.new
result = Points::Update.run(device: s.device,
point: s.tool_slot,
s = Points::Scenario.new
result = Points::Update.run(device: s.device,
point: s.tool_slot,
tool_id: nil)
expect(result.success?).to be(false)
expect(result.errors.message_list).to include(Tool::IN_USE % s.sequence[:name])