Adding support for find_by quick view access searches

This commit is contained in:
Sam Lown 2010-06-15 01:15:30 +02:00
parent 72fe43d64a
commit 5dfd18cd71
3 changed files with 33 additions and 4 deletions

View file

@ -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

View file

@ -5,7 +5,7 @@ gem 'couchrest'
require 'couchrest'
require 'active_support'
require 'activesupport'
require 'mime/types'
require "enumerator"

View file

@ -97,6 +97,30 @@ describe "ExtendedDocument views" do
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
reset_test_db!