Module: SalesforceArSync::SalesforceSync::ClassMethods
- Defined in:
- lib/salesforce_ar_sync/salesforce_sync.rb
Instance Attribute Summary collapse
-
#activerecord_web_id_attribute_name ⇒ Object
Returns the value of attribute activerecord_web_id_attribute_name.
-
#salesforce_async_attributes ⇒ Object
Returns an array of Salesforce attributes which should be synced asynchronously Example: [“Last_Login_Date__c”, “Login_Count__c” ] Note: The model will fall back to synchronous sync if non-synchronous attributes are changed along with async attributes.
-
#salesforce_default_attributes_for_create ⇒ Object
Returns a hash of default attributes that should be used when we are creating a new record.
-
#salesforce_id_attribute_name ⇒ Object
Returns the “Id” attribute of the corresponding Salesforce object.
-
#salesforce_object_name_method ⇒ Object
Optionally holds the name of a method which will return the name of the Salesforce object to sync to.
-
#salesforce_skip_sync_method ⇒ Object
Optionally holds the name of a method which can contain logic to determine if a record should be synced on save.
-
#salesforce_sync_attribute_mapping ⇒ Object
Hash mapping of Salesforce attributes to web attributes Example: { :Email => :login, :FirstName => :first_name, :LastName => :last_name }.
-
#salesforce_sync_enabled ⇒ Object
Optionally holds the value to determine if salesforce syncing is enabled.
-
#salesforce_sync_web_id ⇒ Object
Returns the value of attribute salesforce_sync_web_id.
-
#salesforce_web_class_name ⇒ Object
Returns the name of the Web Objects class.
-
#salesforce_web_id_attribute_name ⇒ Object
Returns the value of attribute salesforce_web_id_attribute_name.
-
#sync_inbound_delete ⇒ Object
Specify whether or not we sync deletes inbound from salesforce or outbound from this app Accepts either a true/false or a symbol to a method to be called.
-
#sync_outbound_delete ⇒ Object
Returns the value of attribute sync_outbound_delete.
-
#unscoped_updates ⇒ Object
Specify whether we should use an unscoped find to update an object (useful with paranoia gem to handle undeletes).
Instance Method Summary collapse
-
#salesforce_update(attributes = {}) ⇒ Object
Accepts values from an outbound message hash and will either update an existing record OR create a new record Firstly attempts to find an object by the salesforce_id attribute Secondly attempts to look an object up by it’s ID (WebId__c in outbound message) Lastly it will create a new record setting it’s salesforce_id.
Instance Attribute Details
#activerecord_web_id_attribute_name ⇒ Object
Returns the value of attribute activerecord_web_id_attribute_name.
47 48 49 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 47 def activerecord_web_id_attribute_name @activerecord_web_id_attribute_name end |
#salesforce_async_attributes ⇒ Object
Returns an array of Salesforce attributes which should be synced asynchronously Example: [“Last_Login_Date__c”, “Login_Count__c” ] Note: The model will fall back to synchronous sync if non-synchronous attributes are changed along with async attributes
24 25 26 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 24 def salesforce_async_attributes @salesforce_async_attributes end |
#salesforce_default_attributes_for_create ⇒ Object
Returns a hash of default attributes that should be used when we are creating a new record
27 28 29 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 27 def salesforce_default_attributes_for_create @salesforce_default_attributes_for_create end |
#salesforce_id_attribute_name ⇒ Object
Returns the “Id” attribute of the corresponding Salesforce object
30 31 32 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 30 def salesforce_id_attribute_name @salesforce_id_attribute_name end |
#salesforce_object_name_method ⇒ Object
Optionally holds the name of a method which will return the name of the Salesforce object to sync to
50 51 52 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 50 def salesforce_object_name_method @salesforce_object_name_method end |
#salesforce_skip_sync_method ⇒ Object
Optionally holds the name of a method which can contain logic to determine if a record should be synced on save. If no method is given then only the salesforce_skip_sync attribute is used.
54 55 56 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 54 def salesforce_skip_sync_method @salesforce_skip_sync_method end |
#salesforce_sync_attribute_mapping ⇒ Object
Hash mapping of Salesforce attributes to web attributes Example: { :Email => :login, :FirstName => :first_name, :LastName => :last_name }
“Web” attributes can be actual method names to return a custom value If you are providing a method name to return a value, you should also implement a corresponding my_method_changed? to return if the value has changed. Otherwise it will always be synced.
19 20 21 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 19 def salesforce_sync_attribute_mapping @salesforce_sync_attribute_mapping end |
#salesforce_sync_enabled ⇒ Object
Optionally holds the value to determine if salesforce syncing is enabled. Defaults to true. If set to false syncing will be disabled for the class
10 11 12 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 10 def salesforce_sync_enabled @salesforce_sync_enabled end |
#salesforce_sync_web_id ⇒ Object
Returns the value of attribute salesforce_sync_web_id.
46 47 48 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 46 def salesforce_sync_web_id @salesforce_sync_web_id end |
#salesforce_web_class_name ⇒ Object
Returns the name of the Web Objects class. A custom value can be provided if you wish to sync to a SF object and back to a different web object. This would generally be used if you wanted to flatten a web object into a larger SF object like Contact
35 36 37 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 35 def salesforce_web_class_name @salesforce_web_class_name end |
#salesforce_web_id_attribute_name ⇒ Object
Returns the value of attribute salesforce_web_id_attribute_name.
45 46 47 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 45 def salesforce_web_id_attribute_name @salesforce_web_id_attribute_name end |
#sync_inbound_delete ⇒ Object
Specify whether or not we sync deletes inbound from salesforce or outbound from this app Accepts either a true/false or a symbol to a method to be called
39 40 41 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 39 def sync_inbound_delete @sync_inbound_delete end |
#sync_outbound_delete ⇒ Object
Returns the value of attribute sync_outbound_delete.
40 41 42 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 40 def sync_outbound_delete @sync_outbound_delete end |
#unscoped_updates ⇒ Object
Specify whether we should use an unscoped find to update an object (useful with paranoia gem to handle undeletes)
43 44 45 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 43 def unscoped_updates @unscoped_updates end |
Instance Method Details
#salesforce_update(attributes = {}) ⇒ Object
Accepts values from an outbound message hash and will either update an existing record OR create a new record Firstly attempts to find an object by the salesforce_id attribute Secondly attempts to look an object up by it’s ID (WebId__c in outbound message) Lastly it will create a new record setting it’s salesforce_id
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/salesforce_ar_sync/salesforce_sync.rb', line 60 def salesforce_update(attributes={}) raise ArgumentError, "#{salesforce_id_attribute_name} parameter required" if attributes[salesforce_id_attribute_name].blank? data_source = unscoped_updates ? unscoped : self object = data_source.find_by(salesforce_id: attributes[salesforce_id_attribute_name]) object ||= data_source.find_by(activerecord_web_id_attribute_name => attributes[salesforce_web_id_attribute_name]) if salesforce_sync_web_id? && attributes[salesforce_web_id_attribute_name] if object.nil? object = new salesforce_default_attributes_for_create.merge(:salesforce_id => attributes[salesforce_id_attribute_name]).each_pair do |k, v| object.send("#{k}=", v) end end object.salesforce_process_update(attributes) if object && (object.salesforce_updated_at.nil? || (object.salesforce_updated_at && object.salesforce_updated_at < Time.parse(attributes[:SystemModstamp]))) end |