Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

cleanup and fix scope handling in uniqueness validator

  • Loading branch information...
commit 93a248c8e289736c9d78e5b052b795fe7bb0db3b 1 parent e4cd969
netskin-ci authored November 13, 2010
28  lib/sequel_sexy_validations/validators/uniqueness.rb
@@ -4,6 +4,21 @@ module Plugins
4 4
     module SexyValidations
5 5
       module Validators
6 6
         class Uniqueness
  7
+          def self.apply_scope_filter!(model, dataset, filter)
  8
+            case filter
  9
+              when Array
  10
+                filter.each do |filter1|
  11
+                  apply_scope_filter!(model, dataset, filter1)
  12
+                end
  13
+              when Symbol
  14
+                dataset.filter!(filter => model.send(filter))
  15
+              when Proc
  16
+                dataset.filter!(filter.call(model))
  17
+              else
  18
+                dataset.filter!(filter)
  19
+            end
  20
+          end
  21
+
7 22
           def self.validate(model, attribute, value, options)
8 23
             return unless value
9 24
 
@@ -16,18 +31,7 @@ def self.validate(model, attribute, value, options)
16 31
             options[:message] ||= "bereits vergeben"
17 32
             
18 33
             dataset = model.class.filter(~{:id => model.id}, {attribute => value})
19  
-            if options[:scope].is_a?(Array)
20  
-              options[:scope].each do |v|
21  
-                case v
22  
-                  when Symbol
23  
-                    dataset = dataset.filter(v => model.send(v))
24  
-                  when Proc
25  
-                    dataset = dataset.filter(v.call(model))
26  
-                  else
27  
-                    dataset = dataset.filter(v)
28  
-                end
29  
-              end
30  
-            end
  34
+            apply_scope_filter!(model, dataset, options[:scope])
31 35
 
32 36
             unless dataset.empty?
33 37
               model.errors.add(attribute, options[:message])

0 notes on commit 93a248c

Please sign in to comment.
Something went wrong with that request. Please try again.