Fix up set_default to not munge existing values.

This commit is contained in:
Max Aller 2009-01-04 00:24:13 -08:00 committed by Chris Anderson
parent b28e40bb96
commit f3bc7f8eba
2 changed files with 24 additions and 1 deletions

View file

@ -506,9 +506,10 @@ module CouchRest
private
def apply_defaults
return unless new_document?
if self.class.default
self.class.default.each do |k,v|
self[k] = v
self[k] = v unless self.key?(k.to_s)
end
end
end

View file

@ -22,6 +22,7 @@ class WithTemplateAndUniqueID < CouchRest::Model
'more-template' => [1,2,3]
})
key_accessor :preset
key_accessor :has_no_default
end
class Question < CouchRest::Model
@ -222,10 +223,31 @@ describe CouchRest::Model do
describe "a model with template values" do
before(:all) do
@tmpl = WithTemplateAndUniqueID.new
@tmpl2 = WithTemplateAndUniqueID.new(:preset => 'not_value', 'important-field' => '1')
end
it "should have fields set when new" do
@tmpl.preset.should == 'value'
end
it "shouldn't override explicitly set values" do
@tmpl2.preset.should == 'not_value'
end
it "shouldn't override existing documents" do
@tmpl2.save
tmpl2_reloaded = WithTemplateAndUniqueID.get(@tmpl2.id)
@tmpl2.preset.should == 'not_value'
tmpl2_reloaded.preset.should == 'not_value'
end
it "shouldn't fill in existing documents" do
@tmpl2.save
# If user adds a new default value, shouldn't be retroactively applied to
# documents upon fetching
WithTemplateAndUniqueID.set_default({:has_no_default => 'giraffe'})
tmpl2_reloaded = WithTemplateAndUniqueID.get(@tmpl2.id)
@tmpl2.has_no_default.should be_nil
tmpl2_reloaded.has_no_default.should be_nil
WithTemplateAndUniqueID.new.has_no_default.should == 'giraffe'
end
end
describe "getting a model" do