2010-06-23 11:58:35 +02:00
|
|
|
# encoding: utf-8
|
2010-06-21 21:33:46 +02:00
|
|
|
|
|
|
|
module CouchRest
|
|
|
|
module Model
|
|
|
|
module Validations
|
2011-05-20 12:21:42 +02:00
|
|
|
|
2010-06-21 21:33:46 +02:00
|
|
|
# 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.
|
2011-02-09 21:21:03 +01:00
|
|
|
def setup(model)
|
|
|
|
@model = model
|
2011-05-20 12:21:42 +02:00
|
|
|
if options[:view].blank?
|
|
|
|
attributes.each do |attribute|
|
|
|
|
opts = merge_view_options(attribute)
|
|
|
|
|
|
|
|
if model.respond_to?(:has_view?) && !model.has_view?(opts[:view_name])
|
|
|
|
opts[:keys] << {:allow_nil => true}
|
|
|
|
model.view_by(*opts[:keys])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-06-21 21:33:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_each(document, attribute, value)
|
2011-05-20 12:21:42 +02:00
|
|
|
opts = merge_view_options(attribute)
|
2011-04-08 14:36:29 +02:00
|
|
|
|
2011-05-20 12:21:42 +02:00
|
|
|
values = opts[:keys].map{|k| document.send(k)}
|
|
|
|
values = values.first if values.length == 1
|
2011-04-04 01:10:31 +02:00
|
|
|
|
2011-04-05 20:41:24 +02:00
|
|
|
model = (document.respond_to?(:model_proxy) && document.model_proxy ? document.model_proxy : @model)
|
2010-06-22 14:15:30 +02:00
|
|
|
# Determine the base of the search
|
2011-05-20 12:21:42 +02:00
|
|
|
base = opts[:proxy].nil? ? model : document.instance_eval(opts[:proxy])
|
2010-06-21 23:12:15 +02:00
|
|
|
|
2011-05-20 12:21:42 +02:00
|
|
|
if base.respond_to?(:has_view?) && !base.has_view?(opts[:view_name])
|
|
|
|
raise "View #{document.class.name}.#{opts[:view_name]} does not exist for validation!"
|
2010-06-21 21:33:46 +02:00
|
|
|
end
|
|
|
|
|
2011-05-20 12:21:42 +02:00
|
|
|
rows = base.view(opts[:view_name], :key => values, :limit => 2, :include_docs => false)['rows']
|
2011-04-08 14:36:29 +02:00
|
|
|
return if rows.empty?
|
2010-06-21 21:33:46 +02:00
|
|
|
|
|
|
|
unless document.new?
|
2011-04-08 14:36:29 +02:00
|
|
|
return if rows.find{|row| row['id'] == document.id}
|
2010-06-21 21:33:46 +02:00
|
|
|
end
|
2011-04-04 01:10:31 +02:00
|
|
|
|
2011-04-08 14:36:29 +02:00
|
|
|
if rows.length > 0
|
|
|
|
opts = options.merge(:value => value)
|
|
|
|
opts.delete(:scope) # Has meaning with I18n!
|
|
|
|
document.errors.add(attribute, :taken, opts)
|
2010-06-21 21:33:46 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-20 12:21:42 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def merge_view_options(attr)
|
|
|
|
keys = [attr]
|
|
|
|
keys.unshift(*options[:scope]) unless options[:scope].nil?
|
|
|
|
|
|
|
|
view_name = options[:view].nil? ? "by_#{keys.join('_and_')}" : options[:view]
|
|
|
|
|
|
|
|
options.merge({:keys => keys, :view_name => view_name})
|
|
|
|
end
|
|
|
|
|
2010-06-21 21:33:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|