Sha256: 5f2b1e909cc30dbf2a74fbee61f2ec45fc9f9f89e57572b5c24257c7b9322bcf

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

# encoding: utf-8
module Sequel
  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

            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
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sequel_sexy_validations-0.2.7 lib/sequel_sexy_validations/validators/uniqueness.rb