Class proxy was not setting database on result sets

This commit is contained in:
Peter Gumeson 2009-09-09 00:08:59 -07:00 committed by Marcos Tapajós
parent 17dac85a02
commit 6d571a7d81
2 changed files with 58 additions and 24 deletions

View file

@ -56,7 +56,9 @@ module CouchRest
# Mixins::DocumentQueries # Mixins::DocumentQueries
def all(opts = {}, &block) def all(opts = {}, &block)
@klass.all({:database => @database}.merge(opts), &block) docs = @klass.all({:database => @database}.merge(opts), &block)
docs.each { |doc| doc.database = @database if doc.respond_to?(:database) } if docs
docs
end end
def count(opts = {}, &block) def count(opts = {}, &block)
@ -64,11 +66,15 @@ module CouchRest
end end
def first(opts = {}) def first(opts = {})
@klass.first({:database => @database}.merge(opts)) doc = @klass.first({:database => @database}.merge(opts))
doc.database = @database if doc && doc.respond_to?(:database)
doc
end end
def get(id) def get(id)
@klass.get(id, @database) doc = @klass.get(id, @database)
doc.database = @database if doc && doc.respond_to?(:database)
doc
end end
# Mixins::Views # Mixins::Views
@ -78,7 +84,9 @@ module CouchRest
end end
def view(name, query={}, &block) def view(name, query={}, &block)
@klass.view(name, {:database => @database}.merge(query), &block) docs = @klass.view(name, {:database => @database}.merge(query), &block)
docs.each { |doc| doc.database = @database if doc.respond_to?(:database) } if docs
docs
end end
def all_design_doc_versions def all_design_doc_versions

View file

@ -25,33 +25,27 @@ describe "ExtendedDocument views" do
written_at += 24 * 3600 written_at += 24 * 3600
end end
end end
it "should have a design doc" do it "should have a design doc" do
Article.design_doc["views"]["by_date"].should_not be_nil Article.design_doc["views"]["by_date"].should_not be_nil
end end
it "should save the design doc" do it "should save the design doc" do
Article.by_date #rescue nil Article.by_date #rescue nil
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 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
end end
it "should not include non-Articles" do it "should not include non-Articles" do
Article.database.save_doc({"date" => 1}) Article.database.save_doc({"date" => 1})
view = Article.by_date :raw => true view = Article.by_date :raw => true
view['rows'].length.should == 4 view['rows'].length.should == 4
end end
it "should return the matching objects (with default argument :descending => true)" do it "should return the matching objects (with default argument :descending => true)" do
articles = Article.by_date articles = Article.by_date
articles.collect{|a|a.title}.should == @titles.reverse articles.collect{|a|a.title}.should == @titles.reverse
end end
it "should allow you to override default args" do it "should allow you to override default args" do
articles = Article.by_date :descending => false articles = Article.by_date :descending => false
articles.collect{|a|a.title}.should == @titles articles.collect{|a|a.title}.should == @titles
@ -97,7 +91,6 @@ describe "ExtendedDocument views" do
end end
end end
describe "a ducktype view" do describe "a ducktype view" do
before(:all) do before(:all) do
reset_test_db! reset_test_db!
@ -254,6 +247,36 @@ describe "ExtendedDocument views" do
u = @us.first u = @us.first
u.title.should =~ /\A...\z/ u.title.should =~ /\A...\z/
end end
it "should set database on first retreived document" do
u = @us.first
u.database.should === DB
end
it "should set database on all retreived documents" do
@us.all.each do |u|
u.database.should === DB
end
end
it "should set database on each retreived document" do
rs = @us.by_title :startkey=>"bbb", :endkey=>"eee"
rs.length.should == 3
rs.each do |u|
u.database.should === DB
end
end
it "should set database on document retreived by id" do
u = @us.get(@first_id)
u.database.should === DB
end
it "should not attempt to set database on raw results using :all" do
@us.all(:raw => true).each do |u|
u.respond_to?(:database).should be_false
end
end
it "should not attempt to set database on raw results using view" do
@us.by_title(:raw => true).each do |u|
u.respond_to?(:database).should be_false
end
end
it "should clean up design docs left around on specific database" do it "should clean up design docs left around on specific database" do
@us.by_title @us.by_title
original_id = @us.model_design_doc['_rev'] original_id = @us.model_design_doc['_rev']
@ -408,6 +431,9 @@ describe "ExtendedDocument views" do
end end
end end
it "should provide a class method to get a collection for a view" do it "should provide a class method to get a collection for a view" do
class Article
provides_collection :article_details, 'Article', 'by_date', :descending => true, :include_docs => true
end
articles = Article.find_all_article_details(:key => Date.today) articles = Article.find_all_article_details(:key => Date.today)
articles.class.should == Array articles.class.should == Array
articles.size.should == 7 articles.size.should == 7