Fixing uniqueness proxy error + more in README

This commit is contained in:
Sam Lown 2010-06-21 22:37:13 +02:00
parent 224c96e68a
commit 8337bab714
4 changed files with 54 additions and 12 deletions

View file

@ -196,6 +196,47 @@ anonymous classes:
Using this method of anonymous classes will *only* create arrays of objects.
## Assocations
Two types at the moment:
belongs_to :person
collection_of :tags
TODO: Document properly!
## Validations
CouchRest Model automatically includes the new ActiveModel validations, so they should work just as the traditional Rails
validations. For more details, please see the ActiveModel::Validations documentation.
CouchRest Model adds the possibility to check the uniqueness of attributes using the @validates_uniqueness_of@ class method, for example:
class Person < CouchRest::Model::Base
property :title, String
validates_uniqueness_of :title
end
The uniqueness validation creates a new view for the attribute or uses one that already exists.
Given that the uniqueness check performs a request to the database, it is also possible
to include a +:proxy+ parameter. This allows you to
call a method on the document and provide an alternate proxy object.
Examples:
# Same as not including proxy:
validates_uniqueness_of :title, :proxy => 'class'
# Person#company.people provides a proxy object for people
validates_uniqueness_of :title, :proxy => 'company.people'
## Notable Issues
CouchRest Model uses active_support for some of its internals. Ensure you have a stable active support gem installed

View file

@ -20,7 +20,7 @@ module CouchRest
end
# Determine the base of the search
base = options[:proxy].nil? ? @klass : document.send(options[:proxy])
base = options[:proxy].nil? ? @klass : document.instance_eval(options[:proxy])
docs = base.view("by_#{attribute}", :key => value, :limit => 2, :include_docs => false)['rows']
return if docs.empty?

View file

@ -429,7 +429,7 @@ describe "Model Base" do
end
end
describe "searching the contents of an extended document" do
describe "searching the contents of a model" do
before :each do
@db = reset_test_db!

View file

@ -34,6 +34,16 @@ describe "Validations" do
@obj.should be_valid
end
context "with a pre-defined view" do
it "should no try to create new view" do
@obj = @objs[1]
@obj.class.should_not_receive('view_by')
@obj.class.should_receive('has_view?').and_return(true)
@obj.class.should_receive('view').and_return({'rows' => [ ]})
@obj.valid?
end
end
context "with a proxy parameter" do
it "should be used" do
@obj = @objs.first
@ -43,15 +53,6 @@ describe "Validations" do
end
end
context "with a pre-defined view" do
it "should no try to create new view" do
@obj = @objs.first
@obj.class.should_not_receive('view_by')
@obj.class.should_receive('has_view?').and_return(true)
@obj.class.should_receive('view').and_return({'rows' => [ ]})
@obj.valid?
end
end
end