Using rails' escape_javascript outside of views

Rails provides a handy function to escape string to be used in your javascript code. Unfortunately, it is only available in your views. It is very easy though to make it available outside of views, eg in your helpers, as it suffices to require and include one module:

require 'action_view/helpers/javascript_helper'
include ActionView::Helpers::JavaScriptHelper

# escape_javascript is available here.

This is used in the MyOwnDB code in the yui entities table helper to translate error messages, which eg in french can contain quotes which need to be escaped.