diff --git a/README.md b/README.md index cc98d59..9995813 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/couchrest/model/validations/uniqueness.rb b/lib/couchrest/model/validations/uniqueness.rb index 81d9392..30cdaa1 100644 --- a/lib/couchrest/model/validations/uniqueness.rb +++ b/lib/couchrest/model/validations/uniqueness.rb @@ -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? diff --git a/spec/couchrest/base_spec.rb b/spec/couchrest/base_spec.rb index 70057a4..1cfcd80 100644 --- a/spec/couchrest/base_spec.rb +++ b/spec/couchrest/base_spec.rb @@ -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! diff --git a/spec/couchrest/validations.rb b/spec/couchrest/validations.rb index 3e3dd83..426c40e 100644 --- a/spec/couchrest/validations.rb +++ b/spec/couchrest/validations.rb @@ -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,16 +53,7 @@ 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 end