trying to refine design doc change detection

This commit is contained in:
Sam Lown 2010-06-15 02:10:14 +02:00
parent d0f8b0be68
commit 19cd7ae41e
2 changed files with 28 additions and 1 deletions

View file

@ -98,7 +98,7 @@ module CouchRest
if saved if saved
changes = force changes = force
design_doc['views'].each do |name, view| design_doc['views'].each do |name, view|
if saved['views'][name] != view if !compare_views(saved['views'][name], view)
changes = true changes = true
saved['views'][name] = view saved['views'][name] = view
end end
@ -114,6 +114,12 @@ module CouchRest
end end
end end
# Return true if the two views match
def compare_views(orig, repl)
return false if orig.nil? or repl.nil?
(orig['map'].to_s.strip == repl['map'].to_s.strip) && (orig['reduce'].to_s.strip == repl['reduce'].to_s.strip)
end
end # module ClassMethods end # module ClassMethods
end end

View file

@ -33,6 +33,27 @@ describe "ExtendedDocument views" do
doc = Article.database.get Article.design_doc.id doc = Article.database.get Article.design_doc.id
doc['views']['by_date'].should_not be_nil doc['views']['by_date'].should_not be_nil
end end
it "should save design doc if a view changed" do
Article.by_date
orig = Article.stored_design_doc
orig['views']['by_date']['map'] = "function() { }"
Article.database.save_doc(orig)
rev = Article.stored_design_doc['_rev']
Article.req_design_doc_refresh # prepare for re-load
Article.by_date
orig = Article.stored_design_doc
orig['views']['by_date']['map'].should eql(Article.design_doc['views']['by_date']['map'])
orig['_rev'].should_not eql(rev)
end
it "should not save design doc if not changed" do
Article.by_date
orig = Article.stored_design_doc['_rev']
Article.req_design_doc_refresh
Article.by_date
Article.stored_design_doc['_rev'].should eql(orig)
end
it "should return the matching raw view result" do it "should return the matching raw view result" do
view = Article.by_date :raw => true view = Article.by_date :raw => true
view['rows'].length.should == 4 view['rows'].length.should == 4