Sha256: ff63f52cee930263ed90b7d32bb35b1924b5ff6f4992ee645f238c19f2eecfd6
Contents?: true
Size: 740 Bytes
Versions: 3
Compression:
Stored size: 740 Bytes
Contents
class SumOfValidator < ActiveModel::EachValidator def check_validity! unless options[:attr_names] raise ArgumentError, "must supply :attr_names option" end end def validate_each(record, attribute, value) addends = options[:attr_names].map {|_| record[_] } return if value.blank? || addends.any?(&:blank?) sum = addends.sum unless value == sum record.errors.add(attribute, options[:message] || :sum_of, options.merge( attr_names: options[:attr_names].to_sentence, addends: addends.join(' + '), sum: sum, value: value, ) ) end end end
Version data entries
3 entries across 3 versions & 1 rubygems