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)
|
def self.method_missing(m, *args, &block)
|
||||||
if has_view?(m)
|
if has_view?(m)
|
||||||
query = args.shift || {}
|
query = args.shift || {}
|
||||||
view(m, query, *args, &block)
|
return view(m, query, *args, &block)
|
||||||
else
|
elsif m.to_s =~ /^find_(.+)/
|
||||||
super
|
view_name = $1
|
||||||
|
if has_view?(view_name)
|
||||||
|
query = {:key => args.first, :limit => 1}
|
||||||
|
return view(view_name, query).first
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
### instance methods
|
### instance methods
|
||||||
|
|
|
@ -5,7 +5,7 @@ gem 'couchrest'
|
||||||
|
|
||||||
require 'couchrest'
|
require 'couchrest'
|
||||||
|
|
||||||
require 'active_support'
|
require 'activesupport'
|
||||||
require 'mime/types'
|
require 'mime/types'
|
||||||
require "enumerator"
|
require "enumerator"
|
||||||
|
|
||||||
|
|
|
@ -97,6 +97,30 @@ describe "ExtendedDocument views" do
|
||||||
end
|
end
|
||||||
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
|
describe "a ducktype view" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
reset_test_db!
|
reset_test_db!
|
||||||
|
|
Loading…
Reference in a new issue