diff --git a/lib/couchrest/extended_document.rb b/lib/couchrest/extended_document.rb index 7515e98..4c824c1 100644 --- a/lib/couchrest/extended_document.rb +++ b/lib/couchrest/extended_document.rb @@ -129,10 +129,15 @@ module CouchRest def self.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_(.+)/ + view_name = $1 + if has_view?(view_name) + query = {:key => args.first, :limit => 1} + return view(view_name, query).first + end end + super end ### instance methods diff --git a/lib/couchrest_extended_document.rb b/lib/couchrest_extended_document.rb index e71d6fd..aa9e5e7 100644 --- a/lib/couchrest_extended_document.rb +++ b/lib/couchrest_extended_document.rb @@ -5,7 +5,7 @@ gem 'couchrest' require 'couchrest' -require 'active_support' +require 'activesupport' require 'mime/types' require "enumerator" diff --git a/spec/couchrest/extended_doc_view_spec.rb b/spec/couchrest/extended_doc_view_spec.rb index 2d3ff60..d429c0f 100644 --- a/spec/couchrest/extended_doc_view_spec.rb +++ b/spec/couchrest/extended_doc_view_spec.rb @@ -96,6 +96,30 @@ describe "ExtendedDocument views" do courses[0]["doc"]["title"].should =='aaa' end end + + describe "find a single item using a view" do + before(:all) do + reset_test_db! + %w{aaa bbb ddd eee}.each do |title| + Course.new(:title => title).save + end + end + + it "should return single matched record" do + course = Course.find_by_title('bbb') + course.should_not be_nil + course.title.should eql('bbb') # Ensure really is a Course! + end + + it "should return nil if not found" do + course = Course.find_by_title('fff') + course.should be_nil + end + + 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 before(:all) do