Class: Recliner::Errors
- Defined in:
- lib/recliner/validations.rb
Instance Method Summary collapse
-
#full_messages(options = {}) ⇒ Object
Returns all the full error messages in an array.
-
#generate_message(attribute, message = :invalid, options = {}) ⇒ Object
Translates an error message in it’s default scope (
recliner.errrors.messages
).
Instance Method Details
#full_messages(options = {}) ⇒ Object
Returns all the full error messages in an array.
class Company < Recliner::Document
validates_presence_of :name, :address, :email
validates_length_of :name, :in => 5..30
end
company = Company.create(:address => '123 First St.')
company.errors. # =>
["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/recliner/validations.rb', line 29 def ( = {}) = [] each do |attribute, | = Array.wrap() next if .empty? if attribute == :base .each {|m| << m } else attr_name = @base.class.human_attribute_name(attribute.to_s) prefix = attr_name + I18n.t('recliner.errors.format.separator', :default => ' ') .each do |m| << "#{prefix}#{m}" end end end end |
#generate_message(attribute, message = :invalid, options = {}) ⇒ Object
Translates an error message in it’s default scope (recliner.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. recliner.errors.messages.MESSAGE
). The translated model name, translated attribute name and the value are available for interpolation.
When using inheritance 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>recliner.errors.models.admin.attributes.title.blank
</li> <li>recliner.errors.models.admin.blank
</li> <li>recliner.errors.models.user.attributes.title.blank
</li> <li>recliner.errors.models.user.blank
</li> <li>recliner.errors.messages.blank
</li> <li>any default you provided through the options
hash (in the recliner.errors scope)</li> </ol>
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/recliner/validations.rb', line 68 def (attribute, = :invalid, = {}) , [:default] = [:default], if [:default].is_a?(Symbol) defaults = @base.class.self_and_descendants_from_recliner.map do |klass| [ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{}", :"models.#{klass.name.underscore}.#{}" ] end defaults << .delete(:default) defaults = defaults.compact.flatten << :"messages.#{}" key = defaults.shift value = @base.respond_to?(attribute) ? @base.send(attribute) : nil = { :default => defaults, :model => @base.class.human_name, :attribute => @base.class.human_attribute_name(attribute.to_s), :value => value, :scope => [:recliner, :errors] }.merge() I18n.translate(key, ) end |