Module: SalesforceRecord::Finder::ClassMethods
- Defined in:
- lib/salesforce_record/finder.rb
Instance Method Summary collapse
-
#find(id) ⇒ Object
Returns the object matching a salesforce id.
-
#format_where_clause(attribute, value) ⇒ Object
Create the query string for an attribute/value pair Returns something along the lines of “key.attribute=‘value’”, to be injected in a WHERE clause.
-
#query_string(query) ⇒ Object
Creates an soql query for the resource.
-
#remote_attribute_name(attribute) ⇒ Object
Aliases : the remote name for an attribute.
-
#salesforce_query_key ⇒ Object
The key used to namespace the salesforce queries : first character of the table name i.e : Lead => l Used in : Select l.Type_de_paiement__c from Lead l where Id=‘id-goes-here’.
-
#where(query) ⇒ Object
Returns a list of object matching a specific query.
Instance Method Details
#find(id) ⇒ Object
Returns the object matching a salesforce id
14 15 16 |
# File 'lib/salesforce_record/finder.rb', line 14 def find(id) where(:Id => id).first end |
#format_where_clause(attribute, value) ⇒ Object
Create the query string for an attribute/value pair Returns something along the lines of “key.attribute=‘value’”, to be injected in a WHERE clause
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/salesforce_record/finder.rb', line 82 def format_where_clause(attribute, value) q = "#{remote_attribute_name(attribute)}=" #exact match # No escaping boolean values if value.is_a?(TrueClass) || value.is_a?(FalseClass) q << value.to_s elsif value.is_a? Date q << value.strftime("%Y-%m-%d") elsif value.nil? q << "NULL" else q << "'#{value}'" end q end |
#query_string(query) ⇒ Object
Creates an soql query for the resource. The query matchers are the argument
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/salesforce_record/finder.rb', line 35 def query_string(query) # The first letter of the model used # i.e : Lead => l # Used in the query : Select l.Type_de_paiement__c from Lead l where Id='id-goes-here' key = # Build the base query : select all the attributes in the model table (except "type" : returned but should not be queried) query_string = "Select " query_string << (salesforce_attributes - [:type]).map{|attribute| remote_attribute_name(attribute)}.join(", ") query_string << " from #{salesforce_table_name} #{salesforce_query_key}" # Add the selectors if query.is_a? String soql_selector = query else soql_selector = query.map{|attribute, value| format_where_clause(attribute, value)}.join(' AND ') end query_string << " WHERE #{soql_selector}" query_string end |
#remote_attribute_name(attribute) ⇒ Object
Aliases : the remote name for an attribute
69 70 71 72 73 74 75 76 |
# File 'lib/salesforce_record/finder.rb', line 69 def remote_attribute_name(attribute) if (field = self.get_field attribute) name_without_alias = field.remote_name else name_without_alias = attribute end "#{salesforce_query_key}.#{name_without_alias}" end |
#salesforce_query_key ⇒ Object
The key used to namespace the salesforce queries : first character of the table name i.e : Lead => l Used in : Select l.Type_de_paiement__c from Lead l where Id=‘id-goes-here’
64 65 66 |
# File 'lib/salesforce_record/finder.rb', line 64 def salesforce_query_key self.salesforce_table_name.to_s[0, 1].downcase end |
#where(query) ⇒ Object
Returns a list of object matching a specific query
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/salesforce_record/finder.rb', line 20 def where(query) # Build the matching query string query_string = query_string(query) # Run the query and get the results (always an array) sf_results = salesforce_adapter.query(query_string) # Create new objets with the results and return them sf_results.map{|sf_attributes| from_salesforce(sf_attributes)} end |