Adding uniqueness validation support out of the box
This commit is contained in:
parent
b81d37fc02
commit
08390e6709
5 changed files with 144 additions and 2 deletions
|
@ -1,6 +1,11 @@
|
|||
# encoding: utf-8
|
||||
|
||||
require "couchrest/model/validations/casted_model"
|
||||
require "couchrest/model/validations/uniqueness"
|
||||
|
||||
I18n.load_path << File.join(
|
||||
File.dirname(__FILE__), "validations", "locale", "en.yml"
|
||||
)
|
||||
|
||||
module CouchRest
|
||||
module Model
|
||||
|
@ -23,8 +28,32 @@ module CouchRest
|
|||
validates_with(CastedModelValidator, _merge_attributes(args))
|
||||
end
|
||||
|
||||
# TODO: Here will lie validates_uniqueness_of
|
||||
|
||||
# Validates if the field is unique for this type of document. Automatically creates
|
||||
# a view if one does not already exist and performs a search for all matching
|
||||
# documents.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# class Person < CouchRest::Model::Base
|
||||
# property :title, String
|
||||
#
|
||||
# validates_uniqueness_of :title
|
||||
# end
|
||||
#
|
||||
# Asside from the standard options, 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:
|
||||
#
|
||||
# # 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'
|
||||
#
|
||||
def validates_uniqueness_of(*args)
|
||||
validates_with(UniquenessValidator, _merge_attributes(args))
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
5
lib/couchrest/model/validations/locale/en.yml
Normal file
5
lib/couchrest/model/validations/locale/en.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
en:
|
||||
errors:
|
||||
messages:
|
||||
taken: "is already taken"
|
||||
|
42
lib/couchrest/model/validations/uniqueness.rb
Normal file
42
lib/couchrest/model/validations/uniqueness.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
# encoding: urf-8
|
||||
|
||||
module CouchRest
|
||||
module Model
|
||||
module Validations
|
||||
|
||||
# Validates if a field is unique
|
||||
class UniquenessValidator < ActiveModel::EachValidator
|
||||
|
||||
# Ensure we have a class available so we can check for a usable view
|
||||
# or add one if necessary.
|
||||
def setup(klass)
|
||||
@klass = klass
|
||||
end
|
||||
|
||||
|
||||
def validate_each(document, attribute, value)
|
||||
unless @klass.has_view?("by_#{attribute}")
|
||||
@klass.view_by attribute
|
||||
end
|
||||
|
||||
# Determine the base of the search
|
||||
base = options[:proxy].nil? ? @klass : document.send(options[:proxy])
|
||||
|
||||
docs = base.view("by_#{attribute}", :key => value, :limit => 2, :include_docs => false)['rows']
|
||||
return if docs.empty?
|
||||
|
||||
unless document.new?
|
||||
return if docs.find{|doc| doc['id'] == document.id}
|
||||
end
|
||||
|
||||
if docs.length > 0
|
||||
document.errors.add(attribute, :taken, :default => options[:message], :value => value)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue