Sha256: 371a4494de3470f1d7020add5ecf1b09ef99e8c8ce3058649bafa709ec4735ae
Contents?: true
Size: 1.02 KB
Versions: 33
Compression:
Stored size: 1.02 KB
Contents
module ActiveModel class ErrorMessage < String attr_accessor :error_code, :data def initialize(message, error_code = nil, data = nil) super message @error_code = error_code @data = data end end class Errors def add(attribute, message = nil, options = {}) # build error code from message symbol error_code = normalize_error_code(attribute, message, options) message = normalize_message(attribute, message, options) if exception = options[:strict] exception = ActiveModel::StrictValidationFailed if exception == true raise exception, full_message(attribute, message) end # use customized String subclass with "error_code" attribute self[attribute] << ErrorMessage.new(message, error_code, options[:data]) end def normalize_error_code(_attribute, message, options) if !options[:error_code].blank? options[:error_code] elsif message.class == Symbol message else :invalid end end end end
Version data entries
33 entries across 33 versions & 1 rubygems