Adding support for find_by quick view access searches
This commit is contained in:
parent
72fe43d64a
commit
5dfd18cd71
|
@ -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
|
||||
|
|
|
@ -5,7 +5,7 @@ gem 'couchrest'
|
|||
|
||||
require 'couchrest'
|
||||
|
||||
require 'active_support'
|
||||
require 'activesupport'
|
||||
require 'mime/types'
|
||||
require "enumerator"
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue