From 4a1de8c1bae6d53076783294af67412921eb0e0a Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Mon, 29 Sep 2008 17:46:33 -0700 Subject: [PATCH] couchrest query params passed through by_xxxx views --- lib/couchrest/core/model.rb | 14 +++++++------- spec/couchrest/core/model_spec.rb | 8 +++++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index 3da5430..1e68daa 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -148,18 +148,18 @@ module CouchRest @@design_doc_fresh = false self.meta_class.instance_eval do - define_method method_name do |args| - args ||= {} + define_method method_name do |*args| + opts = args[0] || {} unless @@design_doc_fresh refresh_design_doc end - raw = args.delete(:raw) + raw = opts.delete(:raw) view_name = "#{type}/#{method_name}" if raw - fetch_view(view_name) + fetch_view(view_name, opts) else - view = fetch_view(view_name) + view = fetch_view(view_name, opts) # TODO this can be optimized once the include-docs patch is applied view['rows'].collect{|r|new(database.get(r['id']))} end @@ -169,10 +169,10 @@ module CouchRest private - def fetch_view view_name + def fetch_view view_name, opts retryable = true begin - database.view(view_name) + database.view(view_name, opts) # the design doc could have been deleted by a rouge process rescue RestClient::ResourceNotFound => e if retryable diff --git a/spec/couchrest/core/model_spec.rb b/spec/couchrest/core/model_spec.rb index 6be7d21..0438049 100644 --- a/spec/couchrest/core/model_spec.rb +++ b/spec/couchrest/core/model_spec.rb @@ -236,7 +236,13 @@ describe CouchRest::Model do end it "should sort correctly" do articles = Article.by_user_id_and_date - # articles.should == 'x' + articles.collect{|a|a.doc['user_id']}.should == ['aaron', 'aaron', 'quentin', 'quentin'] + articles[1].title.should == 'not junk' + end + it "should be queryable with couchrest options" do + articles = Article.by_user_id_and_date :count => 1, :startkey => 'quentin' + articles.length.should == 1 + articles[0].title.should == "even more interesting" end end end \ No newline at end of file