Class: FancyErrors

Inherits:
ActiveRecord::Errors
  • Object
show all
Defined in:
lib/fancy_errors.rb

Instance Method Summary collapse

Instance Method Details

#full_messages(options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/fancy_errors.rb', line 2

def full_messages(options = {})
  if options[:order] && order = options[:order].map {|a| a.to_s }
    # add attrs specified in order
    ordered_attrs = order & @errors.keys
    ordered_attrs += @errors.keys - order
  end

  (ordered_attrs||@errors.keys).inject([]) do |full_messages, a|
    full_messages.concat(full_messages_on(a))
  end
end

#full_messages_on(attribute) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fancy_errors.rb', line 14

def full_messages_on(attribute)
  (@errors[attribute.to_s] || []).inject([]) do |messages, msg|
    if attribute.to_s == 'base'
      messages << msg
    elsif msg =~ /^\^/
      messages << msg[1..-1]
    else
      attr_name = @base.class.human_attribute_name(attribute)
      messages << attr_name + I18n.t('activerecord.errors.format.separator', :default => ' ') + msg
    end
  end
end