Sha256: 1a1cfe49e92671a39c497373f27bc5528932d373d57bf679476d28fa63ef5e9a

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 KB

Contents

# encoding: utf-8
module SexyValidations
  module Validators
    class Count
      def self.validate(record, attribute, value, options)
        return unless value

        unless options.is_a?(Hash)
          options = {
            :within => options,
          }
        end
        
        case options[:within]
          when Fixnum, Integer, Bignum
            unless value.count == options[:within]
              record.errors.add(attribute, "ungültige Anzahl Selektionen")
            end

          when Array
            unless options[:within].include?(value.count)
              record.errors.add(attribute, "ungültige Anzahl Selektionen")
            end
          
          when Range
            min = options[:within].min
            if value.count < min
              record.errors.add(attribute, "zu wenig Selektionen (mindestes #{min})")
            end

            max = options[:within].max
            if value.count > max
              record.errors.add(attribute, "zu viele Selektionen (maximal #{max})")
            end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sexy_validations-0.5.4 lib/sexy_validations/validators/count.rb
sexy_validations-0.5.3 lib/sexy_validations/validators/count.rb
sexy_validations-0.5.2 lib/sexy_validations/validators/count.rb
sexy_validations-0.5.1 lib/sexy_validations/validators/count.rb
sexy_validations-0.5.0 lib/sexy_validations/validators/count.rb
sexy_validations-0.4.3 lib/sexy_validations/validators/count.rb