module UnobtrusiveJavascript::EnhancedAjaxHelpers def form_remote_tag(options = {}) options[:form] = true options[:html] ||= {} options[:unobtrusive] = options[:unobtrusive] || false options[:html][:action] = options[:html][:action] || url_for(options[:url]) options[:html][:id] = options[:html][:id] || "form#{tokenised_id(options[:html][:action])}" options[:html][:method] = options[:html][:method] || "post" if options[:unobtrusive] @controller.register_js_behaviour("##{options[:html][:id]}:submit", unobtrusive_remote_function(options)) else options[:html][:onsubmit] = "#{remote_function(options)}; return false;" end tag("form", options[:html], true) end def link_to_remote(name, options = {}, html_options = {}) html_options[:id] = html_options[:id] || "link_#{tokenised_id(name)}" unobtrusive = options.delete(:unobtrusive) { |e| false } if unobtrusive remote_function = unobtrusive_remote_function(options) remote_function << safari_bug_fix @controller.register_js_behaviour("##{html_options[:id]}:click", remote_function) link_to name, options[:url], html_options else link_to_function(name, remote_function(options), html_options) end end protected def unobtrusive_remote_function(options) remote_function(options).gsub("serialize(this)", "serialize(element)").gsub("return false;", "") << "; Event.stop(event);" end def tokenised_id(arg) arg.gsub("/", "_").gsub("?", "_").gsub("=", "_").gsub(" ", "_").downcase end def safari_bug_fix " element.onclick = function() { return false; }" end end