module SimpleModel module ErrorHelpers attr_accessor :errors_count def errors? !self.errors.blank? end def errors_for_flash(options={}) #set defaults and overwrite options = { :failed_action => "saving", :id => 'errorExplanation', :classes => ''}.merge!(options) error_list = "" # The active_model errors object is not a normal hash and the each method # for the active_mode errors object does not perform as expected # so make it a plain hash {}.merge!(self.errors).each do |error,message| error_list << create_error_list(error,message) end error_string = "

#{self.errors_count}" error_string << " #{puralize_errors_string(self.errors)}" error_string << " prevented #{options[:failed_action]}.

" error_string end # Allow for nested errors like one might see in nested assets when using # accepts_nested_attributes and nested forms def create_error_list(key,value) error_items = "" if value.is_a?(Array) value.uniq! if value.length == 1 self.errors_count = (self.errors_count.to_i + 1) error_items << "
  • #{key.to_s.titleize} #{value[0]}
  • " else error_items << "
  • " end elsif value.is_a?(Hash) error_items << "
  • " else self.errors_count = (self.errors_count.to_i + 1) error_items << "
  • #{key.to_s.titleize} #{value}
  • " end error_items end def puralize_errors_string(array) array = array.to_a if array.is_a?(ActiveModel::Errors) s = "error" s << "s" if array.length > 1 s end def errors_to_s error_string = "" self.errors.full_messages.each do |m| error_string << "#{m} " end error_string end end end