Skip to content

Commit

Permalink
cleanup and fix scope handling in uniqueness validator
Browse files Browse the repository at this point in the history
  • Loading branch information
netskin-ci committed Nov 13, 2010
1 parent e4cd969 commit 93a248c
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/sequel_sexy_validations/validators/uniqueness.rb
Expand Up @@ -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

Expand All @@ -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])
Expand Down

0 comments on commit 93a248c

Please sign in to comment.