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