Permalink
Browse files

send createable attributes on create, updateable attributes on update

  • Loading branch information...
1 parent 9912f25 commit e121d4b525419957e101a635446365a2cb701465 @dburkes dburkes committed Sep 11, 2011
@@ -77,7 +77,8 @@ def update_attributes(new_attrs)
# c.save
def save
attr_hash = {}
- self.class.description["fields"].select { |f| f["updateable"] }.collect { |f| f["name"] }.each { |attr| attr_hash[attr] = self.send(attr) }
+ selection_attr = self.Id.nil? ? "createable" : "updateable"
+ self.class.description["fields"].select { |f| f[selection_attr] }.collect { |f| f["name"] }.each { |attr| attr_hash[attr] = self.send(attr) }
if self.Id.nil?
self.client.create(self.class, attr_hash)
@@ -135,7 +136,13 @@ def self.materialize(sobject_name)
self.description["fields"].each do |field|
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"]}
+ self.type_map[name] = {
+ :type => field["type"],
+ :label => field["label"],
+ :picklist_values => field["picklistValues"],
+ :updateable? => field["updateable"],
+ :createable? => field["createable"]
+ }
end
end
@@ -162,6 +169,11 @@ def self.updateable?(attr_name)
self.type_map_attr(attr_name, :updateable?)
end
+ # Returns true if the attribute +attr_name+ can be created. Raises ArgumentError if attribute does not exist.
+ def self.createable?(attr_name)
+ self.type_map_attr(attr_name, :createable?)
+ end
+
# Delegates to Client.find with arguments +record_id+ and self
#
# client.materialize("Car")
@@ -1076,6 +1076,86 @@
"dependentPicklist" : false,
"writeRequiresMasterRead" : false,
"sortable" : true
+ }, {
+ "length" : 0,
+ "name" : "CreateableButNotUpdateableField",
+ "type" : "datetime",
+ "defaultValue" : null,
+ "label" : "CreateableButNotUpdateableField Label",
+ "updateable" : false,
+ "calculated" : false,
+ "controllerName" : null,
+ "nillable" : true,
+ "unique" : false,
+ "precision" : 0,
+ "scale" : 0,
+ "caseSensitive" : false,
+ "byteLength" : 0,
+ "inlineHelpText" : null,
+ "nameField" : false,
+ "externalId" : false,
+ "idLookup" : false,
+ "filterable" : true,
+ "soapType" : "xsd:dateTime",
+ "createable" : true,
+ "deprecatedAndHidden" : false,
+ "autoNumber" : false,
+ "calculatedFormula" : null,
+ "defaultValueFormula" : null,
+ "defaultedOnCreate" : false,
+ "digits" : 0,
+ "groupable" : false,
+ "picklistValues" : [ ],
+ "referenceTo" : [ ],
+ "relationshipName" : null,
+ "relationshipOrder" : null,
+ "restrictedPicklist" : false,
+ "namePointing" : false,
+ "custom" : true,
+ "htmlFormatted" : false,
+ "dependentPicklist" : false,
+ "writeRequiresMasterRead" : false,
+ "sortable" : true
+ }, {
+ "length" : 0,
+ "name" : "UpdateableButNotCreateableField",
+ "type" : "datetime",
+ "defaultValue" : null,
+ "label" : "UpdateableButNotCreateableField Label",
+ "updateable" : true,
+ "calculated" : false,
+ "controllerName" : null,
+ "nillable" : true,
+ "unique" : false,
+ "precision" : 0,
+ "scale" : 0,
+ "caseSensitive" : false,
+ "byteLength" : 0,
+ "inlineHelpText" : null,
+ "nameField" : false,
+ "externalId" : false,
+ "idLookup" : false,
+ "filterable" : true,
+ "soapType" : "xsd:dateTime",
+ "createable" : false,
+ "deprecatedAndHidden" : false,
+ "autoNumber" : false,
+ "calculatedFormula" : null,
+ "defaultValueFormula" : null,
+ "defaultedOnCreate" : false,
+ "digits" : 0,
+ "groupable" : false,
+ "picklistValues" : [ ],
+ "referenceTo" : [ ],
+ "relationshipName" : null,
+ "relationshipOrder" : null,
+ "restrictedPicklist" : false,
+ "namePointing" : false,
+ "custom" : true,
+ "htmlFormatted" : false,
+ "dependentPicklist" : false,
+ "writeRequiresMasterRead" : false,
+ "sortable" : true
} ],
"label" : "Whizbang",
"custom" : true,
@@ -286,22 +286,44 @@ class TestClass < Databasedotcom::Sobject::Sobject
describe "#save" do
context "with a new object" do
+ before do
+ @obj = TestClass.new
+ @obj.client = @client
+ @obj.Name = "testname"
+ end
+
it "creates the record remotely with the set attributes" do
- obj = TestClass.new
- obj.client = @client
- obj.Name = "testname"
@client.should_receive(:create).and_return("saved")
- obj.save.should == "saved"
+ @obj.save.should == "saved"
+ end
+
+ it "includes only the createable attributes" do
+ @client.should_receive(:create) do |clazz, attrs|
+ attrs.all? {|attr, value| TestClass.createable?(attr).should be_true}
+ end
+
+ @obj.save
end
end
- context "with an object associated with a record" do
+ context "with an previously-persisted object" do
+ before do
+ @obj = TestClass.new
+ @obj.client = @client
+ @obj.Id = "rid"
+ end
+
it "updates the record with the attributes of the object" do
- obj = TestClass.new
- obj.client = @client
- obj.Id = "rid"
@client.should_receive(:update).and_return("saved updates")
- obj.save.should == "saved updates"
+ @obj.save.should == "saved updates"
+ end
+
+ it "includes only the updateable attributes" do
+ @client.should_receive(:update) do |clazz, id, attrs|
+ attrs.all? {|attr, value| TestClass.updateable?(attr).should be_true}
+ end
+
+ @obj.save
end
end
end
@@ -389,6 +411,19 @@ class TestClass < Databasedotcom::Sobject::Sobject
end
end
+ describe ".createable?" do
+ it "returns the createable flag for an attribute" do
+ TestClass.createable?("IsDeleted").should be_false
+ TestClass.createable?("Picklist_Field").should be_true
+ end
+
+ it "raises ArgumentError for unknown attributes" do
+ lambda {
+ TestClass.createable?("Foobar")
+ }.should raise_error(ArgumentError)
+ end
+ end
+
describe "#[]" do
before do
@obj = TestClass.new

0 comments on commit e121d4b

Please sign in to comment.