27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/preciousss/controllers/helpers.rb', line 27
def errors_for(*params)
options = params..symbolize_keys
options[:on] = [*options[:on]].compact
options[:except_on] = [*options[:except_on]].compact
objects = [*params].flatten
object_errors = []
objects.each do |object|
errors = nil
object = instance_variable_get("@#{object}") if object.is_a?(Symbol)
object.errors.each do |attr, msg|
(errors ||= ActiveModel::Errors.new(object)).add(attr, msg) if (options[:on].blank? || options[:on].include?(attr.to_sym)) && (options[:except_on].blank? || !options[:except_on].include?(attr.to_sym))
end
object_errors << errors if errors
end
unless object_errors.empty?
options[:class] ||= 'errors'
options[:id] ||= objects.map{|object| object.class.name.underscore}.join('_') + '_errors'
options[:title] = I18n.t('activerecord.errors.template.header', :model => objects.map{|object| object.class.human}.to_sentence, :count => object_errors.size) if options[:title] === true
I18n.with_options :locale => options[:locale], :scope => [:activerecord, :errors, :template] do |locale|
messages = object_errors.sum{|errors| errors.full_messages.map{|msg| '<li>' + ERB::Util.html_escape(msg) + '</li>'}}.join.html_safe
contents = ''
contents << '<p class="title">' + options[:title] + '</p>' unless options[:title].blank?
contents << '<ul class="messages">' + messages + '</ul>'
"<div id=\"#{options[:id]}\" class=\"#{options[:class]}\">#{contents}</div>".html_safe
end
else
''
end
end
|