From 19cd7ae41ee82344c3312e3f49c260d073806165 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Tue, 15 Jun 2010 02:10:14 +0200 Subject: [PATCH] trying to refine design doc change detection --- lib/couchrest/mixins/design_doc.rb | 8 +++++++- spec/couchrest/extended_doc_view_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/couchrest/mixins/design_doc.rb b/lib/couchrest/mixins/design_doc.rb index d999684..47f3d5a 100644 --- a/lib/couchrest/mixins/design_doc.rb +++ b/lib/couchrest/mixins/design_doc.rb @@ -98,7 +98,7 @@ module CouchRest if saved changes = force design_doc['views'].each do |name, view| - if saved['views'][name] != view + if !compare_views(saved['views'][name], view) changes = true saved['views'][name] = view end @@ -114,6 +114,12 @@ module CouchRest 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 diff --git a/spec/couchrest/extended_doc_view_spec.rb b/spec/couchrest/extended_doc_view_spec.rb index 16ffec0..14808e3 100644 --- a/spec/couchrest/extended_doc_view_spec.rb +++ b/spec/couchrest/extended_doc_view_spec.rb @@ -33,6 +33,27 @@ describe "ExtendedDocument views" do doc = Article.database.get Article.design_doc.id doc['views']['by_date'].should_not be_nil 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 view = Article.by_date :raw => true view['rows'].length.should == 4