Fixing proxy and view bug

improve_associations
Sam Lown 2010-06-22 14:15:30 +02:00
parent 42ca7bca73
commit 3d46db1104
2 changed files with 15 additions and 4 deletions

View File

@ -16,15 +16,14 @@ module CouchRest
def validate_each(document, attribute, value)
view_name = options[:view].nil? ? "by_#{attribute}" : options[:view]
# Determine the base of the search
base = options[:proxy].nil? ? @klass : document.instance_eval(options[:proxy])
unless @klass.has_view?(view_name)
if base.respond_to?(:has_view?) && !base.has_view?(view_name)
raise "View #{document.class.name}.#{options[:view]} does not exist!" unless options[:view].nil?
@klass.view_by attribute
end
# Determine the base of the search
base = options[:proxy].nil? ? @klass : document.instance_eval(options[:proxy])
docs = base.view(view_name, :key => value, :limit => 2, :include_docs => false)['rows']
return if docs.empty?

View File

@ -57,6 +57,7 @@ describe "Validations" do
@obj.class.should_receive('view').and_return({'rows' => [ ]})
@obj.valid?
end
end
context "with a proxy parameter" do
@ -65,6 +66,17 @@ describe "Validations" do
proxy = @obj.should_receive('proxy').and_return(@obj.class)
@obj.valid?.should be_true
end
it "should allow specific view" do
@obj = WithUniqueValidationProxy.new(:title => 'test 7')
@obj.class.should_not_receive('view_by')
proxy = mock('Proxy')
@obj.should_receive('proxy').and_return(proxy)
proxy.should_receive('has_view?').and_return(true)
proxy.should_receive('view').and_return({'rows' => [ ]})
@obj.valid?
end
end