Permalink
Browse files

cleanup and fix scope handling in uniqueness validator

  • Loading branch information...
1 parent e4cd969 commit 93a248c8e289736c9d78e5b052b795fe7bb0db3b @netskin-ci netskin-ci committed Nov 13, 2010
Showing with 16 additions and 12 deletions.
  1. +16 −12 lib/sequel_sexy_validations/validators/uniqueness.rb
@@ -4,6 +4,21 @@ module Plugins
module SexyValidations
module Validators
class Uniqueness
+ def self.apply_scope_filter!(model, dataset, filter)
+ case filter
+ when Array
+ filter.each do |filter1|
+ apply_scope_filter!(model, dataset, filter1)
+ end
+ when Symbol
+ dataset.filter!(filter => model.send(filter))
+ when Proc
+ dataset.filter!(filter.call(model))
+ else
+ dataset.filter!(filter)
+ end
+ end
+
def self.validate(model, attribute, value, options)
return unless value
@@ -16,18 +31,7 @@ def self.validate(model, attribute, value, options)
options[:message] ||= "bereits vergeben"
dataset = model.class.filter(~{:id => model.id}, {attribute => value})
- if options[:scope].is_a?(Array)
- options[:scope].each do |v|
- case v
- when Symbol
- dataset = dataset.filter(v => model.send(v))
- when Proc
- dataset = dataset.filter(v.call(model))
- else
- dataset = dataset.filter(v)
- end
- end
- end
+ apply_scope_filter!(model, dataset, options[:scope])
unless dataset.empty?
model.errors.add(attribute, options[:message])

0 comments on commit 93a248c

Please sign in to comment.