From 3d46db11043d99eb7c79fc02acc5b789abb1b113 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Tue, 22 Jun 2010 14:15:30 +0200 Subject: [PATCH] Fixing proxy and view bug --- lib/couchrest/model/validations/uniqueness.rb | 7 +++---- spec/couchrest/validations.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/couchrest/model/validations/uniqueness.rb b/lib/couchrest/model/validations/uniqueness.rb index 01c7547..1bc5270 100644 --- a/lib/couchrest/model/validations/uniqueness.rb +++ b/lib/couchrest/model/validations/uniqueness.rb @@ -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? diff --git a/spec/couchrest/validations.rb b/spec/couchrest/validations.rb index 1b64f35..ef01df6 100644 --- a/spec/couchrest/validations.rb +++ b/spec/couchrest/validations.rb @@ -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