Saturday, November 10, 2012

Prototype_legacy_helper for rails > 3.0.9

Prototype_legacy_helper for rails > 3.0.9

Prototype_legacy_helper is available for rails 3 which gives rails 2 legacy support to rails 3 such as form_remote_tag. This plugin is gem for large application which want to migrate to rails 3. But this plugin is using some prototype_helper class functions which is deprecated in rails > 3.0.9. Here I am posting code to make it working for new rails versions in case we want to keep our old form_remote_tag working.

Add following code in vendor/plugins/prototype_legacy_helper/lib/prototype_legacy_helper.rb
1:   CALLBACKS = Set.new([ :create, :uninitialized, :loading, :loaded,  
2:             :interactive, :complete, :failure, :success ] +  
3:             (100..599).to_a)  
4:      
5:   def build_callbacks(options)  
6:    callbacks = {}  
7:    options.each do |callback, code|  
8:     if CALLBACKS.include?(callback)  
9:      name = 'on' + callback.to_s.capitalize  
10:      callbacks[name] = "function(request){#{code}}"  
11:     end  
12:    end  
13:    callbacks  
14:   end  
15:    
16:    
17:   def options_for_ajax(options)  
18:    js_options = build_callbacks(options)  
19:      
20:    js_options['asynchronous'] = options[:type] != :synchronous  
21:    js_options['method']    = method_option_to_s(options[:method]) if options[:method]  
22:    js_options['insertion']  = "'#{options[:position].to_s.downcase}'" if options[:position]  
23:    js_options['evalScripts'] = options[:script].nil? || options[:script]  
24:      
25:    if options[:form]  
26:     js_options['parameters'] = 'Form.serialize(this)'  
27:    elsif options[:submit]  
28:     js_options['parameters'] = "Form.serialize('#{options[:submit]}')"  
29:    elsif options[:with]  
30:     js_options['parameters'] = options[:with]  
31:    end  
32:      
33:    if protect_against_forgery? && !options[:form]  
34:     if js_options['parameters']  
35:      js_options['parameters'] << " + '&"  
36:     else  
37:      js_options['parameters'] = "'"  
38:     end  
39:     js_options['parameters'] << "#{request_forgery_protection_token}=' + encodeURIComponent('#{escape_javascript form_authenticity_token}')"  
40:    end  
41:      
42:    options_for_javascript(js_options)  
43:   end  
44:    
45:   def remote_function(options)  
46:    javascript_options = options_for_ajax(options)  
47:      
48:    update = ''  
49:    if options[:update] && options[:update].is_a?(Hash)  
50:     update = []  
51:     update << "success:'#{options[:update][:success]}'" if options[:update][:success]  
52:     update << "failure:'#{options[:update][:failure]}'" if options[:update][:failure]  
53:     update = '{' + update.join(',') + '}'  
54:    elsif options[:update]  
55:     update << "'#{options[:update]}'"  
56:    end  
57:      
58:    function = update.empty? ?  
59:    "new Ajax.Request(" :  
60:     "new Ajax.Updater(#{update}, "  
61:      
62:    url_options = options[:url]  
63:    function << "'#{html_escape(escape_javascript(url_for(url_options)))}'"  
64:    function << ", #{javascript_options})"  
65:      
66:    function = "#{options[:before]}; #{function}" if options[:before]  
67:    function = "#{function}; #{options[:after]}" if options[:after]  
68:    function = "if (#{options[:condition]}) { #{function}; }" if options[:condition]  
69:    function = "if (confirm('#{escape_javascript(options[:confirm])}')) { #{function}; }" if options[:confirm]  
70:      
71:    return function.html_safe  
72:   end  
73: