Adding manual view support to uniqueness validation
This commit is contained in:
parent
8337bab714
commit
fcbc0b08e5
5 changed files with 55 additions and 11 deletions
|
@ -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?
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue