Class: Sequel::Plugins::SexyValidations::Validators::Uniqueness

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel_sexy_validations/validators/uniqueness.rb

Class Method Summary collapse

Class Method Details

.apply_scope_filter!(model, dataset, filter) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sequel_sexy_validations/validators/uniqueness.rb', line 7

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

.validate(model, attribute, value, options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sequel_sexy_validations/validators/uniqueness.rb', line 22

def self.validate(model, attribute, value, options)
  return unless value

  unless options.is_a?(Hash)
    options = {
      :scope => options,
    }
  end
  
  options[:message] ||= "bereits vergeben"
  
  dataset = model.class.filter(~{:id => model.id}, {attribute => value})
  if options[:scope]
    apply_scope_filter!(model, dataset, options[:scope])
  end

  unless dataset.empty?
    model.errors.add(attribute, options[:message])
  end
end