Class proxy was not setting database on result sets
This commit is contained in:
parent
17dac85a02
commit
6d571a7d81
|
@ -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
|
||||||
|
|
|
@ -25,39 +25,33 @@ 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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "another model with a simple view" do
|
describe "another model with a simple view" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
reset_test_db!
|
reset_test_db!
|
||||||
|
@ -96,8 +90,7 @@ describe "ExtendedDocument views" do
|
||||||
courses[0]["doc"]["title"].should =='aaa'
|
courses[0]["doc"]["title"].should =='aaa'
|
||||||
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!
|
||||||
|
@ -117,7 +110,7 @@ describe "ExtendedDocument views" do
|
||||||
@as[0]['_id'].should == @id
|
@as[0]['_id'].should == @id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "a model class not tied to a database" do
|
describe "a model class not tied to a database" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
reset_test_db!
|
reset_test_db!
|
||||||
|
@ -198,7 +191,7 @@ describe "ExtendedDocument views" do
|
||||||
Unattached.model_design_doc(@db)['_rev'].should_not == original_revision
|
Unattached.model_design_doc(@db)['_rev'].should_not == original_revision
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "class proxy" do
|
describe "class proxy" 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']
|
||||||
|
@ -262,7 +285,7 @@ describe "ExtendedDocument views" do
|
||||||
@us.model_design_doc['_rev'].should_not == original_id
|
@us.model_design_doc['_rev'].should_not == original_id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "a model with a compound key view" do
|
describe "a model with a compound key view" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
Article.by_user_id_and_date.each{|a| a.destroy(true)}
|
Article.by_user_id_and_date.each{|a| a.destroy(true)}
|
||||||
|
@ -295,7 +318,7 @@ describe "ExtendedDocument views" do
|
||||||
articles[0].title.should == "even more interesting"
|
articles[0].title.should == "even more interesting"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "with a custom view" do
|
describe "with a custom view" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
@titles = ["very uniq one", "even less interesting", "some fun",
|
@titles = ["very uniq one", "even less interesting", "some fun",
|
||||||
|
@ -311,18 +334,18 @@ describe "ExtendedDocument views" do
|
||||||
view = Article.by_tags :raw => true
|
view = Article.by_tags :raw => true
|
||||||
view['rows'].length.should == 5
|
view['rows'].length.should == 5
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should be default to :reduce => false" do
|
it "should be default to :reduce => false" do
|
||||||
ars = Article.by_tags
|
ars = Article.by_tags
|
||||||
ars.first.tags.first.should == 'cool'
|
ars.first.tags.first.should == 'cool'
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should be raw when reduce is true" do
|
it "should be raw when reduce is true" do
|
||||||
view = Article.by_tags :reduce => true, :group => true
|
view = Article.by_tags :reduce => true, :group => true
|
||||||
view['rows'].find{|r|r['key'] == 'cool'}['value'].should == 3
|
view['rows'].find{|r|r['key'] == 'cool'}['value'].should == 3
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: moved to Design, delete
|
# TODO: moved to Design, delete
|
||||||
describe "adding a view" do
|
describe "adding a view" do
|
||||||
before(:each) do
|
before(:each) do
|
||||||
|
@ -344,7 +367,7 @@ describe "ExtendedDocument views" do
|
||||||
Article.design_doc["views"].keys.should include("by_updated_at")
|
Article.design_doc["views"].keys.should include("by_updated_at")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "with a collection" do
|
describe "with a collection" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
reset_test_db!
|
reset_test_db!
|
||||||
|
@ -354,7 +377,7 @@ describe "ExtendedDocument views" do
|
||||||
a = Article.new(:title => title, :date => Date.today)
|
a = Article.new(:title => title, :date => Date.today)
|
||||||
a.save
|
a.save
|
||||||
end
|
end
|
||||||
|
|
||||||
titles = ["yesterday very uniq one", "yesterday really interesting", "yesterday some fun",
|
titles = ["yesterday very uniq one", "yesterday really interesting", "yesterday some fun",
|
||||||
"yesterday really awesome", "yesterday crazy bob", "yesterday this rocks"]
|
"yesterday really awesome", "yesterday crazy bob", "yesterday this rocks"]
|
||||||
titles.each_with_index do |title,i|
|
titles.each_with_index do |title,i|
|
||||||
|
@ -390,11 +413,11 @@ describe "ExtendedDocument views" do
|
||||||
articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
|
articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
|
||||||
:per_page => 3, :descending => true, :key => Date.today, :include_docs => true)
|
:per_page => 3, :descending => true, :key => Date.today, :include_docs => true)
|
||||||
articles.size.should == 3
|
articles.size.should == 3
|
||||||
|
|
||||||
articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
|
articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
|
||||||
:per_page => 3, :page => 2, :descending => true, :key => Date.today, :include_docs => true)
|
:per_page => 3, :page => 2, :descending => true, :key => Date.today, :include_docs => true)
|
||||||
articles.size.should == 3
|
articles.size.should == 3
|
||||||
|
|
||||||
articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
|
articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
|
||||||
:per_page => 3, :page => 3, :descending => true, :key => Date.today, :include_docs => true)
|
:per_page => 3, :page => 3, :descending => true, :key => Date.today, :include_docs => true)
|
||||||
articles.size.should == 1
|
articles.size.should == 1
|
||||||
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue