Skip to content

Commit 668ccb7

Browse files
XianlinGongSam Gong
andauthored
MEM-7622: Stop-salesforce-syncable-objects-from-being-saved-with-sale… (#41)
Co-authored-by: Sam Gong <sgong@infotech.com>
1 parent f52506f commit 668ccb7

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,6 @@
4949
# Version 5.2.1
5050
* Reverted accidental change to object creation during sync.
5151
* Added CI for the test suite.
52+
53+
# Version 5.3.0
54+
* Changed salesforce_ar_sync to use bang (!) versions of Restforce CRUD methods to raise errors on Salesforce save failures instead of returning false.

lib/salesforce_ar_sync/salesforce_sync.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,19 @@ def is_boolean?(attribute)
176176

177177
def salesforce_create_object(attributes)
178178
attributes[self.class.salesforce_web_id_attribute_name.to_s] = id if self.class.salesforce_sync_web_id? && !new_record?
179-
salesforce_id = SF_CLIENT.create(salesforce_object_name, format_attributes(attributes))
180-
self.salesforce_id = salesforce_id
179+
self.salesforce_id = SF_CLIENT.create!(salesforce_object_name, format_attributes(attributes))
181180
@exists_in_salesforce = true
182181
end
183182

184183
def salesforce_update_object(attributes)
185184
attributes[self.class.salesforce_web_id_attribute_name.to_s] = id if self.class.salesforce_sync_web_id? && !new_record?
186185

187-
SF_CLIENT.update(salesforce_object_name, format_attributes(attributes).merge(Id: salesforce_id))
186+
SF_CLIENT.update!(salesforce_object_name, format_attributes(attributes).merge(Id: salesforce_id))
188187
end
189188

190189
def salesforce_delete_object
191190
if ar_sync_outbound_delete?
192-
SF_CLIENT.destroy(salesforce_object_name, salesforce_id)
191+
SF_CLIENT.destroy!(salesforce_object_name, salesforce_id)
193192
end
194193
end
195194

@@ -236,7 +235,7 @@ def salesforce_sync(*attrs)
236235
def sync_web_id
237236
return false if !self.class.salesforce_sync_web_id? || SalesforceArSync.config['SYNC_ENABLED'] == false
238237

239-
SF_CLIENT.update(salesforce_object_name, Id: salesforce_id, self.class.salesforce_web_id_attribute_name.to_s => get_activerecord_web_id) if salesforce_id
238+
SF_CLIENT.update!(salesforce_object_name, Id: salesforce_id, self.class.salesforce_web_id_attribute_name.to_s => get_activerecord_web_id) if salesforce_id
240239
end
241240

242241
def get_activerecord_web_id

lib/salesforce_ar_sync/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module SalesforceArSync
4-
VERSION = '5.2.1'
4+
VERSION = '5.3.0'
55
end

spec/salesforce_ar_sync/salesforce_ar_sync_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -521,14 +521,14 @@ class ProcessUpdateTest < ActiveRecord::Base
521521
}
522522
)
523523

524-
expect(SF_CLIENT).to receive(:update).with('Contact', expected_attributes)
524+
expect(SF_CLIENT).to receive(:update!).with('Contact', expected_attributes)
525525
contact.salesforce_update_object(contact.attributes)
526526
end
527527
end
528528

529529
context 'when the class should not sync web id' do
530530
it 'calls SF_CLIENT.update with the correct parameters' do
531-
expect(SF_CLIENT).to receive(:update).with('Contact', contact.attributes.merge(Id: contact.salesforce_id))
531+
expect(SF_CLIENT).to receive(:update!).with('Contact', contact.attributes.merge(Id: contact.salesforce_id))
532532
contact.salesforce_update_object(contact.attributes)
533533
end
534534
end
@@ -549,14 +549,14 @@ class ProcessUpdateTest < ActiveRecord::Base
549549
SalesforceArSync.config['SYNC_ENABLED'] = true
550550
stub_const('SF_CLIENT', restforce_client_stub)
551551

552-
allow(SF_CLIENT).to receive(:update)
552+
allow(SF_CLIENT).to receive(:update!)
553553
allow(contact).to receive(:salesforce_object_exists?).and_return(true)
554554
end
555555

556556
context 'when supplied with which attributes to sync' do
557557
it 'calls SF_CLIENT.update with the correct parameters' do
558558
contact.salesforce_sync(:first_name, :email_address)
559-
expect(SF_CLIENT).to have_received(:update).with(
559+
expect(SF_CLIENT).to have_received(:update!).with(
560560
'Contact',
561561
Id: contact.salesforce_id,
562562
Contact.salesforce_sync_attribute_mapping.invert[:first_name] => contact.first_name,
@@ -568,7 +568,7 @@ class ProcessUpdateTest < ActiveRecord::Base
568568
context 'when supplied with invalid attributes to sync' do
569569
it 'calls SF_CLIENT.update with the correct parameters' do
570570
contact.salesforce_sync(:bad_attribute)
571-
expect(SF_CLIENT).not_to have_received(:update)
571+
expect(SF_CLIENT).not_to have_received(:update!)
572572
end
573573
end
574574
end

0 commit comments

Comments
 (0)