Sha256: 34086ca9fb13a07324490b96ffe53eb4afb99953ebb41f9d78e7b940d4b2ba2b
Contents?: true
Size: 1.09 KB
Versions: 9
Compression:
Stored size: 1.09 KB
Contents
module Tuning module Validations extend ActiveSupport::Concern class CountValidator < ActiveModel::EachValidator MESSAGES = { is: :wrong_count, minimum: :too_few, maximum: :too_many } CHECKS = { is: :==, minimum: :>=, maximum: :<= } def initialize(options) if range = (options[:in] || options[:within]) options[:minimum] = range.min options[:maximum] = range.max end super end def validate_each(record, attribute, value) if value.respond_to?(:size) count = value.size CHECKS.each do |name, operator| if options.has_key?(name) other = options[name] unless count.send(operator, other) record.errors.add attribute, MESSAGES[name], count: other end end end else record.errors.add attribute, :uncountable end end end module ClassMethods def validates_count_of(*attr_names) validates_with CountValidator, _merge_attributes(attr_names) end end end end
Version data entries
9 entries across 9 versions & 1 rubygems