[RW] | attribute | |
[RW] | base | |
[RW] | message | |
[RW] | options | |
[RW] | type |
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 21 21: def initialize(base, attribute, type = nil, options = {}) 22: self.base = base 23: self.attribute = attribute 24: self.type = type || :invalid 25: self.options = options 26: self.message = options.delete(:message) || self.type 27: end
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 35 35: def full_message 36: attribute.to_s == 'base' ? message : generate_full_message(default_options) 37: end
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 29 29: def message 30: # When type is a string, it means that we do not have to do a lookup, because 31: # the user already sent the "final" message. 32: type.is_a?(String) ? type : generate_message(default_options) 33: end
Alias for message
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 41 41: def value 42: @base.respond_to?(attribute) ? @base.send(attribute) : nil 43: end
Return user options with default options.
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 121 121: def default_options 122: options.reverse_merge :scope => [:activerecord, :errors], 123: :model => @base.class.human_name, 124: :attribute => @base.class.human_attribute_name(attribute.to_s), 125: :value => value 126: end
Wraps an error message into a full_message format.
The default full_message format for any locale is "{{attribute}} {{message}}". One can specify locale specific default full_message format by storing it as a translation for the key :"activerecord.errors.full_messages.format".
Additionally one can specify a validation specific error message format by storing a translation for :"activerecord.errors.full_messages.[message_key]". E.g. the full_message format for any validation that uses :blank as a message key (such as validates_presence_of) can be stored to :"activerecord.errors.full_messages.blank".
Because the message key used by a validation can be overwritten on the validates_* class macro level one can customize the full_message format for any particular validation:
# app/models/article.rb class Article < ActiveRecord::Base validates_presence_of :title, :message => :"title.blank" end # config/locales/en.yml en: activerecord: errors: full_messages: title: blank: This title is screwed!
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 108 108: def generate_full_message(options = {}) 109: keys = [ 110: "full_messages.#{@message}""full_messages.#{@message}", 111: 'full_messages.format''full_messages.format', 112: '{{attribute}} {{message}}' 113: ] 114: 115: options.merge!(:default => keys, :message => self.message) 116: I18n.translate(keys.shift, options) 117: end
Translates an error message in it‘s default scope (activerecord.errrors.messages). Error messages are first looked up in models.MODEL.attributes.ATTRIBUTE.MESSAGE, if it‘s not there, it‘s looked up in models.MODEL.MESSAGE and if that is not there it returns the translation of the default message (e.g. activerecord.errors.messages.MESSAGE). The translated model name, translated attribute name and the value are available for interpolation.
When using inheritence in your models, it will check all the inherited models too, but only if the model itself hasn‘t been found. Say you have class Admin < User; end and you wanted the translation for the :blank error message for the title attribute, it looks for these translations:
<ol> <li>activerecord.errors.models.admin.attributes.title.blank</li> <li>activerecord.errors.models.admin.blank</li> <li>activerecord.errors.models.user.attributes.title.blank</li> <li>activerecord.errors.models.user.blank</li> <li>activerecord.errors.messages.blank</li> <li>any default you provided through the options hash (in the activerecord.errors scope)</li> </ol>
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 65 65: def generate_message(options = {}) 66: keys = @base.class.self_and_descendants_from_active_record.map do |klass| 67: [ "models.#{klass.name.underscore}.attributes.#{attribute}.#{@message}""models.#{klass.name.underscore}.attributes.#{attribute}.#{@message}", 68: "models.#{klass.name.underscore}.#{@message}""models.#{klass.name.underscore}.#{@message}" ] 69: end.flatten 70: 71: keys << options.delete(:default) 72: keys << "messages.#{@message}""messages.#{@message}" 73: keys << @message if @message.is_a?(String) 74: keys << @type unless @type == @message 75: keys.compact! 76: 77: options.merge!(:default => keys) 78: I18n.translate(keys.shift, options) 79: end