Skip to content

Commit

Permalink
Fixing proxy and view bug
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Jun 22, 2010
1 parent 42ca7bc commit 3d46db1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/couchrest/model/validations/uniqueness.rb
Expand Up @@ -16,15 +16,14 @@ def setup(klass)

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?

Expand Down
12 changes: 12 additions & 0 deletions spec/couchrest/validations.rb
Expand Up @@ -57,6 +57,7 @@
@obj.class.should_receive('view').and_return({'rows' => [ ]})
@obj.valid?
end

end

context "with a proxy parameter" do
Expand All @@ -65,6 +66,17 @@
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


Expand Down

0 comments on commit 3d46db1

Please sign in to comment.