lib/databasedotcom/sobject/sobject.rb in databasedotcom-1.1.7 vs lib/databasedotcom/sobject/sobject.rb in databasedotcom-1.2.0

- old
+ new

@@ -125,31 +125,44 @@ # Returns an Array of attribute names that this Sobject has. # # client.materialize("Car") # Car.attributes #=> ["Id", "Name", "Color", "Year"] def self.attributes - self.description["fields"].collect { |f| f["name"] } + self.description["fields"].collect { |f| [f["name"], f["relationshipName"]] }.flatten.compact end + def self.register_field( name, field ) + attr_accessor name.to_sym + self.type_map[name] = { + :type => field["type"], + :label => field["label"], + :picklist_values => field["picklistValues"], + :updateable? => field["updateable"], + :createable? => field["createable"] + } + end + # Materializes the dynamically created Sobject class by adding all attribute accessors for each field as described in the description of the object on Force.com def self.materialize(sobject_name) self.cattr_accessor :description self.cattr_accessor :type_map self.cattr_accessor :sobject_name self.sobject_name = sobject_name self.description = self.client.describe_sobject(self.sobject_name) self.type_map = {} + self.description["fields"].each do |field| + + # Register normal fields name = field["name"] - attr_accessor name.to_sym - self.type_map[name] = { - :type => field["type"], - :label => field["label"], - :picklist_values => field["picklistValues"], - :updateable? => field["updateable"], - :createable? => field["createable"] - } + register_field( field["name"], field ) + + # Register relationship fields. + if( field["type"] == "reference" and field["relationshipName"] ) + register_field( field["relationshipName"], field ) + end + end end # Returns the Force.com type of the attribute +attr_name+. Raises ArgumentError if attribute does not exist. #