Merge branch 'master' of git://github.com/jchris/couchrest
* 'master' of git://github.com/jchris/couchrest: Fix up set_default to not munge existing values. commented out spec for fm:generate app
This commit is contained in:
commit
a2aa4a9a3c
3 changed files with 55 additions and 32 deletions
|
@ -506,9 +506,10 @@ module CouchRest
|
||||||
private
|
private
|
||||||
|
|
||||||
def apply_defaults
|
def apply_defaults
|
||||||
|
return unless new_document?
|
||||||
if self.class.default
|
if self.class.default
|
||||||
self.class.default.each do |k,v|
|
self.class.default.each do |k,v|
|
||||||
self[k] = v
|
self[k] = v unless self.key?(k.to_s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,6 +22,7 @@ class WithTemplateAndUniqueID < CouchRest::Model
|
||||||
'more-template' => [1,2,3]
|
'more-template' => [1,2,3]
|
||||||
})
|
})
|
||||||
key_accessor :preset
|
key_accessor :preset
|
||||||
|
key_accessor :has_no_default
|
||||||
end
|
end
|
||||||
|
|
||||||
class Question < CouchRest::Model
|
class Question < CouchRest::Model
|
||||||
|
@ -222,10 +223,31 @@ describe CouchRest::Model do
|
||||||
describe "a model with template values" do
|
describe "a model with template values" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
@tmpl = WithTemplateAndUniqueID.new
|
@tmpl = WithTemplateAndUniqueID.new
|
||||||
|
@tmpl2 = WithTemplateAndUniqueID.new(:preset => 'not_value', 'important-field' => '1')
|
||||||
end
|
end
|
||||||
it "should have fields set when new" do
|
it "should have fields set when new" do
|
||||||
@tmpl.preset.should == 'value'
|
@tmpl.preset.should == 'value'
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe "getting a model" do
|
describe "getting a model" do
|
||||||
|
|
|
@ -24,37 +24,37 @@ describe CouchRest::FileManager do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe CouchRest::FileManager, "generating an app" do
|
# describe CouchRest::FileManager, "generating an app" do
|
||||||
before(:all) do
|
# before(:all) do
|
||||||
@appdir = FIXTURE_PATH + '/couchapp/template-app'
|
# @appdir = FIXTURE_PATH + '/couchapp/template-app'
|
||||||
`rm -rf #{@appdir}`
|
# `rm -rf #{@appdir}`
|
||||||
`mkdir -p #{@appdir}`
|
# `mkdir -p #{@appdir}`
|
||||||
CouchRest::FileManager.generate_app(@appdir)
|
# CouchRest::FileManager.generate_app(@appdir)
|
||||||
end
|
# end
|
||||||
it "should create an attachments directory" do
|
# it "should create an attachments directory" do
|
||||||
Dir["#{@appdir}/*"].select{|x|x =~ /_attachments/}.length.should == 1
|
# Dir["#{@appdir}/*"].select{|x|x =~ /_attachments/}.length.should == 1
|
||||||
end
|
# end
|
||||||
it "should create a views directory" do
|
# it "should create a views directory" do
|
||||||
Dir["#{@appdir}/*"].select{|x|x =~ /views/}.length.should == 1
|
# Dir["#{@appdir}/*"].select{|x|x =~ /views/}.length.should == 1
|
||||||
end
|
# end
|
||||||
it "should create a foo directory" do
|
# it "should create a foo directory" do
|
||||||
Dir["#{@appdir}/*"].select{|x|x =~ /foo/}.length.should == 1
|
# Dir["#{@appdir}/*"].select{|x|x =~ /foo/}.length.should == 1
|
||||||
end
|
# end
|
||||||
it "should create index.html" do
|
# it "should create index.html" do
|
||||||
html = File.open("#{@appdir}/_attachments/index.html").read
|
# html = File.open("#{@appdir}/_attachments/index.html").read
|
||||||
html.should match(/DOCTYPE/)
|
# html.should match(/DOCTYPE/)
|
||||||
end
|
# end
|
||||||
it "should create bar.txt" do
|
# it "should create bar.txt" do
|
||||||
html = File.open("#{@appdir}/foo/bar.txt").read
|
# html = File.open("#{@appdir}/foo/bar.txt").read
|
||||||
html.should match(/Couchapp will/)
|
# html.should match(/Couchapp will/)
|
||||||
end
|
# end
|
||||||
it "should create an example view" do
|
# it "should create an example view" do
|
||||||
map = File.open("#{@appdir}/views/example/map.js").read
|
# map = File.open("#{@appdir}/views/example/map.js").read
|
||||||
map.should match(/function\(doc\)/)
|
# map.should match(/function\(doc\)/)
|
||||||
reduce = File.open("#{@appdir}/views/example/reduce.js").read
|
# reduce = File.open("#{@appdir}/views/example/reduce.js").read
|
||||||
reduce.should match(/rereduce/)
|
# reduce.should match(/rereduce/)
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
|
|
||||||
describe CouchRest::FileManager, "pushing an app" do
|
describe CouchRest::FileManager, "pushing an app" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
|
|
Loading…
Add table
Reference in a new issue