lib/couchrest-uniqueness-validation.rb in couchrest-uniqueness-validation-0.0.1 vs lib/couchrest-uniqueness-validation.rb in couchrest-uniqueness-validation-0.1.0
- old
+ new
@@ -4,11 +4,11 @@
class UniquenessValidator < GenericValidator
def initialize(field_name, options = {})
super
@field_name, @options = field_name, options
- @options[:view] ||= "by_#{@field_name}".to_sym
+ @options[:view] ||= "by_#{@field_name}"
end
def call(target)
return true if unique?(target)
@@ -19,11 +19,11 @@
protected
def unique?(target)
value = target.validation_property_value(@field_name)
- existing_docs = target.class.view(@options[:view], :key => value, :limit => 1, :include_docs => false)
+ existing_docs = target.class.view(@options[:view].to_sym, :key => value, :limit => 1, :include_docs => false)['rows']
# normal case when target.new_document? == true and
# no other document exists
return true if existing_docs.empty?
@@ -38,17 +38,17 @@
##
# Validates that the specified attribute is unique across documents with the same
# couchrest-type using a view.
#
- # @note
- # You have to define a design doc view.
- # @see http://rdoc.info/rdoc/couchrest/couchrest/blob/1b34fe4b60694683e98866a51c2109c1885f7e42/CouchRest/Mixins/Views/ClassMethods.html#view_by-instance_method for more details about views.
+ # == Example
#
- # @example [Usage]
- #
- # class User
+ # require 'rubygems'
+ # require 'couchrest'
+ # require 'couchrest-uniqueness-validation'
+ #
+ # class User < CouchRest::ExtendedDocument
#
# property :nickname
# property :login
#
# view_by :nickname
@@ -60,9 +60,15 @@
# validates_uniqueness_of :login, :view => 'my_custom_view'
#
# # a call to valid? will return false unless no other document exists with
# # the same couchrest-type, nickname and login
# end
+ #
+ # Note: at least two views should exist in the User design doc for this example to work -
+ # :by_nickname and :my_custom_view.
+ #
+ # See {CouchRest Views docs}[http://rdoc.info/rdoc/couchrest/couchrest/blob/1b34fe4b60694683e98866a51c2109c1885f7e42/CouchRest/Mixins/Views/ClassMethods.html#view_by-instance_method]
+ # for more info on views
def validates_uniqueness_of(*fields)
opts = opts_from_validator_args(fields)
add_validator_to_context(opts, fields, CouchRest::Validation::UniquenessValidator)
end