Class: Recliner::Errors

Inherits:
ActiveModel::Errors
  • Object
show all
Defined in:
lib/recliner/validations.rb

Instance Method Summary collapse

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.full_messages # =>
  ["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 full_messages(options = {})
  full_messages = []
  
  each do |attribute, messages|
    messages = Array.wrap(messages)
    next if messages.empty?
  
    if attribute == :base
      messages.each {|m| full_messages << m }
    else
      attr_name = @base.class.human_attribute_name(attribute.to_s)
      prefix = attr_name + I18n.t('recliner.errors.format.separator', :default => ' ')
      messages.each do |m|
        full_messages <<  "#{prefix}#{m}"
      end
    end
  end
  
  full_messages
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 generate_message(attribute, message = :invalid, options = {})
  message, options[:default] = options[:default], message if options[:default].is_a?(Symbol)
  
  defaults = @base.class.self_and_descendants_from_recliner.map do |klass|
    [ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{message}", 
      :"models.#{klass.name.underscore}.#{message}" ]
  end
  
  defaults << options.delete(:default)
  defaults = defaults.compact.flatten << :"messages.#{message}"
  
  key = defaults.shift
  value = @base.respond_to?(attribute) ? @base.send(attribute) : nil
  
  options = { :default => defaults,
    :model => @base.class.human_name,
    :attribute => @base.class.human_attribute_name(attribute.to_s),
    :value => value,
    :scope => [:recliner, :errors]
  }.merge(options)
  
  I18n.translate(key, options)
end