Merge meta attrs, dont overwrite
parent
6213028f0f
commit
889c78c77a
|
@ -27,11 +27,19 @@ module Points
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute
|
def execute
|
||||||
Point.transaction { point.update!(inputs.except(:point)) && point }
|
Point.transaction { point.update!(update_params) && point }
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
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?
|
def new_tool_id?
|
||||||
raw_inputs.key?("tool_id")
|
raw_inputs.key?("tool_id")
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,26 @@
|
||||||
require 'spec_helper'
|
require "spec_helper"
|
||||||
require_relative "scenario"
|
require_relative "scenario"
|
||||||
|
|
||||||
describe Points::Update do
|
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
|
it "prevents remove of tool from actively used tool slots" do
|
||||||
s = Points::Scenario.new
|
s = Points::Scenario.new
|
||||||
result = Points::Update.run(device: s.device,
|
result = Points::Update.run(device: s.device,
|
||||||
point: s.tool_slot,
|
point: s.tool_slot,
|
||||||
tool_id: nil)
|
tool_id: nil)
|
||||||
expect(result.success?).to be(false)
|
expect(result.success?).to be(false)
|
||||||
expect(result.errors.message_list).to include(Tool::IN_USE % s.sequence[:name])
|
expect(result.errors.message_list).to include(Tool::IN_USE % s.sequence[:name])
|
||||||
|
|
Loading…
Reference in New Issue