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
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -24,37 +24,37 @@ describe CouchRest::FileManager do
|
|||
end
|
||||
end
|
||||
|
||||
describe CouchRest::FileManager, "generating an app" do
|
||||
before(:all) do
|
||||
@appdir = FIXTURE_PATH + '/couchapp/template-app'
|
||||
`rm -rf #{@appdir}`
|
||||
`mkdir -p #{@appdir}`
|
||||
CouchRest::FileManager.generate_app(@appdir)
|
||||
end
|
||||
it "should create an attachments directory" do
|
||||
Dir["#{@appdir}/*"].select{|x|x =~ /_attachments/}.length.should == 1
|
||||
end
|
||||
it "should create a views directory" do
|
||||
Dir["#{@appdir}/*"].select{|x|x =~ /views/}.length.should == 1
|
||||
end
|
||||
it "should create a foo directory" do
|
||||
Dir["#{@appdir}/*"].select{|x|x =~ /foo/}.length.should == 1
|
||||
end
|
||||
it "should create index.html" do
|
||||
html = File.open("#{@appdir}/_attachments/index.html").read
|
||||
html.should match(/DOCTYPE/)
|
||||
end
|
||||
it "should create bar.txt" do
|
||||
html = File.open("#{@appdir}/foo/bar.txt").read
|
||||
html.should match(/Couchapp will/)
|
||||
end
|
||||
it "should create an example view" do
|
||||
map = File.open("#{@appdir}/views/example/map.js").read
|
||||
map.should match(/function\(doc\)/)
|
||||
reduce = File.open("#{@appdir}/views/example/reduce.js").read
|
||||
reduce.should match(/rereduce/)
|
||||
end
|
||||
end
|
||||
# describe CouchRest::FileManager, "generating an app" do
|
||||
# before(:all) do
|
||||
# @appdir = FIXTURE_PATH + '/couchapp/template-app'
|
||||
# `rm -rf #{@appdir}`
|
||||
# `mkdir -p #{@appdir}`
|
||||
# CouchRest::FileManager.generate_app(@appdir)
|
||||
# end
|
||||
# it "should create an attachments directory" do
|
||||
# Dir["#{@appdir}/*"].select{|x|x =~ /_attachments/}.length.should == 1
|
||||
# end
|
||||
# it "should create a views directory" do
|
||||
# Dir["#{@appdir}/*"].select{|x|x =~ /views/}.length.should == 1
|
||||
# end
|
||||
# it "should create a foo directory" do
|
||||
# Dir["#{@appdir}/*"].select{|x|x =~ /foo/}.length.should == 1
|
||||
# end
|
||||
# it "should create index.html" do
|
||||
# html = File.open("#{@appdir}/_attachments/index.html").read
|
||||
# html.should match(/DOCTYPE/)
|
||||
# end
|
||||
# it "should create bar.txt" do
|
||||
# html = File.open("#{@appdir}/foo/bar.txt").read
|
||||
# html.should match(/Couchapp will/)
|
||||
# end
|
||||
# it "should create an example view" do
|
||||
# map = File.open("#{@appdir}/views/example/map.js").read
|
||||
# map.should match(/function\(doc\)/)
|
||||
# reduce = File.open("#{@appdir}/views/example/reduce.js").read
|
||||
# reduce.should match(/rereduce/)
|
||||
# end
|
||||
# end
|
||||
|
||||
describe CouchRest::FileManager, "pushing an app" do
|
||||
before(:all) do
|
||||
|
|
Loading…
Reference in a new issue