# ErrorMessagesLocalizedFor module ActionView module Helpers module ActiveRecordHelper def error_messages_localized_for(*params) options = params.last.is_a?(Hash) ? params.pop.symbolize_keys : {} objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact count = objects.inject(0) {|sum, object| sum + object.errors.count } unless count.zero? html = {} [:id, :class].each do |key| if options.include?(key) value = options[key] html[key] = value unless value.blank? else html[key] = 'errorExplanation' end end header_message = pluralize(count,_('error')) + " " + _('prohibitedthis') + " " + _((options[:object_name] || params.first).to_s.gsub('_', ' ')) + " " + _('fromsaving') error_messages = objects.map {|object| object.errors.full_messages_localized.map {|field| content_tag(:li,_(field[0].downcase) + " " + _(field[1]))}} content_tag(:div, content_tag(options[:header_tag] || :h2, header_message) << content_tag(:p, _('fieldproblems')) << content_tag(:ul, error_messages), html ) else '' end end end end end module ActiveRecord class Errors def full_messages_localized full_messages = [] i=0 @errors.each_key do |attr| @errors[attr].each do |msg| i+=1 next if msg.nil? if attr == "base" full_messages << [i,msg] else full_messages << [@base.class.human_attribute_name(attr).to_s,msg] end end end full_messages end end end