added an automated way to mark design docs as dirty after the db was reset

This commit is contained in:
Matt Aimonetti 2009-05-27 18:16:50 -07:00
parent e48a6c8866
commit c35c35157a
10 changed files with 41 additions and 25 deletions

View file

@ -236,6 +236,16 @@ module CouchRest
copy_doc(doc, dest)
end
def extended_document_classes
@extended_document_classes ||= []
end
# store extended document classes so we can clear
# their freshness when we reset the DB
def register_extended_document_class(klass)
extended_document_classes << klass
end
# Compact the database, removing old document revisions and optimizing space use.
def compact!
CouchRest.post "#{@uri}/_compact"
@ -271,11 +281,16 @@ module CouchRest
# DELETE the database itself. This is not undoable and could be rather
# catastrophic. Use with care!
def delete!
clear_extended_doc_fresh_cache
CouchRest.delete @uri
end
private
def clear_extended_doc_fresh_cache
extended_document_classes.each{|klass| klass.design_doc_fresh = false if klass.respond_to?(:design_doc_fresh=) }
end
def uri_for_attachment(doc, name)
if doc.is_a?(String)
puts "CouchRest::Database#fetch_attachment will eventually require a doc as the first argument, not a doc.id"

View file

@ -20,6 +20,15 @@ module CouchRest
subklass.properties = self.properties.dup
end
EOS
# re opening the use_database method so we can register our class
subklass.class_eval <<-EOS, __FILE__, __LINE__
def self.use_database(db)
super
db.register_extended_document_class(self) if db.respond_to?(:register_extended_document_class) && !db.extended_document_classes.include?(self)
end
EOS
end
# Accessors
@ -45,6 +54,8 @@ module CouchRest
end
end
# Automatically set <tt>updated_at</tt> and <tt>created_at</tt> fields
# on the document whenever saving occurs. CouchRest uses a pretty

View file

@ -61,12 +61,12 @@ describe "ExtendedDocument views" do
describe "another model with a simple view" do
before(:all) do
reset_test_db!
Course.design_doc_fresh = false
%w{aaa bbb ddd eee}.each do |title|
Course.new(:title => title).save
end
end
it "should make the design doc upon first query" do
Course.design_doc_fresh = false
Course.by_title
doc = Course.design_doc
doc['views']['all']['map'].should include('Course')
@ -74,13 +74,11 @@ describe "ExtendedDocument views" do
it "should can query via view" do
# register methods with method-missing, for local dispatch. method
# missing lookup table, no heuristics.
Course.design_doc_fresh = false
view = Course.view :by_title
designed = Course.by_title
view.should == designed
end
it "should get them" do
Course.design_doc_fresh = false
rs = Course.by_title
rs.length.should == 4
end
@ -104,7 +102,7 @@ describe "ExtendedDocument views" do
describe "a ducktype view" do
before(:all) do
reset_test_db!
@id = TEST_SERVER.default_database.save_doc({:dept => true})['id']
@id = DB.save_doc({:dept => true})['id']
end
it "should setup" do
duck = Course.get(@id) # from a different db
@ -126,7 +124,7 @@ describe "ExtendedDocument views" do
describe "a model class not tied to a database" do
before(:all) do
reset_test_db!
@db = TEST_SERVER.default_database
@db = DB
%w{aaa bbb ddd eee}.each do |title|
u = Unattached.new(:title => title)
u.database = @db
@ -191,7 +189,7 @@ describe "ExtendedDocument views" do
end
it "should be able to cleanup the db/bump the revision number" do
# if the previous specs were not run, the model_design_doc will be blank
Unattached.use_database TEST_SERVER.default_database
Unattached.use_database DB
Unattached.view_by :questions
Unattached.by_questions(:database => @db)
original_revision = Unattached.model_design_doc(@db)['_rev']
@ -204,8 +202,8 @@ describe "ExtendedDocument views" do
before(:all) do
reset_test_db!
# setup the class default doc to save the design doc
Unattached.use_database TEST_SERVER.default_database
@us = Unattached.on(TEST_SERVER.default_database)
Unattached.use_database DB
@us = Unattached.on(DB)
%w{aaa bbb ddd eee}.each do |title|
u = @us.new(:title => title)
u.save
@ -213,12 +211,10 @@ describe "ExtendedDocument views" do
end
end
it "should query all" do
Unattached.design_doc_fresh = false
rs = @us.all
rs.length.should == 4
end
it "should make the design doc upon first query" do
Unattached.design_doc_fresh = false
@us.by_title
doc = @us.design_doc
doc['views']['all']['map'].should include('Unattached')
@ -265,7 +261,6 @@ describe "ExtendedDocument views" do
describe "a model with a compound key view" do
before(:all) do
Article.design_doc_fresh = false
Article.by_user_id_and_date.each{|a| a.destroy(true)}
Article.database.bulk_delete
written_at = Time.now - 24 * 3600 * 7
@ -309,19 +304,16 @@ describe "ExtendedDocument views" do
end
end
it "should be available raw" do
Article.design_doc_fresh = false
view = Article.by_tags :raw => true
view['rows'].length.should == 5
end
it "should be default to :reduce => false" do
Article.design_doc_fresh = false
ars = Article.by_tags
ars.first.tags.first.should == 'cool'
end
it "should be raw when reduce is true" do
Article.design_doc_fresh = false
view = Article.by_tags :reduce => true, :group => true
view['rows'].find{|r|r['key'] == 'cool'}['value'].should == 3
end
@ -331,7 +323,6 @@ describe "ExtendedDocument views" do
describe "adding a view" do
before(:each) do
reset_test_db!
Article.design_doc_fresh = false
Article.by_date
@original_doc_rev = Article.model_design_doc['_rev']
@design_docs = Article.database.documents :startkey => "_design/", :endkey => "_design/\u9999"

View file

@ -1,5 +1,5 @@
class Article < CouchRest::ExtendedDocument
use_database TEST_SERVER.default_database
use_database DB
unique_id :slug
view_by :date, :descending => true

View file

@ -5,7 +5,7 @@ class Card < CouchRest::ExtendedDocument
auto_validate!
# Set the default database to use
use_database TEST_SERVER.default_database
use_database DB
# Official Schema
property :first_name

View file

@ -2,7 +2,7 @@ class Cat < CouchRest::ExtendedDocument
include ::CouchRest::Validation
# Set the default database to use
use_database TEST_SERVER.default_database
use_database DB
property :name
property :toys, :cast_as => ['CatToy'], :default => []

View file

@ -1,5 +1,5 @@
class Event < CouchRest::ExtendedDocument
use_database TEST_SERVER.default_database
use_database DB
property :subject
property :occurs_at, :cast_as => 'Time', :send => 'parse'

View file

@ -3,7 +3,7 @@ class Invoice < CouchRest::ExtendedDocument
include CouchRest::Validation
# Set the default database to use
use_database TEST_SERVER.default_database
use_database DB
# Official Schema
property :client_name

View file

@ -3,7 +3,7 @@ class Service < CouchRest::ExtendedDocument
include CouchRest::Validation
auto_validate!
# Set the default database to use
use_database TEST_SERVER.default_database
use_database DB
# Official Schema
property :name, :length => 4...20

View file

@ -12,6 +12,7 @@ unless defined?(FIXTURE_PATH)
TESTDB = 'couchrest-test'
TEST_SERVER = CouchRest.new
TEST_SERVER.default_database = TESTDB
DB = TEST_SERVER.database(TESTDB)
end
class Basic < CouchRest::ExtendedDocument
@ -19,10 +20,8 @@ class Basic < CouchRest::ExtendedDocument
end
def reset_test_db!
cr = TEST_SERVER
db = cr.database(TESTDB)
db.recreate! rescue nil
db
DB.recreate! rescue nil
DB
end
Spec::Runner.configure do |config|