Ensuring that views with two properties will quick find

This commit is contained in:
Sam Lown 2010-06-15 01:35:14 +02:00
parent 5d7fa3c198
commit d0f8b0be68
3 changed files with 15 additions and 2 deletions

View file

@ -130,7 +130,7 @@ module CouchRest
if has_view?(m) if has_view?(m)
query = args.shift || {} query = args.shift || {}
return view(m, query, *args, &block) return view(m, query, *args, &block)
elsif m.to_s =~ /^find_(.+)/ elsif m.to_s =~ /^find_(by_.+)/
view_name = $1 view_name = $1
if has_view?(view_name) if has_view?(view_name)
query = {:key => args.first, :limit => 1} query = {:key => args.first, :limit => 1}

View file

@ -101,7 +101,7 @@ describe "ExtendedDocument views" do
before(:all) do before(:all) do
reset_test_db! reset_test_db!
%w{aaa bbb ddd eee}.each do |title| %w{aaa bbb ddd eee}.each do |title|
Course.new(:title => title).save Course.new(:title => title, :active => (title == 'bbb')).save
end end
end end
@ -116,6 +116,17 @@ describe "ExtendedDocument views" do
course.should be_nil course.should be_nil
end end
it "should peform search on view with two properties" do
course = Course.find_by_title_and_active(['bbb', true])
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_and_active(['bbb', false])
course.should be_nil
end
it "should raise exception if view not present" do it "should raise exception if view not present" do
lambda { Course.find_by_foobar('123') }.should raise_error(NoMethodError) lambda { Course.find_by_foobar('123') }.should raise_error(NoMethodError)
end end

View file

@ -19,5 +19,7 @@ class Course < CouchRest::ExtendedDocument
property :klass, :type => 'Class' property :klass, :type => 'Class'
view_by :title view_by :title
view_by :title, :active
view_by :dept, :ducktype => true view_by :dept, :ducktype => true
end end