From 5580caf346b6228b108627d619aada0e0cbba2bd Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Fri, 18 Jun 2010 19:00:20 +0200 Subject: [PATCH] Adding missing methods to proxy, proxy needs tests --- lib/couchrest/extended_document.rb | 3 +-- lib/couchrest/mixins/class_proxy.rb | 17 ++++++++++++++--- lib/couchrest/mixins/views.rb | 12 ++++++++++++ spec/couchrest/extended_doc_view_spec.rb | 3 ++- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/couchrest/extended_document.rb b/lib/couchrest/extended_document.rb index 657fb77..808c530 100644 --- a/lib/couchrest/extended_document.rb +++ b/lib/couchrest/extended_document.rb @@ -140,8 +140,7 @@ module CouchRest elsif m.to_s =~ /^find_(by_.+)/ view_name = $1 if has_view?(view_name) - query = {:key => args.first, :limit => 1} - return view(view_name, query).first + return find_first_from_view(view_name, *args) end end super diff --git a/lib/couchrest/mixins/class_proxy.rb b/lib/couchrest/mixins/class_proxy.rb index 0333785..adc208d 100644 --- a/lib/couchrest/mixins/class_proxy.rb +++ b/lib/couchrest/mixins/class_proxy.rb @@ -47,10 +47,14 @@ module CouchRest def method_missing(m, *args, &block) if has_view?(m) query = args.shift || {} - view(m, query, *args, &block) - else - super + return view(m, query, *args, &block) + elsif m.to_s =~ /^find_(by_.+)/ + view_name = $1 + if has_view?(view_name) + return find_first_from_view(view_name, *args) + end end + super end # Mixins::DocumentQueries @@ -76,6 +80,7 @@ module CouchRest doc.database = @database if doc && doc.respond_to?(:database) doc end + alias :find :get # Mixins::Views @@ -89,6 +94,12 @@ module CouchRest docs end + def find_first_from_view(name, *args) + (args[1] ||= {})[:database] = @database + doc = @klass.find_first_from_view(name, args) + doc.database = @database if doc && doc.respond_to?(:database) + doc + end # Mixins::DesignDoc diff --git a/lib/couchrest/mixins/views.rb b/lib/couchrest/mixins/views.rb index bdcab97..a5ea221 100644 --- a/lib/couchrest/mixins/views.rb +++ b/lib/couchrest/mixins/views.rb @@ -102,6 +102,18 @@ module CouchRest fetch_view_with_docs(db, name, query, raw, &block) end + # Find the first entry that matches the provided key. + # Request like: + # + # Course.find_first_from_view('teachers', 'Fred') + # + def find_first_from_view(name, *args) + key = args[0] + query = args[1] || {} + query.update(:limit => 1, :key => key) + view(name, query).first + end + private def fetch_view_with_docs(db, name, opts, raw=false, &block) diff --git a/spec/couchrest/extended_doc_view_spec.rb b/spec/couchrest/extended_doc_view_spec.rb index 14808e3..124347b 100644 --- a/spec/couchrest/extended_doc_view_spec.rb +++ b/spec/couchrest/extended_doc_view_spec.rb @@ -126,7 +126,7 @@ describe "ExtendedDocument views" do end end - it "should return single matched record" do + it "should return single matched record with find helper" do course = Course.find_by_title('bbb') course.should_not be_nil course.title.should eql('bbb') # Ensure really is a Course! @@ -151,6 +151,7 @@ describe "ExtendedDocument views" do it "should raise exception if view not present" do lambda { Course.find_by_foobar('123') }.should raise_error(NoMethodError) end + end describe "a ducktype view" do