2010-07-06 16:29:26 -04:00
|
|
|
module SimpleForm
|
|
|
|
class ErrorNotification
|
|
|
|
delegate :object, :object_name, :template, :to => :@builder
|
|
|
|
|
|
|
|
def initialize(builder, options)
|
|
|
|
@builder = builder
|
2010-07-06 16:49:16 -04:00
|
|
|
@message = options.delete(:message)
|
2010-07-06 16:29:26 -04:00
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
|
|
|
def render
|
|
|
|
if has_errors?
|
2010-07-06 16:49:16 -04:00
|
|
|
template.content_tag(error_notification_tag, error_message, html_options)
|
2010-07-06 16:29:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2010-07-06 16:49:16 -04:00
|
|
|
def error_message
|
|
|
|
@message || translate_error_notification
|
|
|
|
end
|
|
|
|
|
2010-07-06 16:29:26 -04:00
|
|
|
def error_notification_tag
|
|
|
|
SimpleForm.error_notification_tag
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_errors?
|
|
|
|
object && object.respond_to?(:errors) && object.errors.present?
|
|
|
|
end
|
|
|
|
|
2010-07-06 16:49:16 -04:00
|
|
|
def html_options
|
|
|
|
@options[:class] = "error_notification #{@options[:class]}".strip
|
|
|
|
@options
|
|
|
|
end
|
|
|
|
|
2010-07-06 16:29:26 -04:00
|
|
|
def translate_error_notification
|
|
|
|
lookups = []
|
|
|
|
lookups << :"#{object_name}"
|
|
|
|
lookups << :default_message
|
|
|
|
lookups << "Some errors were found, please take a look:"
|
|
|
|
I18n.t(lookups.shift, :scope => :"simple_form.error_notification", :default => lookups)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|