diff options
author | Bob Remeika <bob.remeika@gmail.com> | 2009-11-03 23:18:25 -0800 |
---|---|---|
committer | Stefan Penner <stefan.penner@gmail.com> | 2010-01-27 12:44:30 -0600 |
commit | dad3d09ce83ce54e5aefd7b9f27efb2ddf8b7d39 (patch) | |
tree | bc2f15094902417fb1013ee8c5bc0f190f662c50 /actionpack/lib/action_view/helpers/ajax_helper.rb | |
parent | 5491a663da4bc8a26eb40fb59491199ce253d140 (diff) | |
download | rails-dad3d09ce83ce54e5aefd7b9f27efb2ddf8b7d39.tar.gz rails-dad3d09ce83ce54e5aefd7b9f27efb2ddf8b7d39.tar.bz2 rails-dad3d09ce83ce54e5aefd7b9f27efb2ddf8b7d39.zip |
Applied Yehuda's patch; Sharing extract_object_name_for_form! between form_helper and ajax_helper; Added script_decorator helper
Diffstat (limited to 'actionpack/lib/action_view/helpers/ajax_helper.rb')
-rw-r--r-- | actionpack/lib/action_view/helpers/ajax_helper.rb | 50 |
1 files changed, 27 insertions, 23 deletions
diff --git a/actionpack/lib/action_view/helpers/ajax_helper.rb b/actionpack/lib/action_view/helpers/ajax_helper.rb index 8707378738..da639b0658 100644 --- a/actionpack/lib/action_view/helpers/ajax_helper.rb +++ b/actionpack/lib/action_view/helpers/ajax_helper.rb @@ -3,6 +3,16 @@ module ActionView module AjaxHelper include UrlHelper + def remote_form_for(record_or_name_or_array, *args, &proc) + options = args.extract_options! + object_name = extract_object_name_for_form!(args, options, record_or_name_or_array) + + concat(form_remote_tag(options)) + fields_for(object_name, *(args << options), &proc) + concat('</form>'.html_safe!) + end + alias_method :form_remote_for, :remote_form_for + def form_remote_tag(options = {}, &block) attributes = {} attributes.merge!(extract_remote_attributes!(options)) @@ -73,41 +83,35 @@ module ActionView end def observe_field(name, options = {}) - if options[:url] - options[:url] = options[:url].is_a?(Hash) ? url_for(options[:url]) : options[:url] - end + url = options[:url] + options[:url] = url_for(url) if url && url.is_a?(Hash) - if options[:frequency] - case options[:frequency] - when 0 - options.delete(:frequency) - else - options[:frequency] = options[:frequency].to_i - end + frequency = options.delete(:frequency) + if frequency && frequency != 0 + options[:frequency] = frequency.to_i end - if options[:with] - if options[:with] !~ /[\{=(.]/ + if with = options[:with] + if with !~ /[\{=(.]/ options[:with] = "'#{options[:with]}=' + encodeURIComponent(value)" else options[:with] ||= 'value' unless options[:function] end end - if options[:function] - statements = options[:function] # || remote_function(options) # TODO: Need to implement remote function - BR + if function = options[:function] + statements = function # || remote_function(options) # TODO: Need to implement remote function - BR options[:function] = JSFunction.new(statements, "element", "value") end - options[:name] = name - <<-SCRIPT - <script type="application/json" data-js-type="field_observer"> - //<![CDATA[ - #{options.to_json} - // ]]> - </script> - SCRIPT + script_decorator("field_observer", options) + end + + def script_decorator(js_type, options) + attributes = [%(type="application/json"), %(data-js-type="#{js_type}")] + attributes += options.map{|k, v| %(data-#{k}="#{v}")} + "<script " + attributes.join(" ") + "></script>" end module Rails2Compatibility @@ -146,7 +150,7 @@ module ActionView @statements, @arguments = statements, arguments end - def as_json(options = nil) + def to_s(options = nil) "function(#{@arguments.join(", ")}) {#{@statements}}" end end |