aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2010-01-30 14:53:43 -0600
committerJoshua Peek <josh@joshpeek.com>2010-01-30 14:53:43 -0600
commit61c9b1648703c29133c3ea2ec083e275d95edbec (patch)
treec6ace1eaf0cd59b746dbdcc3a1caa17fbfe98a34 /actionpack
parent4f7a85d2c66f100f83e2fe264e1f03bf079f13dc (diff)
downloadrails-61c9b1648703c29133c3ea2ec083e275d95edbec.tar.gz
rails-61c9b1648703c29133c3ea2ec083e275d95edbec.tar.bz2
rails-61c9b1648703c29133c3ea2ec083e275d95edbec.zip
Move observe_field and observe_form to prototype_legacy_helper plugin
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_view/helpers/prototype_helper.rb189
-rw-r--r--actionpack/test/template/prototype_helper_test.rb37
2 files changed, 35 insertions, 191 deletions
diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb
index bef93dd0f8..92f4ca838f 100644
--- a/actionpack/lib/action_view/helpers/prototype_helper.rb
+++ b/actionpack/lib/action_view/helpers/prototype_helper.rb
@@ -43,16 +43,6 @@ module ActionView
#
# periodically_call_remote(:url => 'update', :frequency => '5', :update => 'ticker')
#
- # ...or through an observer (i.e., a form or field that is observed and calls a remote
- # action when changed).
- #
- # <%= observe_field(:searchbox,
- # :url => { :action => :live_search }),
- # :frequency => 0.5,
- # :update => :hits,
- # :with => 'query'
- # %>
- #
# As you can see, there are numerous ways to use Prototype's Ajax functions (and actually more than
# are listed here); check out the documentation for each method to find out more about its usage and options.
#
@@ -476,99 +466,6 @@ module ActionView
return function
end
- # Observes the field with the DOM ID specified by +field_id+ and calls a
- # callback when its contents have changed. The default callback is an
- # Ajax call. By default the value of the observed field is sent as a
- # parameter with the Ajax call.
- #
- # Example:
- # # Generates: new Form.Element.Observer('suggest', 0.25, function(element, value) {new Ajax.Updater('suggest',
- # # '/testing/find_suggestion', {asynchronous:true, evalScripts:true, parameters:'q=' + value})})
- # <%= observe_field :suggest, :url => { :action => :find_suggestion },
- # :frequency => 0.25,
- # :update => :suggest,
- # :with => 'q'
- # %>
- #
- # Required +options+ are either of:
- # <tt>:url</tt>:: +url_for+-style options for the action to call
- # when the field has changed.
- # <tt>:function</tt>:: Instead of making a remote call to a URL, you
- # can specify javascript code to be called instead.
- # Note that the value of this option is used as the
- # *body* of the javascript function, a function definition
- # with parameters named element and value will be generated for you
- # for example:
- # observe_field("glass", :frequency => 1, :function => "alert('Element changed')")
- # will generate:
- # new Form.Element.Observer('glass', 1, function(element, value) {alert('Element changed')})
- # The element parameter is the DOM element being observed, and the value is its value at the
- # time the observer is triggered.
- #
- # Additional options are:
- # <tt>:frequency</tt>:: The frequency (in seconds) at which changes to
- # this field will be detected. Not setting this
- # option at all or to a value equal to or less than
- # zero will use event based observation instead of
- # time based observation.
- # <tt>:update</tt>:: Specifies the DOM ID of the element whose
- # innerHTML should be updated with the
- # XMLHttpRequest response text.
- # <tt>:with</tt>:: A JavaScript expression specifying the parameters
- # for the XMLHttpRequest. The default is to send the
- # key and value of the observed field. Any custom
- # expressions should return a valid URL query string.
- # The value of the field is stored in the JavaScript
- # variable +value+.
- #
- # Examples
- #
- # :with => "'my_custom_key=' + value"
- # :with => "'person[name]=' + prompt('New name')"
- # :with => "Form.Element.serialize('other-field')"
- #
- # Finally
- # :with => 'name'
- # is shorthand for
- # :with => "'name=' + value"
- # This essentially just changes the key of the parameter.
- #
- # Additionally, you may specify any of the options documented in the
- # <em>Common options</em> section at the top of this document.
- #
- # Example:
- #
- # # Sends params: {:title => 'Title of the book'} when the book_title input
- # # field is changed.
- # observe_field 'book_title',
- # :url => 'http://example.com/books/edit/1',
- # :with => 'title'
- #
- #
- def observe_field(field_id, options = {})
- if options[:frequency] && options[:frequency] > 0
- build_observer('Form.Element.Observer', field_id, options)
- else
- build_observer('Form.Element.EventObserver', field_id, options)
- end
- end
-
- # Observes the form with the DOM ID specified by +form_id+ and calls a
- # callback when its contents have changed. The default callback is an
- # Ajax call. By default all fields of the observed field are sent as
- # parameters with the Ajax call.
- #
- # The +options+ for +observe_form+ are the same as the options for
- # +observe_field+. The JavaScript variable +value+ available to the
- # <tt>:with</tt> option is set to the serialized form by default.
- def observe_form(form_id, options = {})
- if options[:frequency]
- build_observer('Form.Observer', form_id, options)
- else
- build_observer('Form.EventObserver', form_id, options)
- end
- end
-
# All the methods were moved to GeneratorMethods so that
# #include_helpers_from_context has nothing to overwrite.
class JavaScriptGenerator #:nodoc:
@@ -1042,65 +939,49 @@ module ActionView
javascript_tag update_page(&block), html_options
end
- protected
- def options_for_ajax(options)
- js_options = build_callbacks(options)
-
- js_options['asynchronous'] = options[:type] != :synchronous
- js_options['method'] = method_option_to_s(options[:method]) if options[:method]
- js_options['insertion'] = "'#{options[:position].to_s.downcase}'" if options[:position]
- js_options['evalScripts'] = options[:script].nil? || options[:script]
-
- if options[:form]
- js_options['parameters'] = 'Form.serialize(this)'
- elsif options[:submit]
- js_options['parameters'] = "Form.serialize('#{options[:submit]}')"
- elsif options[:with]
- js_options['parameters'] = options[:with]
- end
-
- if protect_against_forgery? && !options[:form]
- if js_options['parameters']
- js_options['parameters'] << " + '&"
- else
- js_options['parameters'] = "'"
+ protected
+ def options_for_ajax(options)
+ js_options = build_callbacks(options)
+
+ js_options['asynchronous'] = options[:type] != :synchronous
+ js_options['method'] = method_option_to_s(options[:method]) if options[:method]
+ js_options['insertion'] = "'#{options[:position].to_s.downcase}'" if options[:position]
+ js_options['evalScripts'] = options[:script].nil? || options[:script]
+
+ if options[:form]
+ js_options['parameters'] = 'Form.serialize(this)'
+ elsif options[:submit]
+ js_options['parameters'] = "Form.serialize('#{options[:submit]}')"
+ elsif options[:with]
+ js_options['parameters'] = options[:with]
end
- js_options['parameters'] << "#{request_forgery_protection_token}=' + encodeURIComponent('#{escape_javascript form_authenticity_token}')"
- end
- options_for_javascript(js_options)
- end
-
- def method_option_to_s(method)
- (method.is_a?(String) and !method.index("'").nil?) ? method : "'#{method}'"
- end
+ if protect_against_forgery? && !options[:form]
+ if js_options['parameters']
+ js_options['parameters'] << " + '&"
+ else
+ js_options['parameters'] = "'"
+ end
+ js_options['parameters'] << "#{request_forgery_protection_token}=' + encodeURIComponent('#{escape_javascript form_authenticity_token}')"
+ end
- def build_observer(klass, name, options = {})
- if options[:with] && (options[:with] !~ /[\{=(.]/)
- options[:with] = "'#{options[:with]}=' + encodeURIComponent(value)"
- else
- options[:with] ||= 'value' unless options[:function]
+ options_for_javascript(js_options)
end
- callback = options[:function] || remote_function(options)
- javascript = "new #{klass}('#{name}', "
- javascript << "#{options[:frequency]}, " if options[:frequency]
- javascript << "function(element, value) {"
- javascript << "#{callback}}"
- javascript << ")"
- javascript_tag(javascript)
- end
+ def method_option_to_s(method)
+ (method.is_a?(String) and !method.index("'").nil?) ? method : "'#{method}'"
+ end
- def build_callbacks(options)
- callbacks = {}
- options.each do |callback, code|
- if CALLBACKS.include?(callback)
- name = 'on' + callback.to_s.capitalize
- callbacks[name] = "function(request){#{code}}"
+ def build_callbacks(options)
+ callbacks = {}
+ options.each do |callback, code|
+ if CALLBACKS.include?(callback)
+ name = 'on' + callback.to_s.capitalize
+ callbacks[name] = "function(request){#{code}}"
+ end
end
+ callbacks
end
- callbacks
- end
end
# Converts chained method calls on DOM proxy elements into JavaScript chains
diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb
index 9225153798..b37f71dd61 100644
--- a/actionpack/test/template/prototype_helper_test.rb
+++ b/actionpack/test/template/prototype_helper_test.rb
@@ -239,42 +239,6 @@ class PrototypeHelperTest < PrototypeHelperBaseTest
submit_to_remote("More beer!", 1_000_000, :update => "empty_bottle")
end
- def test_observe_field
- assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/reorder_if_empty', {asynchronous:true, evalScripts:true, parameters:value})})\n//]]>\n</script>),
- observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" })
- end
-
- def test_observe_field_using_with_option
- expected = %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/check_value', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(value)})})\n//]]>\n</script>)
- assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => 'id')
- assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "'id=' + encodeURIComponent(value)")
- end
-
- def test_observe_field_using_json_in_with_option
- expected = %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/check_value', {asynchronous:true, evalScripts:true, parameters:{'id':value}})})\n//]]>\n</script>)
- assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "{'id':value}")
- end
-
- def test_observe_field_using_function_for_callback
- assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {alert('Element changed')})\n//]]>\n</script>),
- observe_field("glass", :frequency => 5.minutes, :function => "alert('Element changed')")
- end
-
- def test_observe_form
- assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Observer('cart', 2, function(element, value) {new Ajax.Request('http://www.example.com/cart_changed', {asynchronous:true, evalScripts:true, parameters:value})})\n//]]>\n</script>),
- observe_form("cart", :frequency => 2, :url => { :action => "cart_changed" })
- end
-
- def test_observe_form_using_function_for_callback
- assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Observer('cart', 2, function(element, value) {alert('Form changed')})\n//]]>\n</script>),
- observe_form("cart", :frequency => 2, :function => "alert('Form changed')")
- end
-
- def test_observe_field_without_frequency
- assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.EventObserver('glass', function(element, value) {new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:value})})\n//]]>\n</script>),
- observe_field("glass")
- end
-
def test_update_page
old_output_buffer = output_buffer
@@ -294,7 +258,6 @@ class PrototypeHelperTest < PrototypeHelperBaseTest
assert_equal javascript_tag(create_generator(&block).to_s, {:defer => 'true'}), update_page_tag({:defer => 'true'}, &block)
end
-
protected
def author_path(record)
"/authors/#{record.id}"