From fba1e53e20ade78fbbc98a451432cc7f89698be5 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Thu, 19 May 2011 00:37:17 +0200 Subject: [PATCH] Overriding couchrest's database delete method to ensure design cache is deleted --- history.md | 1 + lib/couchrest/model/design_doc.rb | 2 -- lib/couchrest/model/support/couchrest_database.rb | 13 +++++++++++++ lib/couchrest_model.rb | 1 + spec/couchrest/design_doc_spec.rb | 4 ++++ 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 lib/couchrest/model/support/couchrest_database.rb diff --git a/history.md b/history.md index 550f920..d780dd0 100644 --- a/history.md +++ b/history.md @@ -8,6 +8,7 @@ * Rails 3.1 support (Peter Williams) * Initialization blocks when creating new models (Peter Williams) * Removed railties dependency (DAddYE) + * DesignDoc cache refreshed if a database is deleted. ## 1.1.0.beta5 - 2011-04-30 diff --git a/lib/couchrest/model/design_doc.rb b/lib/couchrest/model/design_doc.rb index edcb043..b634351 100644 --- a/lib/couchrest/model/design_doc.rb +++ b/lib/couchrest/model/design_doc.rb @@ -108,8 +108,6 @@ module CouchRest } end - - end # module ClassMethods end diff --git a/lib/couchrest/model/support/couchrest_database.rb b/lib/couchrest/model/support/couchrest_database.rb new file mode 100644 index 0000000..dc75bec --- /dev/null +++ b/lib/couchrest/model/support/couchrest_database.rb @@ -0,0 +1,13 @@ +# +# Extend CouchRest's normal database delete! method to ensure any caches are +# also emptied. Given that this is a rare event, and the consequences are not +# very severe, we just completely empty the cache. +# +CouchRest::Database.class_eval do + + def delete! + Thread.current[:couchrest_design_cache] = { } + CouchRest.delete @root + end + +end diff --git a/lib/couchrest_model.rb b/lib/couchrest_model.rb index 8892ea4..956a2a6 100644 --- a/lib/couchrest_model.rb +++ b/lib/couchrest_model.rb @@ -51,6 +51,7 @@ require "couchrest/model/designs/view" # Monkey patches applied to couchrest require "couchrest/model/support/couchrest_design" +require "couchrest/model/support/couchrest_database" # Core Extensions require "couchrest/model/core_extensions/hash" diff --git a/spec/couchrest/design_doc_spec.rb b/spec/couchrest/design_doc_spec.rb index 8222931..efda852 100644 --- a/spec/couchrest/design_doc_spec.rb +++ b/spec/couchrest/design_doc_spec.rb @@ -155,6 +155,10 @@ describe "Design Documents" do Article.by_date Article.stored_design_doc['_rev'].should eql(orig) end + it "should recreate the design doc if database deleted" do + Article.database.recreate! + lambda { Article.by_date }.should_not raise_error(RestClient::ResourceNotFound) + end end describe "when auto_update_design_doc false" do