Skip to content

Commit

Permalink
Set picklist field to default picklist value.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejholmes committed Jun 7, 2012
1 parent 031a22a commit 0695d2b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/databasedotcom/sobject/sobject.rb
Expand Up @@ -14,7 +14,11 @@ def ==(other)
def initialize(attrs = {})
super()
self.class.description["fields"].each do |field|
self.send("#{field["name"]}=", field["defaultValueFormula"])
if field['type'] == 'picklist' && picklist_option = field['picklistValues'].find { |p| p['defaultValue'] }
self.send("#{field["name"]}=", picklist_option['value'])
else
self.send("#{field["name"]}=", field["defaultValueFormula"])
end
end
self.attributes=(attrs)
end
Expand Down
Expand Up @@ -753,7 +753,7 @@
"value" : "one",
"active" : true,
"label" : "one",
"defaultValue" : false,
"defaultValue" : true,
"validFor" : null
}, {
"value" : "two",
Expand Down
10 changes: 8 additions & 2 deletions spec/lib/sobject/sobject_spec.rb
Expand Up @@ -46,8 +46,14 @@ class TestClass < Databasedotcom::Sobject::Sobject

describe "default values" do
response["fields"].each do |f|
it "sets #{f['name']} to #{f['defaultValueFormula'] ? f['defaultValueFormula'] : 'nil'}" do
@sobject.send(f["name"].to_sym).should == f["defaultValueFormula"]
if f['type'] == 'picklist' && picklist_option = f['picklistValues'].find { |p| p['defaultValue'] }
it "sets #{f['name']} to #{picklist_option['value']}" do
@sobject.send(f['name'].to_sym).should == picklist_option['value']
end
else
it "sets #{f['name']} to #{f['defaultValueFormula'] ? f['defaultValueFormula'] : 'nil'}" do
@sobject.send(f["name"].to_sym).should == f["defaultValueFormula"]
end
end
end
end
Expand Down

0 comments on commit 0695d2b

Please sign in to comment.