Guest User

Untitled

a guest
Mar 13th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. def error_messages_for(*params)
  2. options = params.extract_options!.symbolize_keys
  3. if object = options.delete(:object)
  4. objects = [object].flatten
  5. else
  6. objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact
  7. end
  8. count = objects.inject(0) {|sum, object| sum + object.errors.count }
  9. unless count.zero?
  10. html = {}
  11. [:id, :class].each do |key|
  12. if options.include?(key)
  13. value = options[key]
  14. html[key] = value unless value.blank?
  15. else
  16. html[key] = 'errorExplanation'
  17. end
  18. end
  19. options[:object_name] ||= params.first
  20. options[:header_message] = "#{pluralize(count, 'error')} prohibited this #{options[:object_name].to_s.gsub('_', ' ')} from being saved" unless options.include?(:header_message)
  21. options[:message] ||= 'There were problems with the following fields:' unless options.include?(:message)
  22. error_messages = objects.map {|object| object.errors.full_messages.map {|msg| content_tag(:li, msg) } }
  23.  
  24. contents = ''
  25. contents << content_tag(options[:header_tag] || :h2, options[:header_message]) unless options[:header_message].blank?
  26. contents << content_tag(:p, options[:message]) unless options[:message].blank?
  27. contents << content_tag(:ul, error_messages)
  28.  
  29. content_tag(:div, contents, html)
  30. else
  31. ''
  32. end
  33. end
Add Comment
Please, Sign In to add comment