Adding manual view support to uniqueness validation
This commit is contained in:
parent
8337bab714
commit
fcbc0b08e5
10
README.md
10
README.md
|
@ -222,9 +222,15 @@ CouchRest Model adds the possibility to check the uniqueness of attributes using
|
|||
validates_uniqueness_of :title
|
||||
end
|
||||
|
||||
The uniqueness validation creates a new view for the attribute or uses one that already exists.
|
||||
The uniqueness validation creates a new view for the attribute or uses one that already exists. You can
|
||||
specify a different view using the +:view+ option, useful for when the @unique_id@ is specified and
|
||||
you'd like to avoid the typical RestClient Conflict error:
|
||||
|
||||
unique_id :code
|
||||
validates_uniqueness_of :code, :view => 'all'
|
||||
|
||||
Given that the uniqueness check performs a request to the database, it is also possible
|
||||
to include a +:proxy+ parameter. This allows you to
|
||||
to include a @:proxy@ parameter. This allows you to
|
||||
call a method on the document and provide an alternate proxy object.
|
||||
|
||||
Examples:
|
||||
|
|
|
@ -40,10 +40,17 @@ module CouchRest
|
|||
# validates_uniqueness_of :title
|
||||
# end
|
||||
#
|
||||
# Asside from the standard options, a +:proxy+ parameter is also accepted if you would
|
||||
# Asside from the standard options, you can specify the name of the view you'd like
|
||||
# to use for the search inside the +:view+ option. The following example would search
|
||||
# for the code in side the +all+ view, useful for when +unique_id+ is used and you'd
|
||||
# like to check before receiving a RestClient Conflict error:
|
||||
#
|
||||
# validates_uniqueness_of :code, :view => 'all'
|
||||
#
|
||||
# A +:proxy+ parameter is also accepted if you would
|
||||
# like to call a method on the document on which the view should be performed.
|
||||
#
|
||||
# Examples:
|
||||
# For Example:
|
||||
#
|
||||
# # Same as not including proxy:
|
||||
# validates_uniqueness_of :title, :proxy => 'class'
|
||||
|
|
|
@ -15,14 +15,17 @@ module CouchRest
|
|||
|
||||
|
||||
def validate_each(document, attribute, value)
|
||||
unless @klass.has_view?("by_#{attribute}")
|
||||
view_name = options[:view].nil? ? "by_#{attribute}" : options[:view]
|
||||
|
||||
unless @klass.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("by_#{attribute}", :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?
|
||||
|
||||
unless document.new?
|
||||
|
|
|
@ -34,8 +34,23 @@ describe "Validations" do
|
|||
@obj.should be_valid
|
||||
end
|
||||
|
||||
it "should allow own view to be specified" do
|
||||
# validates_uniqueness_of :code, :view => 'all'
|
||||
WithUniqueValidationView.create(:title => 'title 1', :code => '1234')
|
||||
@obj = WithUniqueValidationView.new(:title => 'title 5', :code => '1234')
|
||||
@obj.should_not be_valid
|
||||
end
|
||||
|
||||
it "should raise an error if specified view does not exist" do
|
||||
WithUniqueValidationView.validates_uniqueness_of :title, :view => 'fooobar'
|
||||
@obj = WithUniqueValidationView.new(:title => 'title 2', :code => '12345')
|
||||
lambda {
|
||||
@obj.valid?
|
||||
}.should raise_error
|
||||
end
|
||||
|
||||
context "with a pre-defined view" do
|
||||
it "should no try to create new view" do
|
||||
it "should not 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)
|
||||
|
@ -46,9 +61,8 @@ describe "Validations" do
|
|||
|
||||
context "with a proxy parameter" do
|
||||
it "should be used" do
|
||||
@obj = @objs.first
|
||||
@obj = WithUniqueValidationProxy.new(:title => 'test 6')
|
||||
proxy = @obj.should_receive('proxy').and_return(@obj.class)
|
||||
@obj.class.validates_uniqueness_of :title, :proxy => 'proxy'
|
||||
@obj.valid?.should be_true
|
||||
end
|
||||
end
|
||||
|
|
18
spec/fixtures/base.rb
vendored
18
spec/fixtures/base.rb
vendored
|
@ -116,10 +116,24 @@ end
|
|||
|
||||
class WithUniqueValidation < CouchRest::Model::Base
|
||||
use_database DB
|
||||
|
||||
property :title
|
||||
validates_uniqueness_of :title
|
||||
end
|
||||
class WithUniqueValidationProxy < CouchRest::Model::Base
|
||||
use_database DB
|
||||
property :title
|
||||
validates_uniqueness_of :title, :proxy => 'proxy'
|
||||
end
|
||||
class WithUniqueValidationView < CouchRest::Model::Base
|
||||
use_database DB
|
||||
attr_accessor :code
|
||||
unique_id :code
|
||||
def code
|
||||
self["_id"] ||= @code
|
||||
end
|
||||
property :title
|
||||
|
||||
validates_uniqueness_of :title
|
||||
validates_uniqueness_of :code, :view => 'all'
|
||||
end
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue