From 87960781ad5d5017cf4bb802f75a105b5e5e28fe Mon Sep 17 00:00:00 2001 From: Ryan Ferguson Date: Tue, 9 Jan 2024 09:58:09 -0500 Subject: [PATCH] Fix missing `nil` attributes in SObject.update (#79) * fix missing nil attributes in SObject.update * update changelog --- CHANGELOG.md | 1 + lib/active_force/sobject.rb | 24 ++++++++++++++++-------- spec/active_force/sobject_spec.rb | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f12a585..6fbe243 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Not released +- Fix `.update` and `.update!`: include given `nil` valued attributes in request (https://github.com/Beyond-Finance/active_force/pull/79) - Change `.first` to not query the API if records have already been retrieved (https://github.com/Beyond-Finance/active_force/pull/77) ## 0.20.1 diff --git a/lib/active_force/sobject.rb b/lib/active_force/sobject.rb index 4b1812f..eb8bf1c 100644 --- a/lib/active_force/sobject.rb +++ b/lib/active_force/sobject.rb @@ -34,8 +34,24 @@ class << self def_delegators :query, :not, :or, :where, :first, :last, :all, :find, :find!, :find_by, :find_by!, :sum, :count, :includes, :limit, :order, :select, :none def_delegators :mapping, :table, :table_name, :custom_table?, :mappings + def update(id, attributes) + prepare_for_update(id, attributes).update + end + + def update!(id, attributes) + prepare_for_update(id, attributes).update! + end + private + def prepare_for_update(id, attributes) + new(attributes.merge(id: id)).tap do |obj| + attributes.each do |name, value| + obj.public_send("#{name}_will_change!") if value.nil? + end + end + end + ### # Provide each subclass with a default id field. Can be overridden # in the subclass if needed @@ -131,14 +147,6 @@ def self.create! args new(args).create! end - def self.update(id, attributes) - new(attributes.merge(id: id)).update - end - - def self.update!(id, attributes) - new(attributes.merge(id: id)).update! - end - def save! run_callbacks :save do if persisted? diff --git a/spec/active_force/sobject_spec.rb b/spec/active_force/sobject_spec.rb index 69b6122..9f1adf9 100644 --- a/spec/active_force/sobject_spec.rb +++ b/spec/active_force/sobject_spec.rb @@ -321,6 +321,15 @@ class IceCream < ActiveForce::SObject .and_return(true) Whizbang.update('12345678', text: 'my text') end + + it 'includes given nil values in the request' do + allow(client).to receive(:update!).and_return(true) + Whizbang.update('test123', text: nil, date: nil) + expect(client).to have_received(:update!).with( + Whizbang.table_name, + { 'Id' => 'test123', 'Text_Label' => nil, 'Date_Label' => nil, 'Updated_From__c' => 'Rails' } + ) + end end describe 'self.update!' do @@ -330,6 +339,15 @@ class IceCream < ActiveForce::SObject .and_return(true) Whizbang.update('123456789', text: 'some other text') end + + it 'includes given nil values in the request' do + allow(client).to receive(:update!).and_return(true) + Whizbang.update!('test123', text: nil, date: nil) + expect(client).to have_received(:update!).with( + Whizbang.table_name, + { 'Id' => 'test123', 'Text_Label' => nil, 'Date_Label' => nil, 'Updated_From__c' => 'Rails' } + ) + end end end