Skip to content
Browse files

Adding a Picklist property type. Retrieving the possible values from SF.

  • Loading branch information...
1 parent a9c850c commit ce8b7ec3524c01eafc1d21bf91a72e2bf92ebe70 @fredjean fredjean committed with jpr5 Sep 19, 2011
View
7 lib/dm-salesforce-adapter/connection.rb
@@ -18,6 +18,7 @@ def on_simple_outbound
def initialize(username, password, wsdl_path, api_dir, organization_id = nil)
@wrapper = SoapWrapper.new("SalesforceAPI", "Soap", wsdl_path, api_dir)
@username, @password, @organization_id = URI.unescape(username), password, organization_id
+ @metadata = {}
login
end
attr_reader :user_id, :user_details
@@ -89,6 +90,12 @@ def delete(keys)
call_api(:delete, DeleteError, "deleting", keys)
end
+ def metadata(sf_object_name)
+ with_reconnection do
+ @metadata[sf_object_name] ||= driver.describeSObject(SalesforceAPI::DescribeSObject.new(sf_object_name)).result
+ end
+ end
+
private
def driver
View
1 lib/dm-salesforce-adapter/property.rb
@@ -5,3 +5,4 @@ module Property
require 'dm-salesforce-adapter/property/serial'
require 'dm-salesforce-adapter/property/boolean'
+require 'dm-salesforce-adapter/property/picklist'
View
14 lib/dm-salesforce-adapter/property/picklist.rb
@@ -0,0 +1,14 @@
+module SalesforceAdapter::Property
+ class Picklist < ::DataMapper::Property::String
+ def values_for_picklist
+ @metadata ||= connection.metadata(model.salesforce_class)
+ @values ||= @metadata.fields.select { |f| f.name =~ /#{self.field}/i || f.name =~ /#{self.name.to_s}/i}.first.picklistValues rescue []
+ @values.map {|value| {:value => value.value, :label => value.label}}
+ end
+
+ private
+ def connection
+ model.repository.adapter.connection
+ end
+ end
+end

0 comments on commit ce8b7ec

Please sign in to comment.
Something went wrong with that request. Please try again.