Fixing proxy and view bug
This commit is contained in:
parent
42ca7bca73
commit
3d46db1104
|
@ -16,15 +16,14 @@ module CouchRest
|
||||||
|
|
||||||
def validate_each(document, attribute, value)
|
def validate_each(document, attribute, value)
|
||||||
view_name = options[:view].nil? ? "by_#{attribute}" : options[:view]
|
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?
|
raise "View #{document.class.name}.#{options[:view]} does not exist!" unless options[:view].nil?
|
||||||
@klass.view_by attribute
|
@klass.view_by attribute
|
||||||
end
|
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']
|
docs = base.view(view_name, :key => value, :limit => 2, :include_docs => false)['rows']
|
||||||
return if docs.empty?
|
return if docs.empty?
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,7 @@ describe "Validations" do
|
||||||
@obj.class.should_receive('view').and_return({'rows' => [ ]})
|
@obj.class.should_receive('view').and_return({'rows' => [ ]})
|
||||||
@obj.valid?
|
@obj.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with a proxy parameter" do
|
context "with a proxy parameter" do
|
||||||
|
@ -65,6 +66,17 @@ describe "Validations" do
|
||||||
proxy = @obj.should_receive('proxy').and_return(@obj.class)
|
proxy = @obj.should_receive('proxy').and_return(@obj.class)
|
||||||
@obj.valid?.should be_true
|
@obj.valid?.should be_true
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue